I'm super new to opencv, so pardon my ignorance...
Basically: I have an object of interest in my image. I would like to extract it.
My problems arise from downscaling the original image in order to facilitate processing. I have found a contour of the object on the smaller image. What I would really like to do is use the information about that contour to extract the object from the original full size image.
I can really only think of two ways to do this, but I have no idea which of these actually make sense in opencv:
- Resize the contour. Then draw it on the original image.
- (The one I'm using, without success...) Use the contour to create a mask. Resize the mask. Then add the mask to the original image.
I'm using No. 2, but I think there is a problem with the mask. Resized, it no longer contains the contour.
Here are the important parts of my current code:
image = cv2.imread(path)
orig = image.copy()
...
#resize the image
image = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)
....
#convert to gray scale
...
#get contour
(cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
...
screenCnt = cv2.approxPolyDP(c, 0.01 * peri, True) #<--the contour
...
#create the mask
mask = np.ones(image.shape,np.uint8)*255
cv2.drawContours(mask,[screenCnt],0,(0,0,0),-1)
cv2.imshow("Small mask",mask) #<--- looks good
print(mask.shape) #<---returns a 3 element tuple
cv2.waitKey(0)
#now resize the mask
mask_big = cv2.resize(mask,(0,0),fx=ratio,fy=300)
cv2.imshow("Big mask",mask_big) #<--- ends up big, but all white (no contour)
cv2.waitKey(0)
I've been scouring the internet without luck, but I think I'm missing something fundamental here. Thanks a million to all answerers!
findContours
found n contours, the contour list is of length n. Each element in the list contains the coordinates of the contour. The simple way to see if this works is to iterate through each element of the contour object (from the downsampled image), multiplying each by the downsampling factor, appending these upsampled values to a new list, and then passing the upsampled contour object todrawContours
to overlay on your original image – Italy