You are on page 1of 11

6/11/2021 MIDTASK.

ipynb - Colaboratory

1 #BASIC DATA STRUCTURES IN OPENCV

1 import matplotlib, cv2


2 import numpy as np
3 import matplotlib.pyplot as plt
4
5 %matplotlib inline
6 # read an image
7 img = cv2.imread('/content/Az.jpeg')
8
9 # show image format (basically a 3-d array of pixel color info, in BGR format)
10 print(img)
11

[[[ 0 29 4]
[ 0 29 4]
[ 0 30 3]
...
[ 35 76 31]
[ 35 76 31]
[ 35 76 31]]

[[ 0 29 4]
[ 0 29 4]
[ 0 31 4]
...
[ 36 77 32]
[ 33 74 29]
[ 31 74 29]]

[[ 0 29 4]
[ 0 30 5]
[ 0 30 5]
...
[ 34 77 34]
[ 28 71 26]
[ 26 72 26]]

...

[[ 86 137 75]
[ 86 137 75]
[ 87 140 77]
...
[ 23 16 1]
[ 28 20 7]
[ 28 20 7]]

[[ 86 137 75]
[ 87 138 76]
[ 88 141 78]
...
[ 27 20 5]
[ 22 14 1]
[ 22 14 1]]

[[ 86 137 75]
[ 87 138 76]
[ 88 141 78]
...
[ 27 20 5]
[ 22 14 1]
[ 22 14 1]]]

1 #COLORS AND COLOR CONVERSIONS

1 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)


2 plt.imshow(img)

https://colab.research.google.com/drive/1GjErfqx20qS5yqUhHdFkSZLTdzeWd7h4#scrollTo=gXrxcbiEpoqH&printMode=true 1/11
6/11/2021 MIDTASK.ipynb - Colaboratory

<matplotlib.image.AxesImage at 0x7f9a69489c90>

1 gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)


2 print(gray_img)

[[ 18 18 19 ... 58 58 58]
[ 18 18 19 ... 59 56 56]
[ 18 19 19 ... 59 53 53]
...
[113 113 115 ... 12 17 17]
[113 114 116 ... 16 11 11]
[113 114 116 ... 16 11 11]]

1 plt.imshow(cv2.cvtColor(gray_img, cv2.COLOR_GRAY2RGB))

<matplotlib.image.AxesImage at 0x7f9a69404d10>

1 #FINDING THE AVERAGE COLOR OF AN IMAGE

1 average_color_per_row = np.average(img, axis=0)


2 average_color = np.average(average_color_per_row, axis=0)
3 average_color = np.uint8(average_color)
4 print(average_color)

[53 73 46]

1 average_color_img = np.array([[average_color]*100]*100, np.uint8)


2 plt.imshow(average_color_img)

https://colab.research.google.com/drive/1GjErfqx20qS5yqUhHdFkSZLTdzeWd7h4#scrollTo=gXrxcbiEpoqH&printMode=true 2/11
6/11/2021 MIDTASK.ipynb - Colaboratory

<matplotlib.image.AxesImage at 0x7f9a6837b450>

1 #BINARY THRESHOLDING ON GRAYSCALE IMAGE

1 _, threshold_img = cv2.threshold(gray_img, 60, 255, cv2.THRESH_BINARY)


2 threshold_img = cv2.cvtColor(threshold_img, cv2.COLOR_GRAY2RGB)
3 plt.figure(figsize=(10,10))
4 plt.imshow(threshold_img)

<matplotlib.image.AxesImage at 0x7f9a682d7f10>

1 #BINARY THRESHOLDING ON COLOR

1 piet = cv2.imread('/content/color.jpg')
2 piet_hsv = cv2.cvtColor(piet, cv2.COLOR_BGR2HSV)

1 plt.imshow(cv2.cvtColor(piet, cv2.COLOR_BGR2RGB))

https://colab.research.google.com/drive/1GjErfqx20qS5yqUhHdFkSZLTdzeWd7h4#scrollTo=gXrxcbiEpoqH&printMode=true 3/11
6/11/2021 MIDTASK.ipynb - Colaboratory

