计算图像中的彩色点

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

问题描述

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

关键是我有一张图像,它显示了不同颜色的不同点.我需要一个脚本来计算有多少个红点、绿点和黄点.颜色是纯红色(ff0000)、绿色(00ff00)和黄色(ffff00).这使得这更容易,并且形状定义明确.

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

关键是我很迷茫.我知道这可以通过 OpenCV 完成,但不知道如何(也找不到任何好的教程).

有什么想法吗?

解决方案

这是一个基于OpenCV 3.2Python 2.7的示例解决方案.

要计算彩色点,请对每种颜色类型重复以下 4 个步骤.

  1. 应用中值滤波器降低噪音 - cv2.medianBlur().
  2. 应用颜色阈值来分割彩色点 - 使用 cv2.inRange().
  3. 使用

    绿色 - 39 点

    黄色 - 30 点

    请注意,未检测到右侧最后一个小于半圆的黄点.这可能是霍夫圆变换 cv2.HoughCircles() 的限制.因此,如果发生此类问题,您需要决定如何处理.

    这里是示例代码:

    导入 cv2导入 numpyred = [(0,0,240),(10,10,255)] # 上下绿色 = [(0,240,0),(10,255,10)]黄色 = [(0,240,250),(10,255,255)]dot_colors = [红、绿、黄]img = cv2.imread('./imagesStackoverflow/count_colored_dots.jpg')# 在阈值处理之前应用中值模糊来平滑图像blur= cv2.medianBlur(img, 7) # 以 7x7 像素平滑图像,可能需要稍微调整一下对于 dot_colors 中的下、上:输出 = img.copy()# 将阈值颜色应用于白色 (255,255, 255),其余应用于黑色 (0,0,0)掩码 = cv2.inRange(模糊,下,上)circles = cv2.HoughCircles(mask,cv2.HOUGH_GRADIENT,1,20,param1=20,param2=8,最小半径=0,最大半径=60)索引 = 0如果 circles 不是 None:# 将圆的 (x, y) 坐标和半径转换为整数circles = numpy.round(circles[0, :]).astype("int")# 循环 (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)索引 = 索引 + 1#print str(index) + ":"+ str(r) + ", (x,y) = "+ str(x) + ', ' + str(y)打印'没有.检测到的圆圈数 = {}'.format(index)

    希望对您有所帮助.

    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 = numpy.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.

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

上一篇:Python OpenCV:魔方解算器颜色提取 下一篇:向图像添加填充以使它们具有相同的形状

相关文章

最新文章