2017-05-30 64 views
-1

我想找到物體的角落。 我嘗試下面的代碼:如何在matlab中找到旋轉物體的角落?

Vstats = regionprops(BW2,'Centroid','MajorAxisLength','MinorAxisLength',... 
    'Orientation'); 
u = [Vstats.Centroid]; 
VcX = u(1:2:end); 
VcY = u(2:2:end); 

[VcY id] = sort(VcY); % sorting regions by vertical position 
VcX = VcX(id); 
Vstats = Vstats(id); % permute according sort 
Bv = Bv(id); 

Vori = [Vstats.Orientation]; 
VRmaj = [Vstats.MajorAxisLength]/2; 
VRmin = [Vstats.MinorAxisLength]/2; 

% find corners of vertebrae 
figure,imshow(BW2) 
hold on 
% C = corner(VER); 
% plot(C(:,1), C(:,2), 'or'); 

C = cell(size(Bv)); 
Anterior = zeros(2*length(C),2); 
Posterior = zeros(2*length(C),2); 
for i = 1:length(C) % for each region 
    cx = VcX(i); % centroid coordinates 
    cy = VcY(i); 
    bx = Bv{i}(:,2); % edge points coordinates 
    by = Bv{i}(:,1); 
    ux = bx-cx; % move to the origin 
    uy = by-cy; 
    [t, r] = cart2pol(ux,uy); % translate in polar coodinates 
    t = t - deg2rad(Vori(i)); % unrotate 
    for k = 1:4 % find corners (look each quadrant) 
     fi = t((t>=(k-3)*pi/2) & (t<=(k-2)*pi/2)); 
     ri = r((t>=(k-3)*pi/2) & (t<=(k-2)*pi/2)); 
     [rp, ip] = max(ri); % find farthest point  
     tc(k) = fi(ip); % save coordinates 
     rc(k) = rp; 
    end 
    [xc,yc] = pol2cart(tc+1*deg2rad(Vori(i)) ,rc); % de-rotate, translate in cartesian 
    C{i}(:,1) = xc + cx; % return to previous place 
    C{i}(:,2) = yc + cy; 
    plot(C{i}([1,4],1),C{i}([1,4],2),'or',C{i}([2,3],1),C{i}([2,3],2),'og') 

    % save coordinates : 
    Anterior([2*i-1,2*i],:) = [C{i}([1,4],1), C{i}([1,4],2)]; 
    Posterior([2*i-1,2*i],:) = [C{i}([2,3],1), C{i}([2,3],2)]; 
end 

我輸入圖像是: enter image description here

我沒有正確檢測到圖像中的最底層對象的下列輸出圖像 enter image description here

。我該如何糾正代碼?它無法爲旋轉的圖像工作。

+0

您是否嘗試過使用matlab的內置角點檢測器: ['detectHarrisFeatures'](https://www.mathworks.com/help/vision/ref/detectharrisfeatures.html)? – kedarps

+0

我不知道如何使用它 – user1234

+0

您是否也可以發佈原始圖像,以便任何答覆者都可以將其用於演示代碼? – Wolfie

回答

0

您可以獲取圖像中的所有點,並使用聚類和劃分點將其分成8組。一旦分區完成,你就可以獲得點數,你可以選擇你想要的點數。

rgbImage = imread('your image') ; 
    %% crop out the unwanted white background from the image 
    grayImage = min(rgbImage, [], 3); 
    binaryImage = grayImage < 200; 
    binaryImage = bwareafilt(binaryImage, 1); 
    [rows, columns] = find(binaryImage); 
    row1 = min(rows); 
    row2 = max(rows); 
    col1 = min(columns); 
    col2 = max(columns); 
    % Crop 
    croppedImage = rgbImage(row1:row2, col1:col2, :); 
    I = rgb2gray(croppedImage) ; 
    %% Get the white regions 
    [y,x,val] = find(I) ; 
    %5 use kmeans clustering 
    [idx,C] = kmeans([x,y],8) ; 
    %% 
    figure 
    imshow(I) ; 
hold on 
for i = 1:8 
    xi = x(idx==i) ; yi = y(idx==i) ; 
    id1=convhull(xi,yi) ; 
    coor = [xi(id1) yi(id1)] ; 
    [id,c] = kmeans(coor,4) ;  

    plot(coor(:,1),coor(:,2),'r','linewidth',3) ; 
    plot(c(:,1),c(:,2),'*b') 
end 

enter image description here

enter image description here

現在,我們能夠捕捉到regions..the邊界/凸包點在手。你可以做任何你想要的數據。

+0

我想找到四個角落。你的代碼不給它 – user1234

+0

你可以檢查我的輸出圖像嗎?我需要對象的四個角落 – user1234

+0

不同的答案爲不同的run.y所以??? – user1234