OpenCV 找到该图像的轮廓以进行裁剪和旋转
OpenCV find contour of this image to crop and rotate
我正在尝试检测此图像的轮廓以便在 openCV 中对其进行裁剪。
我已经想出了工作代码,但是,如果图像上有一些轻微的背景,它就会失败。
图像处理:
检测边界(蓝点):
Crop/rotate:
但是,对于像这样的图像,有一些背景光,它不起作用:
预处理:
边界检测:
def preProcessing(img):
imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
adaptive_thresold1 = 31
adaptive_thresold2 = 7
blur = cv2.blur(imgGray, (3, 3))
thresh = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,adaptive_thresold1,adaptive_thresold2)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
close = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=2)
stackedImages = hp.stackImages(0.1,([img,thresh, close],[img,thresh, close]))
cv2.imshow("WorkFlow", stackedImages)
cv2.waitKey(0)
return thresh
def getContours(img):
biggest = np.array([])
maxArea = 0
img = cv2.bitwise_not(img)
contours,hierarchy = cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
for cnt in contours:
area = cv2.contourArea(cnt)
if area>5000:
print (area)
#cv2.drawContours(imgContour, cnt, -1, (255, 0, 0), 3)
peri = cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt,0.02*peri,True)
if area >maxArea and len(approx) == 4:
biggest = approx
maxArea = area
print ("ok")
print (biggest)
out = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
cv2.drawContours(out, biggest, -1, (255, 0, 0), 50)
stackedImages = hp.stackImages(0.1,([img,out],[img,out]))
cv2.imshow("WorkFlow", stackedImages)
cv2.waitKey(0)
return biggest
有什么建议可以使此代码更可靠吗?
尝试使用 Otsu's thresholding。
而不是使用自适应阈值
更改此行
thresh = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,adaptive_thresold1,adaptive_thresold2)
在你的代码中 -
retval_blue, thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
这在图像中对我有用。
我正在尝试检测此图像的轮廓以便在 openCV 中对其进行裁剪。
我已经想出了工作代码,但是,如果图像上有一些轻微的背景,它就会失败。
图像处理:
检测边界(蓝点):
Crop/rotate:
但是,对于像这样的图像,有一些背景光,它不起作用:
预处理:
边界检测:
def preProcessing(img):
imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
adaptive_thresold1 = 31
adaptive_thresold2 = 7
blur = cv2.blur(imgGray, (3, 3))
thresh = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,adaptive_thresold1,adaptive_thresold2)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
close = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=2)
stackedImages = hp.stackImages(0.1,([img,thresh, close],[img,thresh, close]))
cv2.imshow("WorkFlow", stackedImages)
cv2.waitKey(0)
return thresh
def getContours(img):
biggest = np.array([])
maxArea = 0
img = cv2.bitwise_not(img)
contours,hierarchy = cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
for cnt in contours:
area = cv2.contourArea(cnt)
if area>5000:
print (area)
#cv2.drawContours(imgContour, cnt, -1, (255, 0, 0), 3)
peri = cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt,0.02*peri,True)
if area >maxArea and len(approx) == 4:
biggest = approx
maxArea = area
print ("ok")
print (biggest)
out = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
cv2.drawContours(out, biggest, -1, (255, 0, 0), 50)
stackedImages = hp.stackImages(0.1,([img,out],[img,out]))
cv2.imshow("WorkFlow", stackedImages)
cv2.waitKey(0)
return biggest
有什么建议可以使此代码更可靠吗?
尝试使用 Otsu's thresholding。
而不是使用自适应阈值更改此行
thresh = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,adaptive_thresold1,adaptive_thresold2)
在你的代码中 -
retval_blue, thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
这在图像中对我有用。