Detecting center point of cross using Matlab

后端 未结 6 1423
南笙
南笙 2020-12-30 09:46

\"alt

Hello, I have an image as shown above. Is it possible for me to detect the center point of the cro

6条回答
  •  执念已碎
    2020-12-30 09:55

    I just encountered the same kind of problem, and I found other solutions that I would like to share:

    Assume image file name is pict1.jpg.

    1.Read input image, crop relevant part and covert to Gray-scale:

    origI = imread('pict1.jpg'); %Read input image
    
    I = origI(32:304, 83:532, :); %Crop relevant part
    I = im2double(rgb2gray(I)); %Covert to Grayscale and to double (set pixel range [0, 1]).
    

    2.Convert image to binary image in robust approach:

    %Subtract from each pixel the median of its 21x21 neighbors
    %Emphasize pixels that are deviated from surrounding neighbors
    medD = abs(I - medfilt2(I, [21, 21], 'symmetric'));
    
    %Set threshold to 5 sigma of medD
    thresh = std2(medD(:))*5;
    
    %Convert image to binary image using above threshold
    BW = im2bw(medD, thresh);
    

    BW Image:

    3.Now I suggest two approaches for finding the center:

    • Find find centroid (find center of mass of the white cluster)
    • Find two lines using Hough transform, and find the intersection point

    Both solutions return sub-pixel result.

    3.1.Find cross center using regionprops (find centroid):

    %Find centroid of the cross (centroid of the cluster)
    s = regionprops(BW, 'centroid');
    centroids = cat(1, s.Centroid);
    
    figure;imshow(BW);
    hold on, plot(centroids(:,1), centroids(:,2), 'b*', 'MarkerSize', 15), hold off
    
    %Display cross center in original image
    figure;imshow(origI), hold on, plot(82+centroids(:,1), 31+centroids(:,2), 'b*', 'MarkerSize', 15), hold off
    

    Centroid result (BW image):

    Centroid result (original image):

    3.2 Find cross center by intersection of two lines (using Hough transform):

    %Create the Hough transform using the binary image.
    [H,T,R] = hough(BW);
    
    %ind peaks in the Hough transform of the image.
    P  = houghpeaks(H,2,'threshold',ceil(0.3*max(H(:))));
    x = T(P(:,2)); y = R(P(:,1));
    
    %Find lines and plot them.
    lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
    figure, imshow(BW), hold on
    L = cell(1, length(lines));
    for k = 1:length(lines)
        xy = [lines(k).point1; lines(k).point2];
        plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
    
        % Plot beginnings and ends of lines
        plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
        plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
    
        %http://robotics.stanford.edu/~birch/projective/node4.html
        %Find lines in homogeneous coordinates (using cross product):
        L{k} = cross([xy(1,1); xy(1,2); 1], [xy(2,1); xy(2,2); 1]);
    end
    
    %https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
    %Lines intersection in homogeneous coordinates (using cross product):
    p = cross(L{1}, L{2});
    
    %Convert from homogeneous coordinate to euclidean coordinate (divide by last element).
    p = p./p(end);
    plot(p(1), p(2), 'x', 'LineWidth', 1, 'Color', 'white', 'MarkerSize', 15)
    

    Hough transform result:

提交回复
热议问题