0

Working on 2D Rectangular Nesting. Need to find the utilization percentage of the material. Assuming i have the length, breadth, left-bottom position of each rectangle. What is the best way to determine the Boundary-Cut Utilization?

Objective:- To find the AREA under the RED Line.

Sample images attached to depict what i have done and what i need.

What i have done enter image description here

what i need enter image description here

Another Example image of rectangles packed with allowance enter image description here

Santhan Salai
  • 3,888
  • 19
  • 29
  • I would perhaps place all of these rectangles in a binary image, then trace the boundary using a boundary tracing algorithm. How flexible can you be with regards to the algorithm? Could we use... say... the image processing toolbox? If you can, then you can very easily get what you want with a combination of logical indexing and `bwperim`. – rayryeng Mar 22 '15 at 07:09
  • Not familiar with image Image processing toolbox. but if you use bwperim, wont it give the perimeter for the inside holes too? could you explain it further, maybe with a code? i'm a beginner in matlab @rayryeng – Santhan Salai Mar 22 '15 at 07:20
  • The holes I didn't notice. You can simply fill in the holes using `imfill`, then extract the outer edge with `bwperim`. You are right where if we used `bwperim` straight up, it would get the perimeter of the holes, so it's best that you fill the holes using `imfill` first. Do you have this toolbox? I could certainly explain it further, but I would need to know how you are storing those rectangles. Could you provide any code that you have written to generate the above figure? It's going to be more difficult for me if I don't have it, as I don't know how you're representing those rectangles. – rayryeng Mar 22 '15 at 07:22
  • Or better yet, we don't even need the image processing toolbox. I can do it without that... but I'd still need to know those rectangles are stored. – rayryeng Mar 22 '15 at 07:24
  • Rectangles are stored as x,y position of the bottom-left of each rectangle. i have the corresponding length, breadth values in another variable.. BTW, i'm interested in Area under the RED line rather than perimeter.. i have the IP toolbox but i don't have much knowledge on it. – Santhan Salai Mar 22 '15 at 07:32
  • That's really easy to do. Once you find the shape, you can simply sum up the total number of "pixels" to determine the area. We don't need the perimeter in that case. – rayryeng Mar 22 '15 at 07:34
  • How do i generate a binary image out of the inputs i have? can you please come up with a solution with code? @rayryeng – Santhan Salai Mar 22 '15 at 07:56

2 Answers2

1

If you're interested in determining the total "area" underneath the red line, one suggestion I have is if you have access to the Image Processing Toolbox, simply create a binary image where we draw all of the rectangles on the image at once, fill all of the holes, then to determine the area, just determine the total sum of all of the binary "pixels" in the image. You said you have the (x,y) positions of the bottom-left corner of each rectangle, as well as the width and height of each rectangle. To make this compatible in an image context, the y axis is usually flipped so that the top-left corner of the space is the origin instead of the bottom-left. However, this shouldn't affect our analysis as we are simply reflecting the whole 2D space downwards.

Therefore, I would start with a blank image that is the same size as the grid you are dealing with, then writing a loop that simply sets a rectangular grid of coordinates to true for each rectangle you have. After, use imfill to fill in any of the holes in the image, then calculate the total sum of the pixels to get the area. The definition of a hole in an image processing context is any black pixels that are completely surrounded by white pixels. Therefore, should we have gaps that are surrounded by white pixels, these will get filled in with white.

Therefore, assuming that we have four separate variables of x, y, width and height that are N elements long, where N is the number of rectangles you have, do something like this:

N = numel(x); %// Determine total number of rectangles
rows = 100; cols = 200; %// Define dimensions of grid here
im = false(rows, cols); %// Declare blank image

%// For each rectangle we have...
for idx = 1 : N
    %// Set interior of rectangle at location all to true
    im(y(idx)+1:y(idx)+height(idx), x(idx)+1:x(idx)+width(idx)) = true;
end

%// Fill in the holes
im_filled = imfill(im, 'holes');

%// Determine total area
ar = sum(im_filled(:));

The indexing in the for loop:

im(y(idx)+1:y(idx)+height(idx), x(idx)+1:x(idx)+width(idx)) = true;

