python opencv轮廓提取,如何在opencv 2.4.11 python中调整轮廓的大小? (目标:对象提取)...

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) #

...

#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) #

print(mask.shape) #

cv2.waitKey(0)

#now resize the mask

mask_big = cv2.resize(mask,(0,0),fx=ratio,fy=300)

cv2.imshow("Big mask",mask_big) #

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!

解决方案

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值