vertical rotation of a leaf image using orientation of farthest points code is given. but not getting proper rotation. here input and output given

2 views (last 30 days)
clear; workspace; % Make sure the workspace panel is showing. format long g; format compact; fontSize = 25;
imgrd = imread('1062.tif'); BW=imbinarize(imgrd); D=BW; str=strel('disk',4,0);%Structuring element F=imerode(D,str);%Erode the image by structuring element img = D-F; figure; imshow(img); title("boudary image"); bw2 = bwareaopen(img,1000); figure, imshow(bw2); title("2nd image"); I2 = imcomplement(bw2); %title("complement image"); %imshow(I2); stats = regionprops(~I2); figure,imshow(I2); title("3rd");
hold on; for i = 1:1 %numel(stats) rectangle('Position', stats(i).BoundingBox, ... 'Linewidth', 2, 'EdgeColor', 'r', 'LineStyle', '-'); p1=stats(i).BoundingBox(1); p2=stats(i).BoundingBox(2); p3=stats(i).BoundingBox(3); p4=stats(i).BoundingBox(4); end
I3 = imcrop(I2, [p1 p2 p3 p4]);
figure;
imshow(I3, 'Border', 'tight');
I4 = imcomplement(I3);
figure;
imshow(I4);
I5 =I4;
boundaries = bwboundaries(~I5); boundary = boundaries{1}; boundaryX1 = boundary(:, 2); boundaryY1 = boundary(:, 1); boundaryX2 = boundary(:, 2); boundaryY2 = boundary(:, 1);
[distances, index] = pdist2([boundaryX1, boundaryY1], [boundaryX2, boundaryY2], 'euclidean', 'largest', 1); [maxDistance, indexOfMax] = max(distances); index2 = indexOfMax; index1 = index(indexOfMax); %plot([boundaryX1(index1), boundaryX2(index2)], [boundaryY1(index1), boundaryY2(index2)], 'mo-', 'LineWidth', 2, 'MarkerSize', 20); angle = atan2d(boundaryY2(index2) - boundaryY1(index1) , boundaryX2(index2) - boundaryX1(index1)); if angle >= 0 b1 = imrotate(I5,angle+90); else b1 = imrotate(I5,-angle+90); end %I2 = A;
%Again rotate image crop
I7 = imcomplement(b1);
stats = regionprops(~I7);
imshow(I7);
hold on;
for j = 1:1
rectangle('Position', stats(j).BoundingBox, ...
'Linewidth', 2, 'EdgeColor', 'r', 'LineStyle', '-');
p11=stats(j).BoundingBox(1);
p21=stats(j).BoundingBox(2);
p31=stats(j).BoundingBox(3);
p41=stats(j).BoundingBox(4);
end
I6 = imcrop(I7, [p11 p21 p31 p41]);
figure;
imshow(I6);
title("rotated");
<<
>>

Answers (1)

NASU NASU
NASU NASU on 28 Mar 2018

Categories

Find more on Images in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!