OpenCV Python Notes - Object Detection

OpenCV Python Notes - Object Detection
In [16]:
import numpy as np
import cv2
import matplotlib.pyplot as plt
import random
In [3]:
def show_image(title, bgr_image):
    rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)
    plt.axis('off')
    plt.title(title)
    plt.imshow(rgb_image)
    plt.show()


def show_bgra_image(title, bgra_image):
    b = bgra_image[:,:,0]
    g = bgra_image[:,:,1]
    r = bgra_image[:,:,2]
    a = bgra_image[:,:,3]
    rgba_image = cv2.merge([r, g, b, a])
    
    plt.axis('off')
    plt.title(title)
    plt.imshow(rgba_image)
    plt.show()
In [7]:
bw = cv2.imread('detect_blob.png', cv2.IMREAD_GRAYSCALE)
height, width = bw.shape[0:2]
show_image("Original BW",bw)

binary = np.zeros([height,width,1],'uint8')

thresh = 85

for row in range(0,height):
	for col in range(0, width):
		if bw[row][col]>thresh:
			binary[row][col]=255

show_image("Slow Binary",binary)

ret, thresh = cv2.threshold(bw,thresh,255,cv2.THRESH_BINARY)
show_image("CV Threshold",thresh)
In [9]:
img = cv2.imread('sudoku.png', cv2.IMREAD_GRAYSCALE)
show_image("Original",img)

ret, thresh_basic = cv2.threshold(img,70,255,cv2.THRESH_BINARY)
show_image("Basic Binary",thresh_basic)

thres_adapt = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 115, 1)
show_image("Adaptive Threshold",thres_adapt)
In [10]:
img = cv2.imread('faces.jpeg', cv2.IMREAD_COLOR)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h = hsv[:,:,0]
s = hsv[:,:,1]
v = hsv[:,:,2]

hsv_split = np.concatenate((h,s,v), axis=1)
show_image("Split HSV",hsv_split)

ret, min_sat = cv2.threshold(s,40,255, cv2.THRESH_BINARY)
show_image("Sat Filter",min_sat)

ret, max_hue = cv2.threshold(h,15, 255, cv2.THRESH_BINARY_INV)
show_image("Hue Filter",max_hue)

final = cv2.bitwise_and(min_sat,max_hue)
show_image("Final",final)
show_image("Original",img)
In [12]:
img = cv2.imread('detect_blob.png', cv2.IMREAD_COLOR)
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 115, 1)
show_image("Binary", thresh)

contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

img2 = img.copy()
index = -1
thickness = 4
color = (255, 0, 255)

cv2.drawContours(img2, contours, index, color, thickness)
show_image("Contours",img2)
In [13]:
img = cv2.imread('detect_blob.png', cv2.IMREAD_COLOR)
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 115, 1)
cv2.imshow("Binary", thresh)

contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

img2 = img.copy()
index = -1
thickness = 4
color = (255, 0, 255)

objects = np.zeros([img.shape[0], img.shape[1],3], 'uint8')
for c in contours:
	cv2.drawContours(objects, [c], -1, color, -1)

	area = cv2.contourArea(c)
	perimeter = cv2.arcLength(c, True)

	M = cv2.moments(c)
	cx = int( M['m10']/M['m00'])
	cy = int( M['m01']/M['m00'])
	cv2.circle(objects, (cx,cy), 4, (0,0,255), -1)

	print("Area: {}, perimeter: {}".format(area,perimeter))

show_image("Contours",objects)
Area: 86.5, perimeter: 45.55634891986847
Area: 959.5, perimeter: 251.8406196832657
Area: 13091.5, perimeter: 754.0416301488876
Area: 10069.5, perimeter: 401.41421353816986
Area: 7780.0, perimeter: 329.22034430503845
Area: 4160.0, perimeter: 258.0
Area: 1672.0, perimeter: 160.48528122901917
Area: 14515.0, perimeter: 1225.768675327301
Area: 6357.0, perimeter: 446.9116872549057
Area: 7718.0, perimeter: 484.8284270763397
Area: 4592.5, perimeter: 502.0315263271332
Area: 5014.0, perimeter: 357.2792183160782
Area: 5019.0, perimeter: 444.3675308227539
Area: 8829.0, perimeter: 450.5929263830185
Area: 108.0, perimeter: 41.79898953437805
Area: 551.5, perimeter: 93.41421353816986
Area: 2707.5, perimeter: 194.75230765342712
Area: 1644.5, perimeter: 152.1248904466629
Area: 767.0, perimeter: 105.74011433124542
Area: 3501.5, perimeter: 251.0710676908493
Area: 8556.0, perimeter: 345.70562493801117
Area: 8868.0, perimeter: 378.8284270763397
Area: 865.0, perimeter: 185.4558435678482
Area: 1482.0, perimeter: 244.16652035713196
Area: 747.0, perimeter: 102.9116872549057
Area: 1638.0, perimeter: 152.36753034591675
Area: 1.0, perimeter: 4114.82842707634
In [15]:
img = cv2.imread("tomatoes.jpg", cv2.IMREAD_COLOR)

show_image("Tomatoes", img)

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
res,thresh = cv2.threshold(hsv[:,:,0], 25, 255, cv2.THRESH_BINARY_INV)
show_image("Thresh",thresh)

edges = cv2.Canny(img, 100, 70)
show_image("Canny",edges)
In [19]:
img = cv2.imread("fuzzy.png",cv2.IMREAD_COLOR)
show_image("Original",img)

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3),0)

thresh = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 205, 1)
show_image("Binary",thresh)

contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
print("len(contours) =", len(contours))

filtered = []
for c in contours:
	if cv2.contourArea(c) < 1000:continue
	filtered.append(c)

print("len(filtered) =", len(filtered))

objects = np.zeros([img.shape[0],img.shape[1],3], 'uint8')
for c in filtered:
	col = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
	cv2.drawContours(objects,[c], -1, col, -1)
	area = cv2.contourArea(c)
	p = cv2.arcLength(c,True)
	print("area, p =", area,p)

show_image("Contours",objects)
len(contours) = 2054
len(filtered) = 4
area, p = 4926.0 298.2253956794739
area, p = 29882.0 795.3868639469147
area, p = 1038.0 645.0681030750275
area, p = 17250.0 585.0782079696655