0% found this document useful (0 votes)
98 views8 pages

Actividad 3 Images Computer Vision - Ipynb - Colaboratory

The document discusses image thresholding and binary images. It shows how to convert a grayscale image to a binary image by selecting a pixel value threshold. Pixels above the threshold are set to white (255) and below are set to black (0). It also demonstrates inverse thresholding, where pixels above the threshold are set to black and below to white. This inverse threshold image is used as a mask to segment objects from the original grayscale image. Examples are shown applying these techniques to images of coins and euro coins.

Uploaded by

Ana Velez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views8 pages

Actividad 3 Images Computer Vision - Ipynb - Colaboratory

The document discusses image thresholding and binary images. It shows how to convert a grayscale image to a binary image by selecting a pixel value threshold. Pixels above the threshold are set to white (255) and below are set to black (0). It also demonstrates inverse thresholding, where pixels above the threshold are set to black and below to white. This inverse threshold image is used as a mask to segment objects from the original grayscale image. Examples are shown applying these techniques to images of coins and euro coins.

Uploaded by

Ana Velez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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 matplotlib.pyplot as plt
5 import urllib.request
6 from google.colab.patches import cv2_imshow # for image display
7 from skimage import io
8 from PIL import Image 
9  

1 IMAGE_URL = 'https://github.com/agmarrugo/computer-vision-utb/blob/main/data/coins.png?r
2  
3 # urllib.request.urlretrieve(IMAGE_URL, 'coins.png')
4  
5 image = io.imread(IMAGE_URL) 
6 # image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
7 image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
8 blurred = cv2.GaussianBlur(image, (5, 5), 0)
9 # cv2.imshow("Image", image)
10  
11 # plt.imshow(image)
12 print("Grayscale image")
13 cv2_imshow(image)

Grayscale image

Thresholding

https://colab.research.google.com/drive/1_VE500Bp7eFet6TLRmEf5hU5pV4u6rFW#scrollTo=i90TDsBZX2hq&printMode=true 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) = cv2.threshold(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) = cv2.threshold(blurred, 155, 255, cv2.THRESH_BINARY_INV)
3  
4 print("Threshold Binary Inverse")
5 cv2_imshow(threshInv)
6  

https://colab.research.google.com/drive/1_VE500Bp7eFet6TLRmEf5hU5pV4u6rFW#scrollTo=i90TDsBZX2hq&printMode=true 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 matplotlib.pyplot as plt
8 import urllib.request
9 from google.colab.patches import cv2_imshow # for image display
10 from skimage import io
11 from PIL import Image 
12  
13 IMAGE_URL = 'https://upload.wikimedia.org/wikipedia/commons/b/b9/Euro_coins_line.jpg'
14  
15 # urllib.request.urlretrieve(IMAGE_URL, 'coins.png')
16  
17 image = io.imread(IMAGE_URL) 
18 # image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
19 image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
20 blurred = cv2.GaussianBlur(image, (5, 5), 0)
21 # cv2.imshow("Image", image)
22  
23 # plt.imshow(image)
24 print("Grayscale image")
25 cv2_imshow(image)
26  
https://colab.research.google.com/drive/1_VE500Bp7eFet6TLRmEf5hU5pV4u6rFW#scrollTo=i90TDsBZX2hq&printMode=true 3/8
2/9/2021 actividad 3_images_computer_vision.ipynb - Colaboratory

27 # Here we select T=155 as the threshold
28 (T, threshImg) = cv2.threshold(blurred, 155, 255, cv2.THRESH_BINARY)
29 print("Threshold Binary")
30 cv2_imshow(threshImg)
31  
32  
33 # Inverse thresholding
34 (T, threshInv) = cv2.threshold(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))

https://colab.research.google.com/drive/1_VE500Bp7eFet6TLRmEf5hU5pV4u6rFW#scrollTo=i90TDsBZX2hq&printMode=true 4/8
2/9/2021 actividad 3_images_computer_vision.ipynb - Colaboratory

Grayscale image

Threshold Binary

https://colab.research.google.com/drive/1_VE500Bp7eFet6TLRmEf5hU5pV4u6rFW#scrollTo=i90TDsBZX2hq&printMode=true 5/8
2/9/2021 actividad 3_images_computer_vision.ipynb - Colaboratory

Threshold Binary Inverse

https://colab.research.google.com/drive/1_VE500Bp7eFet6TLRmEf5hU5pV4u6rFW#scrollTo=i90TDsBZX2hq&printMode=true 6/8
2/9/2021 actividad 3_images_computer_vision.ipynb - Colaboratory

Coins

https://colab.research.google.com/drive/1_VE500Bp7eFet6TLRmEf5hU5pV4u6rFW#scrollTo=i90TDsBZX2hq&printMode=true 7/8
2/9/2021 actividad 3_images_computer_vision.ipynb - Colaboratory

check 2 s se ejecutó 23:48

https://colab.research.google.com/drive/1_VE500Bp7eFet6TLRmEf5hU5pV4u6rFW#scrollTo=i90TDsBZX2hq&printMode=true 8/8

You might also like