# 计算cv2.circle内部的白色像素

##### 问题描述投票：0回答：2

1. 找到图像的最亮部分，然后使用cv2.circle在其上画一个圆圈-Done
2. 计算cv2.circle内部图像上的白色部分-需要帮助

``````gray = cv2.GaussianBlur(gray, (371, 371), 0)
(minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray)
image = orig.copy()
cv2.circle(image, maxLoc, 371, (255, 0, 0), 2)

sought = [254,254,254]
amount = 0

for x in range(image.shape[0]):
for y in range(image.shape[1]):
b, g, r = image[x, y]
if (b, g, r) == sought:
amount += 1

print(amount)

image = imutils.resize(image, width=400)

# display the results of our newly improved method
cv2.imshow("Optic Image", image)
cv2.waitKey(0)
``````

[我现在要做的是确定cv2.circle内图像白色区域的大小。

python opencv
##### 2个回答
1

``````import cv2
import numpy as np

# convert to HSV and extract saturation channel
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

# threshold
thresh = cv2.threshold(gray, 175, 255, cv2.THRESH_BINARY)[1]

# count number of white pixels
count = np.sum(np.where(thresh == 255))
print("count =",count)

# write result to disk
cv2.imwrite("optic_thresh.png", thresh)

# display it
cv2.imshow("IMAGE", img)
cv2.imshow("THRESH", thresh)
cv2.waitKey(0)
``````

``````count = 1025729
``````

0

• 读取输入
• 将输入转换为0至1的范围作为一维数据
• 使用kmeans聚类减少色彩数量并将其转换回2图像的范围是0到255
• 使用inRange颜色阈值分离“黄色”区域
• 用形态清理并获得轮廓
• 获得最小的封闭圆心和半径并稍微偏心
• 在输入上绘制未填充的白色圆圈
• 在黑色背景上绘制白色填充圆作为黄色区域的圆形蒙版
• 将输入转换为灰度
• 阈值灰度图像
• 将蒙版应用于阈值灰度图像
• 计算白色像素数

``````import cv2
import numpy as np
from sklearn import cluster

h, w, c = img.shape

# convert to range 0 to 1
image = img.copy()/255

# reshape to 1D array
image_1d = image.reshape(h*w, c)

# do kmeans processing
kmeans_cluster = cluster.KMeans(n_clusters=int(5))
kmeans_cluster.fit(image_1d)
cluster_centers = kmeans_cluster.cluster_centers_
cluster_labels = kmeans_cluster.labels_

# need to scale result back to range 0-255
newimage = cluster_centers[cluster_labels].reshape(h, w, c)*255.0
newimage = newimage.astype('uint8')

# threshold brightest region
lowcolor = (150,180,230)
highcolor = (170,200,250)
thresh1 = cv2.inRange(newimage, lowcolor, highcolor)

# apply morphology open and close
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7,7))
thresh1 = cv2.morphologyEx(thresh1, cv2.MORPH_OPEN, kernel, iterations=1)
thresh1 = cv2.morphologyEx(thresh1, cv2.MORPH_CLOSE, kernel, iterations=1)

# get contour
cntrs = cv2.findContours(thresh1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cntrs = cntrs[0] if len(cntrs) == 2 else cntrs[1]
c = cntrs[0]

# get enclosing circle and bias center, if desired, since it is slightly offset (or alternately, increase the radius)
bias = 5
cx = int(round(center[0]))-bias
cy = int(round(center[1]))+bias

# draw filled circle over black and also outline circle over input
cv2.circle(mask, (cx,cy), rr, (255, 255, 255), -1)
circle = img.copy()
cv2.circle(circle, (cx,cy), rr, (255, 255, 255), 1)

# convert to gray
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

# threshold gray image
thresh2 = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)[1]

# count number of white pixels
count = np.sum(np.where(thresh2 == 255))
print("count =",count)

# write result to disk
#cv2.imwrite("optic_thresh.png", thresh)
cv2.imwrite("optic_kmeans.png", newimage)
cv2.imwrite("optic_thresh1.png", thresh1)
cv2.imwrite("optic_circle.png", circle)
cv2.imwrite("optic_thresh2.png", thresh2)

# display it
cv2.imshow("IMAGE", img)
cv2.imshow("KMEANS", newimage)
cv2.imshow("THRESH1", thresh1)
cv2.imshow("CIRCLE", circle)
cv2.imshow("GRAY", gray)
cv2.imshow("THRESH2", thresh2)
cv2.waitKey(0)
``````

kmeans图片：

inRange阈值图像：

``````count = 443239
``````