我正在尝试找到基于许多输入变量生成一系列图像的最有效方法。输出应该是一系列“区域”,通过“通道”连接到中间的中心区域。我希望能够输入我想要使用的区域数量,并自动创建图像,区域和通道在图像中间隔适当。
我曾尝试使用opencv库在python中执行此操作:虽然我有它工作但它只是一个愚蠢的暴力方法。任何人都可以提供一些关于如何自动化此代码的想法,以便仅从输入变量生成图像?在循环中创建区域和通道时,基本上在#个区域上循环?
以下是我用于创建上面显示的3区版本的代码:
img = np.zeros((500,500),np.uint8)
img = cv2.bitwise_not(img)
img = cv.fromarray(img)
channel_width = 10
channel_length = 75
zones = 3
zone_width = 40
zone_thickness = 4
channel_thickness = 4
h = img.rows; w = img.cols;#,w,z = img.shape
# center
Xc = h/2
Xc0 = int(round(Xc - zone_width/2))
Yc = w/2
Yc0 = int(round(Yc - zone_width/2))
cv.Rectangle(img,(Xc0,Yc0), (Xc0+zone_width,Yc0+zone_width),
cv.RGB(0,0,0),zone_thickness,8)
#zones to left
x0 = int(round(Xc-1.5*zone_width-channel_length))
y0 =int(round(Yc-zone_width/2))
x1 =int(round(Xc -zone_width/2 - channel_length))
y1 = int(round(Yc + zone_width/2))
cv.Rectangle(img,(x0,y0),(x1,y1),cv.RGB(0,0,0),zone_thickness,8)
#left channel
cx0 = int(round(Xc0)) #int(round(Xc0-1.5*zone_width-channel_length))
cy0 = int(round(Yc0 + zone_width/2 - channel_width/2))
cx1 = int(round(Xc0 - channel_length))
cy1 = int(round(Yc0 + zone_width/2 + channel_width/2))
cv.Rectangle(img,(cx0,cy0),(cx1,cy1),cv.RGB(0,0,0),channel_thickness,8)
cv.Rectangle(img,(cx0+2*zone_thickness,cy0),(cx1-2*zone_thickness,cy1),
cv.RGB(255,255,255),-1,8)
#zones to top
x0 = int(round(Xc-.5*zone_width))
y0 =int(round(Yc-1.5*zone_width - channel_length))
x1 =int(round(Xc + zone_width/2 ))
y1 = int(round(Yc - zone_width/2 - channel_length))
cv.Rectangle(img,(x0,y0),(x1,y1),cv.RGB(0,0,0),zone_thickness,8)
#top channel
cx0 = int(round(Xc0 + zone_width/2 - channel_width/2))
#int(round(Xc0-1.5*zone_width-channel_length))
cy0 = int(round(Yc0 ))
cx1 = int(round(Xc0 + zone_width/2 + channel_width/2))
cy1 = int(round(Yc0 -channel_length))
cv.Rectangle(img,(cx0,cy0),(cx1,cy1),cv.RGB(0,0,0),channel_thickness,8)
cv.Rectangle(img,(cx0,cy0+2*zone_thickness),(cx1,cy1-2*zone_thickness),
cv.RGB(255,255,255),-1,8)
#zones to right
x0 = int(round(Xc+1.5*zone_width+channel_length))
y0 =int(round(Yc-zone_width/2))
x1 =int(round(Xc +zone_width/2 + channel_length))
y1 = int(round(Yc + zone_width/2))
cv.Rectangle(img,(x0,y0),(x1,y1),cv.RGB(0,0,0),zone_thickness,8)
#right channel
cx0 = int(round(Xc0+zone_width)) #int(round(Xc0-1.5*zone_width-channel_length))
cy0 = int(round(Yc0 + zone_width/2 - channel_width/2))
cx1 = int(round(Xc0 +zone_width + channel_length))
cy1 = int(round(Yc0 + zone_width/2 + channel_width/2))
cv.Rectangle(img,(cx0,cy0),(cx1,cy1),cv.RGB(0,0,0),channel_thickness,8)
cv.Rectangle(img,(cx0-2*zone_thickness,cy0),(cx1+2*zone_thickness,cy1),
cv.RGB(255,255,255),-1,8)