<matplotlib.image.AxesImage at 0x7f9a682c9f90>

1 blue_min = np.array([80, 60, 60], np.uint8)


2 blue_max = np.array([150, 255, 255], np.uint8)
3 threshold_blue_img = cv2.inRange(piet_hsv, blue_min, blue_max)
4 threshold_blue_img = cv2.cvtColor(threshold_blue_img, cv2.COLOR_GRAY2RGB)
5 plt.imshow(threshold_blue_img)

<matplotlib.image.AxesImage at 0x7f9a6823eed0>

1 #USING BINARY THRESHOLDING TO OBTAIN AN IMAGE MASK

1 upstate = cv2.imread('/content/biru.webp')
2 upstate_hsv =cv2.cvtColor(upstate, cv2.COLOR_BGR2HSV)
3 plt.imshow(cv2.cvtColor(upstate_hsv, cv2.COLOR_HSV2RGB))

<matplotlib.image.AxesImage at 0x7f9a68101c50>

1 mask_inverse =cv2.inRange(upstate_hsv, blue_min, blue_max)


2 mask = cv2.bitwise_not(mask_inverse)
3 plt.imshow(cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB))

<matplotlib.image.AxesImage at 0x7f9a6945f750>

1 mask_rgb = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB)


2 masked upstate = cv2.bitwise and(upstate, mask rgb)
https://colab.research.google.com/drive/1GjErfqx20qS5yqUhHdFkSZLTdzeWd7h4#scrollTo=gXrxcbiEpoqH&printMode=true 4/11
6/11/2021 MIDTASK.ipynb - Colaboratory
_ p _ ( p , _ g )
3 masked_replace_white = cv2.addWeighted(masked_upstate, 1, \
4 cv2.cvtColor(mask_inverse,cv2.COLOR_GRAY2RGB), 1, 0)
5 plt.imshow(cv2.cvtColor(masked_replace_white, cv2.COLOR_BGR2RGB))

<matplotlib.image.AxesImage at 0x7f9a68166e90>

1 #GAUSSIAN BLUR

1 img =cv2.imread('/content/city.jpg')
2 plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

<matplotlib.image.AxesImage at 0x7f9a67f85990>

1 img_blur_small = cv2.GaussianBlur(img, (5,5), 0)


2 cv2.imwrite('output/oy-gaussian-blur-5.jpg', img_blur_small)
3 plt.imshow(cv2.cvtColor(img_blur_small, cv2.COLOR_BGR2RGB))

<matplotlib.image.AxesImage at 0x7f9a67efa190>

1 img_blur_small = cv2.GaussianBlur(img, (5,5), 25)


2 cv2.imwrite('output/oy-gaussian-blur-5-3.jpg', img_blur_small)
3 plt.imshow(cv2.cvtColor(img_blur_small, cv2.COLOR_BGR2RGB))

https://colab.research.google.com/drive/1GjErfqx20qS5yqUhHdFkSZLTdzeWd7h4#scrollTo=gXrxcbiEpoqH&printMode=true 5/11
6/11/2021 MIDTASK.ipynb - Colaboratory

<matplotlib.image.AxesImage at 0x7f9a67e65650>

1 img_blur_large = cv2.GaussianBlur(img, (15,15), 0)


2 cv2.imwrite('output/oy-gaussian-blur-15.jpg', img_blur_large)
3 plt.imshow(cv2.cvtColor(img_blur_large, cv2.COLOR_BGR2RGB))

<matplotlib.image.AxesImage at 0x7f9a67dceb10>

1 gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)


2 _, threshold_img = cv2.threshold(gray_img, 100, 255, cv2.THRESH_BINARY)
3 cv2.imwrite('output/oy-no-blur-thresh.jpg', threshold_img)
4
5 plt.imshow(cv2.cvtColor(threshold_img, cv2.COLOR_GRAY2RGB))

<matplotlib.image.AxesImage at 0x7f9a67dc72d0>

