繁体   English   中英

如何使用 OpenCV Python 在圆形对象周围绘制轮廓并找到其 ROI?

[英]How to draw Contours around the circular object and find its ROI using OpenCV Python?

我试图在图像中存在的圆形对象上绘制轮廓并找到其面积和质心。 但是我没能做到,因为如图所示,整个图像都绘制了轮廓。 我只想在圆形白色对象上绘制轮廓,如图所示。

在此处输入图片说明

预期结果:

我只想在图像中显示的这个白色物体上绘制圆形轮廓并显示其质心和面积。 OpenCV,忽略其余部分。

下面附上代码。

代码:

import cv2
import numpy as np
import imutils

cap = cv2.VideoCapture(0)

cap.set(3,640)
cap.set(4,480)

while True:
    _,frame = cap.read()
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    lower_blue = np.array([90,60,0])
    upper_blue = np.array([121,255,255])

    mask = cv2.inRange(hsv, lower_blue, upper_blue)
    cnts = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    cnts= imutils.grab_contours(cnts)

    for c in cnts:
        area = cv2.contourArea(c)
        if area > 1500:
            cv2.drawContours(frame, [c], -1, (0,255,0), 3)
            M = cv2.moments(c)
            cx = int(M["m10"] / M["m00"])
            cy = int(M["m01"] / M["m00"])

            cv2.circle(frame, (cx, cy), 7, (255, 255, 255), -1)
            cv2.putText(frame, "Centre", (cx - 20, cy - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)

            cv2.imshow("frame", frame)


            print("area is ...", area)
            print("centroid is at ...", cx, cy)

    k=cv2.waitKey(1000)
    if k ==27:
        break
cap.release()
cv2.destroyAllWindows()

任何帮助,将不胜感激。 提前致谢。

这可以根据需要以多种方式完成。 一种简单的方法可以是:

首先,过滤投资回报率。 所以我们知道 3 件事,ROI 是白色的,是一个圆圈,我们知道它的大约面积。

对于白色:

def detect_white(img):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    _, thresh = cv2.threshold(gray, 220, 255, cv2.THRESH_BINARY)
    return thresh

尽管您需要根据需要使用白色检测算法。 一些白色检测的好方法是使用阈值(如上),使用 HSL 色彩空间,因为白色度密切依赖于亮度( cv2.cvtColor(img, cv2.COLOR_BGR2HLS ) 等。


所以在下面的代码中,首先我们通过白色过滤出 ROI,然后通过这些白色轮廓的形状和面积。

image = cv2.imread("path/to/your/image")

white_only = detect_white(image)

contours, hierarchy = cv2.findContours(white_only, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

circles = []

for contour in contours:
    epsilon = 0.01 * cv2.arcLength(contour, True)
    approx = cv2.approxPolyDP(contour, epsilon, True)
    area = cv2.contourArea(contour)
    if len(approx) > 12 and area > 1000 and area < 3000:
        circles.append(contour)

# Now the list circles would have all such
# contours satisfying the above conditions
# If len(circles) != 1 or isn't the circle you
# desire, more filtering is required

cv2.drawContours(image, circles, -1, (0, 255, 0), 3)

选择轮廓依赖于面积和顶点(由cv2. approxPolyDP()返回)。

就像你的图片一样,白色的大圆圈面积很大,非常接近一个圆圈,我检查了一下: len(approx) > 12 and area > 1000 and area < 3000: 根据您的情况调整这条线,并告诉我它是否解决了您的问题。 如果没有,我们可以讨论一些更好的方法或使用这个方法使其更准确。

暂无
暂无

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

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