Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

OpenCV canny edge detection does not draw the bounding box

$
0
0

I have an image that i would like to do an edge detection and draw a bounding box to it, my problem is my python code does not draw the bounding box and im not sure if its because it was not able to detect any objects in it or im just drawing the rectangle wrong.

enter image description here

Here is my attempt

import cv2import numpy as npimg = 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() 

This produces this image

enter image description here

and I am expecting an image that is something like this:

enter image description here


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>