1 gray_blur_img = cv2.cvtColor(img_blur_small, cv2.COLOR_BGR2GRAY)


2 _, threshold_img_blur = cv2.threshold(gray_blur_img, 100, 255, cv2.THRESH_BINARY)
3 cv2.imwrite('output/oy-gaussian-blur-5-thresh.jpg', threshold_img_blur)
4 plt.imshow(cv2.cvtColor(threshold_img_blur, cv2.COLOR_GRAY2RGB))

<matplotlib.image.AxesImage at 0x7f9a67d337d0>

1 adaptive_thresh = cv2.adaptiveThreshold(gray_img,255,\
2 cv2 ADAPTIVE THRESH GAUSSIAN C \
https://colab.research.google.com/drive/1GjErfqx20qS5yqUhHdFkSZLTdzeWd7h4#scrollTo=gXrxcbiEpoqH&printMode=true 6/11
6/11/2021 MIDTASK.ipynb - Colaboratory
2 cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
3 cv2.THRESH_BINARY,11,2)
4 plt.imshow(cv2.cvtColor(adaptive_thresh, cv2.COLOR_GRAY2RGB))

<matplotlib.image.AxesImage at 0x7f9a67ca2990>

1 #CONTOUR AND BOUNDING BOXES

1 coins = cv2.imread('/content/coin.jpg')
2 plt.imshow(cv2.cvtColor(coins, cv2.COLOR_BGR2RGB))
3 cv2.imwrite('output/coins-orig.jpg', coins)

False

1 coins_gray = cv2.cvtColor(coins, cv2.COLOR_BGR2GRAY)


2 coins_preprocessed = cv2.GaussianBlur(coins_gray, (5, 5), 0)
3 plt.imshow(cv2.cvtColor(coins_preprocessed, cv2.COLOR_GRAY2RGB))

<matplotlib.image.AxesImage at 0x7f9a67834f90>

1 _, coins_binary = cv2.threshold(coins_preprocessed, 130, 255, cv2.THRESH_BINARY)


2
3 coins_binary = cv2.bitwise_not(coins_binary)
4 plt.imshow(cv2.cvtColor(coins_binary, cv2.COLOR_GRAY2RGB))
5 cv2.imwrite('ouput/coins-binary.png', coins_binary)

https://colab.research.google.com/drive/1GjErfqx20qS5yqUhHdFkSZLTdzeWd7h4#scrollTo=gXrxcbiEpoqH&printMode=true 7/11
6/11/2021 MIDTASK.ipynb - Colaboratory

False

1 morph_karnel = np.ones((15,15),np.uint8)
2 coins_morph = cv2.morphologyEx(coins_binary, cv2.MORPH_CLOSE, morph_karnel)
3
4 plt.imshow(cv2.cvtColor(coins_morph, cv2.COLOR_GRAY2RGB))

<matplotlib.image.AxesImage at 0x7f9a67719990>

1 coins_contours, _= cv2.findContours(coins_binary, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)


2
3 coins_and_contours = np.copy(coins)
4
5 min_coin_area = 60
6 large_contours = [cnt for cnt in coins_contours if cv2.contourArea(cnt) > min_coin_area]
7
8 cv2.drawContours(coins_and_contours, large_contours, -1, (255,0,0))
9
10 plt.imshow(cv2.cvtColor(coins_and_contours, cv2.COLOR_BGR2RGB))
11 cv2.imwrite('output/coins-contour.jpg', coins_and_contours)

False

1 #FIND BOUNDING BOXES

1 bounding_img = np.copy(coins)
2
3
4 for contour in large contours:
https://colab.research.google.com/drive/1GjErfqx20qS5yqUhHdFkSZLTdzeWd7h4#scrollTo=gXrxcbiEpoqH&printMode=true 8/11
6/11/2021 MIDTASK.ipynb - Colaboratory
4 for contour in large_contours:
5 x, y, w, h = cv2.boundingRect(contour)
6 cv2.rectangle(bounding_img, (x, y), (x + w, y + h), (0, 255, 0), 3)
7
8 plt.imshow(cv2.cvtColor(bounding_img, cv2.COLOR_BGR2RGB))
9 cv2.imwrite('output/coins-bounding.jpg', bounding_img)

