【发布时间】:2021-03-11 10:37:11
【问题描述】:
我试图在图像的特定区域中找到轮廓。是否可以只显示 ROI 内的轮廓而不是图像其余部分的轮廓?我在另一篇类似的帖子中读到我应该使用口罩,但我认为我没有正确使用它。我是 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)
【问题讨论】: