Sha*_*r P 1 python opencv image-processing contour computer-vision
我一直在尝试检测复选框。虽然我能够检测到其他图像中的方形轮廓,但我无法获取该特定图像的轮廓。请帮我检测复选框。
这是我的代码,
for myfile in files:
image=cv2.imread(myfile)
image = cv2.resize(image, (180,60), interpolation = cv2.INTER_AREA)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#apply otsu's threshold
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
#setting up threshold values
threshold_max_area = 300
threshold_min_area = 10
#finding contours in the image
cnts = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
#getting the coordinates for each checkbox
count=0
centers=[]
for c in cnts:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.035 * peri, True)
x,y,w,h = cv2.boundingRect(approx)
aspect_ratio = w / float(h)
area = cv2.contourArea(c)
if len(approx) == 4 and area < threshold_max_area and area > threshold_min_area and (aspect_ratio >= 0.9 and aspect_ratio <= 1.1):
centers.append([x,y,x+w,y+h])
count=count+1
print(centers)
cv2.imshow(" ",image)
cv2.waitKey()
Run Code Online (Sandbox Code Playgroud)
这是解决该问题的一种方法。
import os
import cv2
import numpy as np
import pandas as pd
### reading input image
image_path='test_sample.jpg'
image=cv2.imread(image_path)
### converting BGR to Grayscale
gray_scale=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
### Binarising image
th1,img_bin = cv2.threshold(gray_scale,180,225,cv2.THRESH_OTSU)
Run Code Online (Sandbox Code Playgroud)
二值图像:
### defining kernels
lWidth = 2
lineMinWidth = 15
kernal1 = np.ones((lWidth,lWidth), np.uint8)
kernal1h = np.ones((1,lWidth), np.uint8)
kernal1v = np.ones((lWidth,1), np.uint8)
kernal6 = np.ones((lineMinWidth,lineMinWidth), np.uint8)
kernal6h = np.ones((1,lineMinWidth), np.uint8)
kernal6v = np.ones((lineMinWidth,1), np.uint8)
### finding horizontal lines
img_bin_h = cv2.morphologyEx(~img_bin, cv2.MORPH_CLOSE, kernal1h) # bridge small gap in horizonntal lines
img_bin_h = cv2.morphologyEx(img_bin_h, cv2.MORPH_OPEN, kernal6h) # kep ony horiz lines by eroding everything else in hor direction
Run Code Online (Sandbox Code Playgroud)
水平线:
## detect vert lines
img_bin_v = cv2.morphologyEx(~img_bin, cv2.MORPH_CLOSE, kernal1v) # bridge small gap in vert lines
img_bin_v = cv2.morphologyEx(img_bin_v, cv2.MORPH_OPEN, kernal6v)# kep ony vert lines by eroding everything else in vert direction
Run Code Online (Sandbox Code Playgroud)
垂直线:
def fix(img):
img[img>127]=255
img[img<127]=0
return img
img_bin_final = fix(fix(img_bin_h)|fix(img_bin_v))
Run Code Online (Sandbox Code Playgroud)
组合二进制输出:
### getting labels
ret, labels, stats,centroids = cv2.connectedComponentsWithStats(~img_bin_final, connectivity=8, ltype=cv2.CV_32S)
### drawing recangles for visualisation
for x,y,w,h,area in stats[2:]:
cv2.rectangle(image,(x,y),(x+w,y+h),(0,0,255),2)
Run Code Online (Sandbox Code Playgroud)