0

I am trying to find the most efficient way to generate a series of images based on a number of input variables. The output should be a series of "zones" connected by "channels" to a central region in the middle. I want to be able to enter the number of zones I want to use, and have the image automatically created with the zones and channels properly spaced within the image.

I have attempted to do this in python using the opencv library: although I have it working it is just a dumb brute force method. Can anyone provide some ideas on how to automate this code so the image is generated just from the input variables? Essentially looping over the # of zones while creating the zones and channels within the loop?

image showing layout for 2 and 3 zones

Here's the code I used to create the 3 zone version shown above:

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)
martineau
  • 119,623
  • 25
  • 170
  • 301
TPB
  • 737
  • 1
  • 6
  • 8
  • I'd split the code up into separate functions each called by a new `main()` one which drives the whole process. This will allow you to see the pattern(s) in what's being done. Next I'd replace references to global variables with ones to parameters that are passed to them as arguments from `main()`, again noting the patterns to what needs to go on. You may need to do this with both versions of your program to see what _they_ have in common. At that point you should have a good idea of what needs to go on to generalized version of `main()` which is passed a `num_zones` argument. – martineau May 08 '13 at 00:43
  • Thanks for the input martineau. I actually just got it working but it's still rather convoluted, tho it will work for what I need. If anyone in interested I can post the full code and would still like to optimize it if possible. – TPB May 09 '13 at 01:54
  • http://codereview.stackexchange.com/ – GPPK May 11 '13 at 09:33

0 Answers0