问题描述
我想在下图中使用opencv在“已删除”一词周围画一个框,并找到坐标。
我从以下代码获得了上图:
kernel = np.ones((3,3),np.uint8)
dilation = cv2.dilate(img,kernel,iterations =1)
plt.imshow(dilation)
原始图片是:
解决方法
以您已经编写的代码为基础,您需要反转结果并应用findContours()
。
inv_img = cv2.bitwise_not(dilation)
contours,hierarchy = cv2.findContours(gray_in,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
rect = cv2.minAreaRect(cnt)
box = cv2.cv.BoxPoints(rect) # cv2.boxPoints(rect) for OpenCV 3.x
box = np.int0(box)
cv2.drawContours(im,[box],(0,255),2)
您需要查看哪个框更好,哪个轮廓最合适。 这个答案会有所帮助: Python OpenCV Box2D