Search code examples
matlabimage-processingmatlab-figuregeometry

Summarize all intersection points by clockwise direction


This a program that I have, I already asked before for how to find the intersection on my image with a circle, and somebody has an answer it (thank you) and I have another problem...

a = imread('001_4.bmp');
I2 = imcrop(a,[90 5 93 180]);
[i,j]=size(I2);    

x_hist=sum(I2,1);
y_hist=(sum(I2,2))';

x=1:j ; y=1:i;
centx=sum(x.*x_hist)/sum(x_hist);
centy=sum(y.*y_hist)/sum(y_hist);

BW = edge(I2,'Canny',0.5);
bw2 = imcomplement(BW);
circle = int32([centx,centy,40]);%<<----------
shapeInserter = vision.ShapeInserter('Fill',false);
release(shapeInserter);
set(shapeInserter,'Shape','Circles');
% construct binary image of circle only
bwCircle = step(shapeInserter,true(size(bw2)),circle); 
% find the indexes of the intersection between the binary image and the circle
[i, j] = find ((bw2 | bwCircle) == 0);     
figure 
imshow(bw2 & bwCircle) % plot the combination of both images
hold on
plot(j, i, 'r*') % plot the intersection points
K = step(shapeInserter,bw2,circle);

[n,m]=size(i);
d=0;
k=1;
while (k < n)
    d = d + sqrt((i(k+1)-i(k)).^2 + (j(k+1)-j(k)).^2);
    k = k+1;        
end

enter image description here

Q: How can I calculate all the existing intersection values(red *) in a clockwise direction?


Solution

  • Not sure, but I think that your teacher meant something different from the answer given in the previous question, because

    • CW direction can be a hint, not a additional problem
    • I've seen no clue that the circle should be drawn; for small radius circles are blocky and simple bw_circle & bw_edges may not work. For details please see "Steve on image processing" blog, "Intersecting curves that don't intersect"[1]
    • May be your teacher is rather old and wants Pascal-stile answer.

    If my assumptions are correct the code below should be right

    img = zeros(7,7); %just sample image, edges == 1
    img(:,4) = 1; img(sub2ind(size(img),1:7,1:7)) = 1;
    ccx = 4; % Please notice: x is for columns, y is for rows
    ccy = 3;
    rad = 2;
    theta = [(pi/2):-0.01:(-3*pi/2)]; 
    % while plotting it will appear CCW, for visual CW and de-facto CCW use
    % 0:0.01:2*pi
    cx = rad * cos(theta) + ccx; % gives slightly different data as for 
    cy = rad * sin(theta) + ccy; % (x-xc)^2 + (y-yc)^2 == rad^2 [2]
    ccoord=[cy;cx]'; % Once again: x is for columns, y is for rows
    [ccoord, rem_idx, ~] =unique(round(ccoord),'rows');
    cx = ccoord(:,2); 
    cy = ccoord(:,1);
    circ = zeros(size(img)); circ(sub2ind(size(img),cy,cx))=1;
    cross_sum = 0; 
    figure, imshow(img | circ,'initialmagnification',5000)
    hold on,
    h = [];
    for un_ang = 1:length(cx),
        tmp_val= img(cy(un_ang),cx(un_ang));
        if  tmp_val == 1 %the point belongs to edge of interest
            cross_sum = cross_sum + tmp_val;
            h = plot(cx(un_ang),cy(un_ang),'ro');
            pause,
            set(h,'marker','x')
        end
    end
    hold off
    

    [1] https://blogs.mathworks.com/steve/2016/04/12/intersecting-curves-that-dont-intersect/

    [2] http://matlab.wikia.com/wiki/FAQ#How_do_I_create_a_circle.3F