Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize a contour in opencv 2.4.11 python? (Goal: Object extraction)

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:

  1. Resize the contour. Then draw it on the original image.
  2. (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!

like image 401
lakhesis Avatar asked Jul 27 '15 04:07

lakhesis


1 Answers

As I see it is quite old question. However, I had the same one and was't able to find quick answer with sample of code. Here is the example (for opencv 3.4)

_, contours, _ = cv2.findContours(mask.astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

coef_y = img_orig.shape[0] / img_resized.shape[0]
coef_x = img_orig.shape[1] / img_resized.shape[1]

for contour in contours:
    contour[:, :, 0] = contour[:, :, 0] * coef_x
    contour[:, :, 1] = contour[:, :,  1] * coef_y

    cv2.drawContours(img_orig, contour, -1, (0, 255, 0), 2)
like image 117
MykolaSharhan Avatar answered Oct 06 '22 17:10

MykolaSharhan