What is an simple way to compute the overlap between an image and a polygon?

前端 未结 6 1244
清酒与你
清酒与你 2020-12-16 05:03

I have a closed non-self-intersecting polygon. Its vertices are saved in two vectors X, and Y. Finally the values of X and Y are bound between 0 and 22.

I\'d like t

6条回答
  •  眼角桃花
    2020-12-16 05:27

    One suggestion is to use the polybool function (not available in 2008b or earlier). It finds the intersection of two polygons and returns resulting vertices (or an empty vector if no vertices exist). To use it here, we iterate (using arrayfun) over all of the squares in your grid check to see whether the output argument to polybool is empty (e.g. no overlap).

    N=22;
    sqX = repmat([1:N]',1,N);
    sqX = sqX(:);
    sqY = repmat(1:N,N,1);
    sqY = sqY(:);
    
    intersects = arrayfun((@(xs,ys) ...
          (~isempty(polybool('intersection',X,Y,[xs-1 xs-1 xs xs],[ys-1 ys ys ys-1])))),...
          sqX,sqY);
    
    intersects = reshape(intersects,22,22);
    

    Here is the resulting image:

    enter image description here

    Code for plotting:

    imagesc(.5:1:N-.5,.5:1:N-.5,intersects');
    hold on;
    plot(X,Y,'w');
    for x = 1:N
        plot([0 N],[x x],'-k');
        plot([x x],[0 N],'-k');
    end
    hold off;
    

提交回复
热议问题