如何用OpenCV定位图片中的化学检测试纸?Canny检测未生成目标Bounding Box
问题描述
我有一张包含化学检测试纸的图片,希望通过边缘检测为其绘制Bounding Box,但我的Python代码未能生成预期的框,不确定是未检测到目标还是绘制逻辑有误。

尝试代码
import cv2 import numpy as np img = cv2.imread("image1.jpg") (B, G, R) = cv2.split(img) img = cv2.Canny(B, 20, 100) # Blue channel gives the best box so far # img = cv2.Canny(R, 20, 100) # img = cv2.Canny(R, 20, 100) ret,thresh = cv2.threshold(img,20,100,0) contours,hierarchy = cv2.findContours(thresh, 1, 2) cnt = contours[0] M = cv2.moments(cnt) for c in contours: rect = cv2.minAreaRect(c) box = cv2.boxPoints(rect) box = np.intp(box) img = cv2.drawContours(img,[box],0,(0,0,255),2) # display the image with bounding rectangle drawn on it # cv2.namedWindow('Bounding Rectangle', cv2.WINDOW_KEEPRATIO) cv2.imshow('Bounding Rectangle', img) cv2.waitKey(0) cv2.destroyAllWindows()
当前输出效果

预期效果

内容的提问来源于stack exchange,提问作者THESIS
相关产品推荐
相关产品推荐