Is a bit tricky to deal with. Bear in mind that I'm assuming that y accesses the rows of the image and x accesses the columns. I'm also assuming that x and y are 0-based, so the origin is at (0,0). Because we access arrays and matrices in MATLAB starting at 1, we need to offset the coordinates by 1. Now, the beginning index for the row starts from y(idx)+1. We end at y(idx) + height(idx) because we technically start at y(idx)+1 but then we need to go up to height(idx) but then we also subtract by 1 as your coordinates begin at 0. Take for example a line with the width of 20, from x = 0 to x = 19. This width is 20, but we draw from 0, up to 20-1 which is 19. Because of the indexing starting at 1 for MATLAB, and the subtraction of 1 due to the 0 indexing, the +1 and -1 cancel, which is why we are just left with y(idx) + height(idx). The same can be said with the x coordinate and the width.

Once we draw all of the rectangles in the image, we use imfill to fill up the holes, then we can sum up the total area by just unrolling the whole image into a single vector and invoking sum. This should (hopefully) get what you need.


Now, if you want to find the area without the filled in holes (I suspect this is what you actually need), then you can skip the imfill step. Simply apply the sum on the im, instead of im_filled, and so:

ar = sum(im(:));

This will sum up all of the "white" pixels in the image, which is effectively the area. I'm not sure what you're actually after, so use one or the other depending on your needs.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • I actually need the one with filled holes. if i wanted without filled holes, obviously i would have summed up area of individual rectangles.. Great solution by the way. +1 – Santhan Salai Mar 22 '15 at 08:04
  • Although i know this would do my job, if u could come up with alternate answers which doesn't involve creating images, please do post it. because i need to calculate utilization percentage for each iterations like around 80000. I think creating image and doing image processing tricks would slow my actual process. – Santhan Salai Mar 22 '15 at 08:09
  • Can you generalize the code for other convex polygons as well? Assuming we have the X, Y Vectors of the vertices of each polygon. is it possible to do that using `Image processing toolbox` ? – Santhan Salai Mar 22 '15 at 08:15
  • With rectangles I have an idea without the IPT but it's rather late where I am and I need to go to bed. I'll write something when I get up. For general polygons, that'll be much more difficult. I would probably still use the IPT but with some more processing. I can provide a solution for rectangles but for polygons that's another story. – rayryeng Mar 22 '15 at 08:17
  • Btw thank you for accepting my answer even though it isn't quite complete yet. I'll add how to do this without the IPT tomorrow. – rayryeng Mar 22 '15 at 08:18
  • I think i figured it out for convex polygons. I could use `poly2mask` function to convert the convex polygon to binary image for each polygon and then sum up the individual matrices to get the resultant binary image. then follow your answer from `imfill`. Am i right? – Santhan Salai Mar 22 '15 at 08:46
  • @SanthanSalai - Yes `poly2mask` would work. Forgot about that function. However, you would need to make sure that the polygon is closed and so when you use `poly2mask`, you'd have to make sure the first and last points are the same. – rayryeng Mar 22 '15 at 18:32
  • I have edited the question to show another image.. i can't use IPT because i need to fill those open holes too as u could see in the third image. so, waiting for your alternate solution without using IPT. – Santhan Salai Mar 22 '15 at 18:43
  • @SanthanSalai - I'll have to think about it and get back to you. Unfortunately I have other priorities to deal with. – rayryeng Mar 22 '15 at 18:43
  • I have explained the problem in more detail [here](http://stackoverflow.com/questions/29231658/rectangular-nesting-convergence-to-optimal-solution-using-simulated-annealing) . It would be great for me if you take a look at that, why i need it this desperate. BTW, no hard feelings for un-marking this answer. I can't use IPT as i have to deal with allowances(open holes). If you could solve this without IPT, i will mark both your solutions as answers. – Santhan Salai Mar 24 '15 at 15:44
0

Boundary-Cut Area without using Image Processing Toolbox.

The Detailed question description and answer could be found here

This solution is applicable only to Rectangular parts.

Community
  • 1
  • 1
Santhan Salai
  • 3,888
  • 19
  • 29
  • if somebody could generalize the solution for other convex polygons as well, please share it. i'll mark it as **Answer** – Santhan Salai Mar 25 '15 at 05:20