OPENCV摄像机边界被视为对象,仅在跨路径时检测到对象
我试图用相机检测对象。但是,只有外边界变成绿色,并且一旦将物体切开外边框,就会检测到它们。
https://i.sstatic.net/9tiqg.jpg 当它切开轮廓时,但不会检测到矩形。
如您所知,只有边界是绿色的,一旦对象将边框切割成一个物体。如果我要在屏幕中间放置其他东西,而这些东西不触及大纲边界,则不被视为对象。
我的代码:
import math
import numpy as np
import cv2 as cv
from threading import *
class robotVision(Thread):
def run(self):
self.cap = cv.VideoCapture(0)
if not self.cap.isOpened():
print("Cannot open camera")
exit()
while True:
# Capture frame-by-frame
self.ret, self.frame = self.cap.read()
if not self.ret:
print("Can't receive frame (stream end?). Exiting ...")
break
self.detectCookie()
self.imshow()
if cv.waitKey(1) == ord('q'):
self.releaseStream()
break
def detectCookie(self):
# turn scene gray and put a threshold on noise
self.gray = cv.cvtColor(self.frame, cv.COLOR_BGR2GRAY)
self.blur = cv.GaussianBlur(self.gray,(39, 39), 0)
_, self.thresh = cv.threshold(self.blur, 75, 255, 0, cv.THRESH_BINARY)
self.dilated = cv.dilate(self.thresh, (7, 7), iterations = 3)
self.findContours()
for cnt in self.contours:
area = cv.contourArea(cnt)
perimeter = cv.arcLength(cnt, True)
if (perimeter != 0 and area != 0):
#the rounding -> how round it is
formFactor = 4 * math.pi * area / perimeter**2
if (area > 100):
self.drawContours(cnt)
def releaseStream(self):
# When everything done, release the capture
self.cap.release()
cv.destroyAllWindows()
def imshow(self):
cv.imshow("Gray capture (First cycle)", self.gray)
cv.imshow("Blur capture (Second cycle)", self.blur)
cv.imshow("Thresh capture (Third cycle)", self.thresh)
cv.imshow("Dilated capture (Fourth cycle)", self.dilated)
cv.imshow("Video capture (Final result)", self.frame)
def findContours(self):
self.contours, self.hierarchy = cv.findContours(self.dilated, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_NONE)
def drawContours(self, target = "default"):
if (target == "default"):
cv.drawContours(self.frame, self.contours, -1, (0,255,0), 3)
elif (isinstance(target, list)):
cv.drawContours(self.frame, target, -1, (0,255,0), 3)
else:
cv.drawContours(self.frame, [target], -1, (0,255,0), 3)
我的问题:
我如何删除外线并检测对象而无需对象切开外线以被视为对象。
I am trying to detect objects with my camera. However only the outer border becomes green and objects are detected once they cut the outer border.
https://i.sstatic.net/9TiQG.jpg
You can see how it detects images when it cuts through the outlines, but it does not detect the rectangle.
as you can tell only the borders are green and once an object cuts the border it becomes an object. If I were to place something else in the middle of the screen which doesn't touch the outline borders it is not considered an object.
My code:
import math
import numpy as np
import cv2 as cv
from threading import *
class robotVision(Thread):
def run(self):
self.cap = cv.VideoCapture(0)
if not self.cap.isOpened():
print("Cannot open camera")
exit()
while True:
# Capture frame-by-frame
self.ret, self.frame = self.cap.read()
if not self.ret:
print("Can't receive frame (stream end?). Exiting ...")
break
self.detectCookie()
self.imshow()
if cv.waitKey(1) == ord('q'):
self.releaseStream()
break
def detectCookie(self):
# turn scene gray and put a threshold on noise
self.gray = cv.cvtColor(self.frame, cv.COLOR_BGR2GRAY)
self.blur = cv.GaussianBlur(self.gray,(39, 39), 0)
_, self.thresh = cv.threshold(self.blur, 75, 255, 0, cv.THRESH_BINARY)
self.dilated = cv.dilate(self.thresh, (7, 7), iterations = 3)
self.findContours()
for cnt in self.contours:
area = cv.contourArea(cnt)
perimeter = cv.arcLength(cnt, True)
if (perimeter != 0 and area != 0):
#the rounding -> how round it is
formFactor = 4 * math.pi * area / perimeter**2
if (area > 100):
self.drawContours(cnt)
def releaseStream(self):
# When everything done, release the capture
self.cap.release()
cv.destroyAllWindows()
def imshow(self):
cv.imshow("Gray capture (First cycle)", self.gray)
cv.imshow("Blur capture (Second cycle)", self.blur)
cv.imshow("Thresh capture (Third cycle)", self.thresh)
cv.imshow("Dilated capture (Fourth cycle)", self.dilated)
cv.imshow("Video capture (Final result)", self.frame)
def findContours(self):
self.contours, self.hierarchy = cv.findContours(self.dilated, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_NONE)
def drawContours(self, target = "default"):
if (target == "default"):
cv.drawContours(self.frame, self.contours, -1, (0,255,0), 3)
elif (isinstance(target, list)):
cv.drawContours(self.frame, target, -1, (0,255,0), 3)
else:
cv.drawContours(self.frame, [target], -1, (0,255,0), 3)
My question:
How can I remove the outer lines and detect objects without having the objects have to cut the outer lines to be considered an object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论