简体   繁体   English

如何使用 opencv 和 Python 在 ROI 内找到轮廓?

[英]How can I find contours inside ROI using opencv and Python?

Im trying to find contours in a specific area of the image.我试图在图像的特定区域找到轮廓。 Is it possible to just show the contours inside the ROI and not the contours in the rest of the image?是否可以只显示 ROI 内的轮廓而不显示图像其余部分的轮廓? I read in another similar post that I should use a mask, but I dont think I used it correctly.我在另一篇类似的帖子中读到我应该使用口罩,但我认为我没有正确使用它。 Im new to openCV and Python, so any help is much appriciated.我是 openCV 和 Python 的新手,所以任何帮助都非常有用。

import numpy as np
import cv2

cap = cv2.VideoCapture('size4.avi')
x, y, w, h= 150, 50, 400 ,350
roi = (x, y, w, h)

while(True): 
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    _, thresh = cv2.threshold(gray, 127, 255, 0)
    im2, contours, hierarchy = cv2.findContours(thresh,    cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

    roi = cv2.rectangle(frame, (x,y), (x+w, y+h), (0,0,255), 2)
    mask = np.zeros(roi.shape,np.uint8)
    cv2.drawContours(mask, contours, -1, (0,255,0), 3)

    cv2.imshow('img', frame)

Since you claim to be a novice, I have worked out a solution along with an illustration.由于你自称是新手,所以我已经制定了一个解决方案并附有插图。

Consider the following to be your original image:将以下内容视为您的原始图像:

在此处输入图片说明

Assume that the following region in red is your region of interest (ROI), where you would like to find your contours:假设以下红色区域是您的感兴趣区域 (ROI),您希望在其中找到您的轮廓:

在此处输入图片说明

First, construct an image of black pixels of the same size.首先,构造一个相同大小的黑色像素的图像。 It MUST BE OF SAME size:它必须是相同的尺寸:

black = np.zeros((img.shape[0], img.shape[1], 3), np.uint8) #---black in RGB

在此处输入图片说明

Now to form the mask and highlight the ROI:现在形成遮罩并突出显示投资回报率:

black1 = cv2.rectangle(black,(185,13),(407,224),(255, 255, 255), -1)   #---the dimension of the ROI
gray = cv2.cvtColor(black,cv2.COLOR_BGR2GRAY)               #---converting to gray
ret,b_mask = cv2.threshold(gray,127,255, 0)                 #---converting to binary image

在此处输入图片说明

Now mask the image above with your original image:现在用您的原始图像掩盖上面的图像:

fin = cv2.bitwise_and(th,th,mask = mask)

在此处输入图片说明

Now use cv2.findContours() to find contours in the image above.现在使用cv2.findContours()查找cv2.findContours()轮廓。

Then use cv2.drawContours() to draw contours on the original image.然后使用cv2.drawContours()在原始图像上绘制轮廓。 You will finally obtain the following:您最终将获得以下内容:

在此处输入图片说明

There might be better methods as well, but this was done so as to get you aware of the bitwise AND operation availabe in OpenCV which is exclusively used for masking也可能有更好的方法,但这样做是为了让您了解 OpenCV 中专门用于屏蔽按位 AND操作

For setting a ROI in Python, one uses standard NumPy indexing such as in this example .为了在 Python 中设置 ROI,可以使用标准 NumPy 索引, 例如在本例中

So, to select the right ROI, you don't use the cv2.rectangle function (that is for drawing a rectangle), but you do this instead:因此,要选择正确的 ROI,您不使用 cv2.rectangle 函数(用于绘制矩形),而是执行以下操作:

 _, thresh = cv2.threshold(gray, 127, 255, 0)
 roi = thresh[x:(x+w), y:(y+h)]
 im2, contours, hierarchy = cv2.findContours(roi, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM