如何使用OpenCV查找圆圈中的最大RGB值?

时间:2016-03-02 19:30:30

标签: python opencv image-processing

我的照片有9个不同颜色强度的水滴(即它们都是绿色,但不同的绿色)。目标是:

  • 识别9滴
  • 查找相关值(大小,位置,RGB值等)
  • 绘图数据

我正在使用SimpleBlobDetector来识别这些点。这将输出keypoints,其中包含有关每个blob的相关信息。

但是,我不知道如何访问特定blob的RGB(或HSV)值。如何仅搜索blob中的像素以确定最小/最大/平均颜色值?

非常感谢任何建议!

这是我的完整代码。它只打印每个blob的x_positiony_positionarea。我还附上了我正在使用的文件:

# Standard imports
import cv2
import numpy as np
from matplotlib import pyplot as plt

# Read image
filename= "C:\Users\Kevin\Pictures\Far 3.jpg"
img = cv2.imread(filename, 0)
img_color = cv2.imread(filename, cv2.IMREAD_ANYCOLOR)
img_c = cv2.resize(img_color,(800,600))
img1 = cv2.resize(img,(800,600))
ret,im = cv2.threshold(img1,120,255,cv2.THRESH_BINARY)

#######################################################
#######################################################

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

# Change thresholds
params.minThreshold = 50
params.maxThreshold = 150

# Filter by Area.
params.filterByArea = True
params.minArea = 150
params.maxArea = 400

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

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

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

detector = cv2.SimpleBlobDetector_create(params)

#######################################################
#######################################################

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

# 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(img_c, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# Show keypoints
cv2.imshow("Keypoints", im_with_keypoints)

x=[]
y=[]
area=[]

for i in xrange(9):
    xx = keypoints[i].pt[0]
    yy = keypoints[i].pt[1]
    aarea = keypoints[i].size
    print "PT.%f -- " %i, "x = %f," %xx, "y = %f," %yy,"area = %f," %aarea, "\n"

#######################################################
#######################################################
cv2.waitKey(0)

1 个答案:

答案 0 :(得分:0)

对于img中的每个像素,B,G,R值可以读作:

B=img[xx,yy,0]
G=img[xx,yy,1]
R=img[xx,yy,2]

您可以获取blob中所有像素的平均B,G,R值,然后在blob中找到最大值