False

1 #EDGE DETECTIONS

1 cups = cv2.imread('/content/bHszC.jpg')
2 plt.imshow(cv2.cvtColor(cups, cv2.COLOR_BGR2RGB))
3 cv2.imwrite('output/cup-orig.jpg', cups)

False

1 cups_preprocessed =cv2.cvtColor(cv2.GaussianBlur(cups, (7,7), 0), cv2.COLOR_BGR2GRAY)

1 low_thresh = 120
2 high_thresh = 200
3 _, cups_thresh = cv2.threshold(cups_preprocessed, low_thresh, 255, cv2.THRESH_BINARY)
4 plt.imshow(cv2.cvtColor(cups_thresh, cv2.COLOR_GRAY2RGB))
5 cv2.imwrite('ouput/cups-thresh-low.png', cups_thresh)
6
7 _, cups_thresh_hi = cv2.threshold(cups_preprocessed, high_thresh, 255, cv2.THRESH_BINARY)
8 cv2.imwrite('ouput/cups-thresh-hi-.png', cups_thresh_hi)

https://colab.research.google.com/drive/1GjErfqx20qS5yqUhHdFkSZLTdzeWd7h4#scrollTo=gXrxcbiEpoqH&printMode=true 9/11
6/11/2021 MIDTASK.ipynb - Colaboratory

False

1 cups_edges = cv2.Canny(cups_preprocessed, threshold1=90, threshold2=110)


2 plt.imshow(cv2.cvtColor(cups_edges, cv2.COLOR_GRAY2RGB))
3 cv2.imwrite('ouput/cup-edges.png', cups_edges)

False

1 #HOUGH TRANSFORM

1 circles = cv2.HoughCircles(cups_edges, cv2.HOUGH_GRADIENT, dp=1.5, minDist=50, minRadius=20, maxRadius=


2 cups_circles = np.copy(cups)
3
4 if circles is not None and len(circles) > 0:
5 for (x, y, r) in circles[0]:
6 x, y, r = int(x), int(y), int(r)
7 cv2.circle(cups_circles, (x, y), r, (255, 255, 0), 4)
8 plt.imshow(cv2.cvtColor(cups_circles, cv2.COLOR_BGR2RGB))
9
10 print('number of circle detected: %d' % len(circles[0]))
11 cv2.imwrite('output/cups-circles.jpg', cups)

number of circle detected: 2


False

1 # copy of image to draw lines


2 cups_lines = np.copy(cups)
3
4 # find hough lines
5 num_pix_threshold = 110 # minimum number of pixels that must be on a line
6 lines = cv2.HoughLines(cups_edges, 1, np.pi/180, num_pix_threshold)
7
8 for rho, theta in lines[0]:
9 # convert line equation into start and end points of line
10 a = np.cos(theta)
11 b = np.sin(theta)
12 x0 = a * rho
13 y0 = b * rho
14
15 x1 = int(x0 + 1000*(-b))
https://colab.research.google.com/drive/1GjErfqx20qS5yqUhHdFkSZLTdzeWd7h4#scrollTo=gXrxcbiEpoqH&printMode=true 10/11
6/11/2021 MIDTASK.ipynb - Colaboratory
5 t( 0 000 ( b))
16 y1 = int(y0 + 1000*(a))
17
18 x2 = int(x0 - 1000*(-b))
19 y2 = int(y0 - 1000*(a))
20
21 cv2.line(cups_lines, (x1,y1), (x2,y2), (0,0,255), 1)
22 plt.imshow(cv2.cvtColor(cups_lines, cv2.COLOR_BGR2RGB))
23 cv2.imwrite('ouput/cups-lines.jpg', cups_lines)

False

 1s completed at 8:39 AM

https://colab.research.google.com/drive/1GjErfqx20qS5yqUhHdFkSZLTdzeWd7h4#scrollTo=gXrxcbiEpoqH&printMode=true 11/11

You might also like