检测不同颜色的blob opencv [英] Detect different color blob opencv

查看:124
本文介绍了检测不同颜色的blob opencv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是opencv的新手,对于一个学校项目,我需要用相机检测一个红色和绿色的圆圈,所以我使用了blobdetection,但是它可以检测到我的两种颜色,我认为我的口罩很差,每种颜色都链接到特定的动作.

I'm new to opencv and for a school project i need to detect a red and a green circle with a camera, so i've use blobdetection, but it detect me the two colors, i think that my mask is bad, each color is linked to a specific action.

此刻,我的代码在同一页面上检测到了很好的红色和绿色圆圈,但我希望它在白色页面上仅检测到红色圆圈.

At the moment my code detect well red and green circle on the same page but i want it to detect only red circle on a white page.

感谢您的帮助

# Standard imports
import cv2
import numpy as np;

    # Read image
im = cv2.VideoCapture(0)

# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()

# Change thresholds
params.minThreshold = 100;
params.maxThreshold = 200;

# Filter by Area.
params.filterByArea = True
params.minArea = 200
params.maxArea = 20000

# Filter by Circularity
params.filterByCircularity = True
params.minCircularity = 0.1

# Filter by Convexity
params.filterByConvexity = True
params.minConvexity = 0.1

# Filter by Inertia
params.filterByInertia = True
params.minInertiaRatio = 0.1


blueLower = (0,85,170)  #100,130,50
blueUpper = (140,110,255) #200,200,130


while(1):

    ret, frame=im.read()

    mask = cv2.inRange(frame, blueLower, blueUpper)
    mask = cv2.erode(mask, None, iterations=0)
    mask = cv2.dilate(mask, None, iterations=0)
    frame = cv2.bitwise_and(frame,frame,mask = mask)

# Set up the detector with default parameters.
    detector = cv2.SimpleBlobDetector_create(params)

# Detect blobs.
    keypoints = detector.detect(mask)

# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle corresponds to the size of blob
    im_with_keypoints = cv2.drawKeypoints(mask, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)


# Display the resulting frame

    frame = cv2.bitwise_and(frame,im_with_keypoints,mask = mask)

    cv2.imshow('frame',frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
im.release()
cv2.destroyAllWindows()

代码更新

现在我遇到一个问题,就是我的整个圈子都没有被检测到.

Now i got a issue where my full circle isn't detected.

无斑点检测

# Standard imports
import cv2
import numpy as np;

# Read image
im = cv2.VideoCapture(0)

while(1):
        ret, frame=im.read()


        lower = (130,150,80)  #130,150,80
        upper = (250,250,120) #250,250,120
        mask = cv2.inRange(frame, lower, upper)
        lower, contours, upper = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
        blob = max(contours, key=lambda el: cv2.contourArea(el))
        M = cv2.moments(blob)
        center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
        canvas = im.copy()
        cv2.circle(canvas, center, 2, (0,0,255), -1)

        cv2.imshow('frame',frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
                break
im.release()
cv2.destroyAllWindows()

推荐答案

您需要计算出绿色的BGR编号是什么(假设参数[0, 255, 0]假设是这样),然后创建一个忽略外部任何颜色的遮罩果岭周围的公差:

You need to work out what the BGR numbers for your green are (let's say for arguments sake [0, 255, 0]), then create a mask that ignores any colours outside a tolerance around your green:

mask = cv2.inRange(image, lower, upper)

看看教程一步一步来.

上下左右玩耍以获得正确的行为.然后,您可以在遮罩中找到轮廓:

Play around with lower and upper to get the right behaviour. Then you can find the contours in the mask:

_, contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, 
                                                    cv2.CHAIN_APPROX_NONE)

然后在contours列表中查找最大的列表(过滤掉所有可能的噪音):

Then go through the contours list to find the biggest one (filter out any possible noise):

blob = max(contours, key=lambda el: cv2.contourArea(el))

这是您最后的斑点".您可以通过以下方法找到中心:

And that's your final 'blob'. You can find the center by doing:

M = cv2.moments(blob)
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

您可以将此中心绘制到图像的副本上,以进行检查:

You can draw this center onto a copy of your image, for checking:

canvas = im.copy()
cv2.circle(canvas, center, 2, (0,0,255), -1)

显然,这是假设图像中只有一个绿色的球,而没有其他绿色.但这是一个开始.

Obviously, this makes the assumption that there's only one green ball and nothing else green in the image. But it's a start.

编辑-响应第二个帖子

我认为以下应该可行.我还没有测试过,但是您应该至少可以使用显示的画布和蒙版进行更多的调试:

I think the following should work. I haven't tested it, but you should be able to at least do a bit more debugging with the canvas and mask displayed:

# Standard imports
import cv2
import numpy as np;

# Read image
cam = cv2.VideoCapture(0)

while(1):
        ret, frame = cam.read()

        if not ret:
            break

        canvas = frame.copy()


        lower = (130,150,80)  #130,150,80
        upper = (250,250,120) #250,250,120
        mask = cv2.inRange(frame, lower, upper)
        try:
            # NB: using _ as the variable name for two of the outputs, as they're not used
            _, contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
            blob = max(contours, key=lambda el: cv2.contourArea(el))
            M = cv2.moments(blob)
            center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

            cv2.circle(canvas, center, 2, (0,0,255), -1)

        except (ValueError, ZeroDivisionError):
            pass

        cv2.imshow('frame',frame)
        cv2.imshow('canvas',canvas)
        cv2.imshow('mask',mask)

        if cv2.waitKey(1) & 0xFF == ord('q'):
                break
im.release()
cv2.destroyAllWindows()

这篇关于检测不同颜色的blob opencv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