计算图像中的彩色圆点 [英] count colored dots in image

查看:205
本文介绍了计算图像中的彩色圆点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,对不起,如果这个话题已经存在(我认为这是一个常见的任务,但找不到任何东西)。



重点是,我有一个图像显示不同颜色的不同点。我需要一个脚本来计算有多少红色,绿色和黄色点。
颜色为纯红色(ff0000),绿色(00ff00)和黄色(ffff00)。这使得这更容易,形状也很明确。





我目前的做法是选择圆形(点)形状,选择它们,然后一旦我将所有点都远离背景图像,读取它的颜色来计算它们......



重点是我迷失了。我知道这可以通过OpenCV完成,但不知道如何(并且找不到任何好的教程)。



任何想法?

解决方案

以下是基于 OpenCV 3.2 Python 2.7的示例解决方案



要计算彩色圆点,请在每种颜色类型下重复4次以下步骤。


  1. 应用中值滤波器以降低噪音 - cv2.medianBlur()

  2. 应用颜色阈值来分割彩色圆点 - 使用 cv2.inRange()

  3. 使用



    绿色 - 39点



    黄色 - 30点



    请注意最后的黄点没有检测到右侧小于半圈。这可能是霍夫圆变换 cv2.HoughCircles()的限制。所以你需要决定如何处理这类问题。



    以下是示例代码:



      import cv2 
    import numpy

    red = [(0,0,240),(10,10,255)] #low and upper
    绿色= [(0,240,0),(10,255,10)]
    黄色= [(0,240,250),(10,255,255)]
    dot_colors = [红色,绿色,黄色]

    img = cv2.imread('./ imagesStackoverflow / count_colored_dots.jpg')
    #应用medianBlur来平滑图像之前的阈值
    blur = cv2.medianBlur(img,7)#7x7像素的平滑图像,可能需要调整一点

    为更低,更高的dot_colors:
    output = img.copy()
    #将threshhold颜色应用于白色(255,255,255),其余to black(0,0,0)
    mask = cv2.inRange(blur,lower,upper)

    circles = cv2.HoughCircles(mask,cv2.HOUGH_GRADIENT,1,20,param1 = 20,param2 = 8,
    minRadius = 0,maxRadius = 60)
    index = 0
    如果circle不是None:
    #将(x,y)坐标和圆的半径转换为整数
    circles = np.round(circles [0,:])。astype(int)

    #loop在(x,y)坐标和圆的半径上
    为(x,y,r)在圆圈中:
    #在输出图像中绘制圆圈,
    #然后绘制一个矩形对应圆圈的中心
    cv2.circle(输出,(x,y),r,(255,0,255),2)
    cv2.rectangle(输出,(x - 5, y - 5),(x + 5,y + 5),(255,0,255), - 1)

    index = index + 1
    #print str(index)+ :+ str(r)+,(x,y)=+ str(x)+','+ str(y)
    打印'否检测到的圈子= {}'。格式(索引)

    希望此帮助。


    First of all, sorry if this topic already exists (I think this is a common task, but couldn't find anything).

    The point, is that I have an image who shows different dots of different colors. And I need an script to count how many red, green and yellow dots are. The colors are pure red(ff0000), green(00ff00) and yellow(ffff00). Which makes this easier, and the shape is well defined.

    My current approach is to select the round(dot) shape, select them and then once I have all dots away from background image, read its color to count them...

    The point is that I'm so lost with this. I know that this can be done with OpenCV but don't know how (and couldn't find any nice tutorial).

    Any idea?

    解决方案

    Here is a sample solution based on OpenCV 3.2 and Python 2.7.

    To count the colored dots, repeat below 4 steps once per color type.

    1. Apply median filter to reduce noise - cv2.medianBlur().
    2. Apply color threshold to segment the colored dots - use cv2.inRange().
    3. Use Hough Circle Transform to detect the circles - use circles = cv2.HoughCircles(mask,cv2.HOUGH_GRADIENT,...)
    4. Loop through each detected circles to draw its center and a circle around it, and count the numbers of colored dots.

    Sample images of dots detected:

    Red - 10 dots

    Green - 39 dots

    Yellow - 30 dots

    Take note that the last yellow dots at the right side with less than half a circle hasn't been detected. This is likely a limitation of the Hough Circle Transform cv2.HoughCircles(). So you need to decide how to handle this type of issue if it happens.

    Here is the sample code:

    import cv2
    import numpy
    
    red = [(0,0,240),(10,10,255)] # lower and upper 
    green = [(0,240,0),(10,255,10)]
    yellow = [(0,240,250),(10,255,255)]
    dot_colors = [red, green, yellow]
    
    img = cv2.imread('./imagesStackoverflow/count_colored_dots.jpg')   
    # apply medianBlur to smooth image before threshholding
    blur= cv2.medianBlur(img, 7) # smooth image by 7x7 pixels, may need to adjust a bit
    
    for lower, upper in dot_colors:
        output = img.copy()
        # apply threshhold color to white (255,255, 255) and the rest to black(0,0,0)
        mask = cv2.inRange(blur,lower,upper) 
    
        circles = cv2.HoughCircles(mask,cv2.HOUGH_GRADIENT,1,20,param1=20,param2=8,
                                   minRadius=0,maxRadius=60)    
        index = 0
        if circles is not None:
            # convert the (x, y) coordinates and radius of the circles to integers
            circles = np.round(circles[0, :]).astype("int")
    
            # loop over the (x, y) coordinates and radius of the circles
            for (x, y, r) in circles:
                # draw the circle in the output image, 
                #   then draw a rectangle corresponding to the center of the circle
                cv2.circle(output, (x, y), r, (255, 0, 255), 2)
                cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (255, 0, 255), -1)
    
                index = index + 1
                #print str(index) + " : " + str(r) + ", (x,y) = " + str(x) + ', ' + str(y)
            print 'No. of circles detected = {}'.format(index)
    

    Hope this help.

    这篇关于计算图像中的彩色圆点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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