Matlab: how to avoid ellipses overlapping in image?

前端 未结 3 626
北恋
北恋 2020-12-21 22:35

I\'ve been using a function file [ret]=drawellipse(x,y,a,b,angle,steps,color,img). Calling the function through a script file to draw random ellipses in image.

3条回答
  •  别那么骄傲
    2020-12-21 23:18

    Assuming you are drawing the ellipses into a raster graphics image, you could calculate the pixels you would have to draw for an ellipse, check whether these pixels in the image are still of the background color, and draw the ellipse only if the answer is yes, otherwise reject it (because something else, i.e. another ellipse, is in the way) and try other x,y,a and b.

    Alternatively, you could split your image into rectangles (not neccessarily of equal size) and place one ellipse in each of those, picking x,y,a,b such that no ellipse exceeds its rectangle - then the ellipses cannot overlap either, but it depends on how much "randomness" your ellipse placing should have whether this suffices.

    The mathematically rigorous way would be to store x,y,a,b of each drawn ellipse and for each new ellipse, do pairwise checks with each of those whether they have common points by solving a system of two quadratic equations. However, this might be a bit complicated, especially once the angle is not 0.

    Edit in response to the added code: Instead of fixing all x's and y's before the loop, you can determine them inside the loop. Since you know how many ellipses you want, but not how many you have to sample, you need a while loop. The test loop you give may come in handy, but you need to compare all previous ellipses to the one created in the loop iteration, not the first one.

    i=1;
    while (i<=4) %# or length(a), or, more elegantly, some pre-defined max
        x(i) = rand*400; y(i) = rand*400; %# or take x and y as givren and decrease a and b
        %# now, check overlap for given center
        overlap = false;
        for k=1:(i-1)
           overlap = overlap || overlap_ellipses(x(i),y(i),a(i),b(i),angle(i),x(k),y(k),a(k),b(k),angle(k))
        end
        if (~overlap)
            img = drawellipse(x(i),y(i),a(i),b(i),angle(i),steps,color,img);
            i = i+1; %# determine next ellipse
        end %# else x(i) and y(i) will be overwritten in next while loop iteration
    end
    

    Of course, if a and b are fixed, it may happen that no ellipse fits the image dimensions if the already present ones are unfortunately placed, resulting in an infinite loop. Regarding your plan of leaving the center fixed and decreasing the ellipse's size until it fits: where does your overlap_ellipses method come from? Maybe itcan be adapted to return a factor by which one ellipse needs to be shrinked to fit next to the other (and 1 if it fits already)?

提交回复
热议问题