0

我试图找到基于多个输入变量生成一系列图像的最有效方法。输出应该是通过“通道”连接到中间的中心区域的一系列“区域”。我希望能够输入我想要使用的区域数量,并自动创建图像,区域和通道在图像中适当间隔。

我曾尝试使用 opencv 库在 python 中执行此操作:尽管我可以使用它,但它只是一种愚蠢的蛮力方法。谁能提供一些关于如何自动化此代码以便仅从输入变量生成图像的想法?在循环内创建区域和通道时基本上循环了区域的数量?

显示 2 区和 3 区布局的图像

这是我用来创建上面显示的 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)
4

0 回答 0