2/9/2021 actividad 3_images_computer_vision.
ipynb - Colaboratory
Histograms and binary images
Andres Marrugo, PhD
Universidad Tecnológica de Bolívar
1 # import cv2 as cv
2 import cv2
3 import numpy as np
4 import [Link] as plt
5 import [Link]
6 from [Link] import cv2_imshow # for image display
7 from skimage import io
8 from PIL import Image
9
1 IMAGE_URL = '[Link]
2
3 # [Link](IMAGE_URL, '[Link]')
4
5 image = [Link](IMAGE_URL)
6 # image = [Link](image, cv.COLOR_BGR2RGB)
7 image = [Link](image, cv2.COLOR_BGR2GRAY)
8 blurred = [Link](image, (5, 5), 0)
9 # [Link]("Image", image)
10
11 # [Link](image)
12 print("Grayscale image")
13 cv2_imshow(image)
Grayscale image
Thresholding
[Link] 1/8
2/9/2021 actividad 3_images_computer_vision.ipynb - Colaboratory
Thresholding is the binarization of an image. In general,
we seek to convert a grayscale image to a
binary image,
where the pixels are either 0 or 255.
A simple thresholding example would be
selecting a pixel
value p, and then setting all pixel intensities less than p to
zero, and all pixel values
greater than p to 255. In this way,
we are able to create a binary representation of the image.
1 # Here we select T=155 as the threshold
2 (T, threshImg) = [Link](blurred, 155, 255, cv2.THRESH_BINARY)
3 print("Threshold Binary")
4 cv2_imshow(threshImg)
Threshold Binary
In this first example, any pixel value that is greater than 155 is set to 255. Any value that is less than
155 is set to zero.
We used the cv2.THRESH_BINARY method, which indicates that pixel values p greater than T are set
to the maximum value (the third argument).
1 # Inverse thresholding
2 (T, threshInv) = [Link](blurred, 155, 255, cv2.THRESH_BINARY_INV)
3
4 print("Threshold Binary Inverse")
5 cv2_imshow(threshInv)
6
[Link] 2/8
2/9/2021 actividad 3_images_computer_vision.ipynb - Colaboratory
Threshold Binary Inverse
We applied inverse thresholding rather than
normal thresholding by using cv2.THRESH_BINARY_INV
as
our thresholding method. As we can see in the image above, our coins are now white and the
background is black. This is convenient for producing a mask.
1 # Inverse thresholding image as a mask
2 print("Coins")
3 cv2_imshow(cv2.bitwise_and(image, image, mask=threshInv))
We performed masking by using the cv2.bitwise_and function. We supplied our original coin
image as the first
two arguments, and then our inverted thresholded image as
our mask.
Remember, a mask only considers pixels in the
original image where the mask is greater than zero.
Since our inverted thresholded image does a good job
at approximating the areas the coins are
contained in, we
can use this inverted thresholded image as our mask.
1 # TODO repeat the above operations with an image of your choosing
2 # that is ease to segment.
3
4 # import cv2 as cv
5 import cv2
6 import numpy as np
7 import [Link] as plt
8 import [Link]
9 from [Link] import cv2_imshow # for image display
10 from skimage import io
11 from PIL import Image
12
13 IMAGE_URL = '[Link]
14
15 # [Link](IMAGE_URL, '[Link]')
16
17 image = [Link](IMAGE_URL)
18 # image = [Link](image, cv.COLOR_BGR2RGB)
19 image = [Link](image, cv2.COLOR_BGR2GRAY)
20 blurred = [Link](image, (5, 5), 0)
21 # [Link]("Image", image)
22
23 # [Link](image)
24 print("Grayscale image")
25 cv2_imshow(image)
26
[Link] 3/8
2/9/2021 actividad 3_images_computer_vision.ipynb - Colaboratory
27 # Here we select T=155 as the threshold
28 (T, threshImg) = [Link](blurred, 155, 255, cv2.THRESH_BINARY)
29 print("Threshold Binary")
30 cv2_imshow(threshImg)
31
32
33 # Inverse thresholding
34 (T, threshInv) = [Link](blurred, 155, 255, cv2.THRESH_BINARY_INV)
35
36 print("Threshold Binary Inverse")
37 cv2_imshow(threshInv)
38
39 # Inverse thresholding image as a mask
40 print("Coins")
41 cv2_imshow(cv2.bitwise_and(image, image, mask=threshInv))
[Link] 4/8
2/9/2021 actividad 3_images_computer_vision.ipynb - Colaboratory
Grayscale image
Threshold Binary
[Link] 5/8
2/9/2021 actividad 3_images_computer_vision.ipynb - Colaboratory
Threshold Binary Inverse
[Link] 6/8
2/9/2021 actividad 3_images_computer_vision.ipynb - Colaboratory
Coins
[Link] 7/8
2/9/2021 actividad 3_images_computer_vision.ipynb - Colaboratory
check 2 s se ejecutó 23:48
[Link] 8/8