You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在检测到的特定颜色圆形区域内绘制适配边界的水平线?

问题:复现光学错觉图时圆形内部水平线绘制失败

我正在尝试复现一个光学错觉图,但在圆形内部添加水平线时遇到困难。

我的尝试步骤:

  • 检测圆形的特定颜色
  • 检测轮廓,提取圆心点和半径
  • 尝试绘制水平线(失败)

附上我的代码:

import numpy as np
import cv2

img = 255*np.ones((800, 800, 3), np.uint8)
height, width,_ = img.shape

#filling the image with lines
for i in range(0, height, 15):
    cv2.line(img, (0, i+3), (width, i+3), (255, 0, 0), 4)
    cv2.line(img, (0, i+8), (width, i+8), (0, 255, 0), 4)
    cv2.line(img, (0, i+13), (width, i+13), (0, 0, 255), 4)

#adding 5 gray circles
for i in range(0, height, int(height/5)):
        cv2.circle(img, (i+50, i+50), 75, (128, 128, 128), -1) 
#finding rannge of gray circles
lower=np.array([127,127,127])
upper=np.array([129,129,129])
mask = cv2.inRange(img, lower, upper)

#contours
contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
    #draw circles around the contours
    coordinates = cv2.minEnclosingCircle(cnt)
     #coordinates and radius:
    center = (int(coordinates[0][0]), int(coordinates[0][1]))
    radius = int(coordinates[1])
    #I wanted to do a sanity check before the for loop (I added a line the longest line should be 2*radius)
    cv2.line(img, (center[0]-radius, center[1]), (center[0]+radius, center[1]), (0, 0, 0), 4)
    
    for i in range(0, radius, int(radius/5)):
        cv2.line(img, (center[0]-radius+i, center[1]+i), (center[0]+radius-i, center[1]+i), (0, 0, 0), 4)
        cv2.line(img, (center[0]-radius+i, center[1]-i), (center[0]+radius-i, center[1]-i), (0, 0, 0), 4)
        

cv2.imwrite('munker.png',img)

当前结果:
当前绘制结果:圆形内线条过短,不符合边界比例

可以看到循环中的参数与圆形边界不成比例,导致绘制的线条过短(最长的线除外)。我还尝试过Hough变换,但遇到类似问题。

为了明确需求,我写了一段代码展示预期效果:

for i in range(0, 360, 15):
        x = int(center[0] + radius * np.cos(np.deg2rad(i)))
        y = int(center[1] + radius * np.sin(np.deg2rad(i)))
        cv2.line(img, (x,y), (x, y), (0, 255, 255), 10)

预期效果:
预期效果示意图:黄色点标记圆形边界的对应位置,需用水平线连接

我希望用水平线连接这些黄色点,但不知道如何实现,恳请帮忙解决。


内容的提问来源于stack exchange,提问作者AltunE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 05:20:48