OpenCV像素为所有sam着色

2024-04-23 10:15:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要第二只眼睛看我的代码,它能得到圆圈的中心颜色。由于一些不为人知的原因,对于每个中心,它返回的值几乎相同,这是远远不正确的。圆的结果:

中心颜色: [126 126 126] 点 x: 年:440502年

中心色 [124 124 124 124] 点 x: 502年:516年

中心色 [133 133 133] 点 x: 502年:596年

中心色 [116 116 116] 点 x: 504年:306

中心色 [119 119 119] 点 x: 504年:366年

输入图像如下所示。显然应该有非常不同的值,因为黑圈的平均RBG值应该远低于100范围。在

input image

下图显示代码正确地找到了圆和圆的中心(用绿色标记),只是没有找到正确的中心颜色值

output

代码如下:

import cv2
import numpy as np
from math import sqrt

# Open
img = cv2.imread('test1.jpg',0)

# Process
img = cv2.medianBlur(img,5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

# Find Interest
circles = cv2.HoughCircles(img,cv2.cv.CV_HOUGH_GRADIENT,1,20,
                                param1=50,param2=30,minRadius=1,maxRadius=20)
circles = np.uint16(np.around(circles))

# Post Process
for i in circles[0,:]:

  # draw the outer circle
  cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
  # draw the center of the circle
  cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)

  print "Center Colour"
  print cimg[i[0], i[1]]
  print "Point"
  print "x: "+str(i[0]) + " y: " + str(i[1])

cv2.imwrite('output.png',cimg)

Tags: the代码importimg颜色np中心cv2
1条回答
网友
1楼 · 发布于 2024-04-23 10:15:39

根据我的理解,你应该得到内圈的颜色,因为你是在访问颜色值之前绘制的。但由于返回的值不是0,0,255,所以坐标必须交换。在

原始打印为:

print "Center Colour (orig)", cimg[i[0], i[1]]

交换打印:

^{pr2}$

绘图后输出:

Center Colour (orig) [126 126 126]
Center Colour (orig) [116 116 116]
...
Center Colour (swapped) [  0   0 255]
Center Colour (swapped) [  0   0 255]
...

如果现在在绘制内圈之前使用交换打印,则输出将如下所示:

Center Colour (swapped) [128 128 128]
Center Colour (swapped) [27 27 27]
...

这就是你要找的吗?在

相关问题 更多 >