問題描述
首先,對不起,如果這個話題已經存在(我認為這是一個常見的任務,但找不到任何東西).
關鍵是我有一張圖像,它顯示了不同顏色的不同點.我需要一個腳本來計算有多少個紅點、綠點和黃點.顏色是純紅色(ff0000)、綠色(00ff00)和黃色(ffff00).這使得這更容易,并且形狀定義明確.
我目前的方法是選擇圓形(點)形狀,選擇它們,然后一旦我將所有點都遠離背景圖像,讀取它的顏色來計算它們......
關鍵是我很迷茫.我知道這可以通過 OpenCV 完成,但不知道如何(也找不到任何好的教程).
有什么想法嗎?
這是一個基于OpenCV 3.2
和Python 2.7
的示例解決方案.
要計算彩色點,請對每種顏色類型重復以下 4 個步驟.
- 應用中值濾波器降低噪音 -
cv2.medianBlur()
. - 應用顏色閾值來分割彩色點 - 使用
cv2.inRange()
. - 使用
綠色 - 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
andPython 2.7
.To count the colored dots, repeat below 4 steps once per color type.
- Apply median filter to reduce noise -
cv2.medianBlur()
. - Apply color threshold to segment the colored dots - use
cv2.inRange()
. - Use Hough Circle Transform to detect the circles - use
circles = cv2.HoughCircles(mask,cv2.HOUGH_GRADIENT,...)
- 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模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持! - Apply median filter to reduce noise -