can someone help me edit the code below so that the output image can display the value of rotation angle of the object with axis guide

It is just the example image that i want, the axis with the angle:
this is my input image:
% Perform edge detection with interpolation during non maximum suppression
function CannyEdgeDetector()
close all; % Close figures
saveImage = true;
sigma = 1; % Gaussian filter sigma
highThresholdRatio = 0.275; % High threshold ratio
lowThresholdRatio = 0.25; % Low threshold ratio OF THE high threshold
% Change the current folder to the folder of this m-file.
% Courtesy of Brett Shoelson
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
im = imread('C:\Users\User\Pictures\bt.jpeg');
figure; imshow(im);
title('Original Image');
%if saveImage
%imwrite(im, 'C:\Users\User\Pictures\O3.jpeg');
%end
% Smooth with Gaussian 5x5 filter to reduce noise
im = rgb2gray(im);
figure; imshow(im);
title('B/W Image');
%if saveImage
%imwrite(im, 'Output_Photos\2_bw.jpg');
%end
im = double(imgaussfilt(im,sigma));
figure; imshow(NormalizeMatrix(im));
title('Gaussian Filter');
%if saveImage
%imwrite(NormalizeMatrix(im), 'Output_Photos\3_gaussian.jpg');
%end
% Find the intensity gradient of the image
Gx = SobelFilter(im, 'x');
Gy = SobelFilter(im, 'y');
Gx = imgaussfilt(Gx,sigma);
Gy = imgaussfilt(Gy,sigma);
figure; imshow(abs(normalize(Gx)));
title('Gx Sobel Filter');
%if saveImage
%imwrite(abs(NormalizeMatrix(Gx)), 'Output_Photos\4_gx_sobel.jpg');
%end
figure; imshow(abs(NormalizeMatrix(Gy)));
title('Gy Sobel Filter');
%if saveImage
%imwrite(abs(NormalizeMatrix(Gy)), 'Output_Photos\5_gy_sobel.jpg');
%end
% Find the magnitude of the gradient
Gmag = sqrt(Gx.^2 + Gy.^2);
angle = atan2(Gy,Gx)*180/pi;
figure; imshow(NormalizeMatrix(Gmag));
title('Gmag');
%if saveImage
%imwrite(NormalizeMatrix(Gmag), 'Output_Photos\6_gmag.jpg');
%end
% Perform non-maximum suppression using interpolation
[h,w] = size(im);
X=[-1,0,+1 ;-1,0,+1 ;-1,0,+1];
Y=[-1,-1,-1 ;0,0,0 ;+1,+1,+1];
output = zeros(h,w);
x = [0 1];
for i=2:h-1 % row
for j=2:w-1 % col
if (angle(i,j)>=0 && angle(i,j)<=45) || ...
(angle(i,j)<-135 && angle(i,j)>=-180)
yBot = [Gmag(i,j+1) Gmag(i+1,j+1)];
yTop = [Gmag(i,j-1) Gmag(i-1,j-1)];
x_est = abs(Gy(i,j)/Gmag(i,j)); % y
if (Gmag(i,j) >= ((yBot(2)-yBot(1))*x_est+yBot(1)) && ...
Gmag(i,j) >= ((yTop(2)-yTop(1))*x_est+yTop(1))) % interpolation
output(i,j)= Gmag(i,j);
else
output(i,j)=0;
end
elseif (angle(i,j)>45 && angle(i,j)<=90) || ...
(angle(i,j)<-90 && angle(i,j)>=-135)
yBot = [Gmag(i+1,j) Gmag(i+1,j+1)];
yTop = [Gmag(i-1,j) Gmag(i-1,j-1)];
x_est = abs(Gx(i,j)/Gmag(i,j));
if (Gmag(i,j) >= ((yBot(2)-yBot(1))*x_est+yBot(1)) && ...
Gmag(i,j) >= ((yTop(2)-yTop(1))*x_est+yTop(1)))
output(i,j)= Gmag(i,j);
else
output(i,j)=0;
end
elseif (angle(i,j)>90 && angle(i,j)<=135) || ...
(angle(i,j)<-45 && angle(i,j)>=-90)
yBot = [Gmag(i+1,j) Gmag(i+1,j-1)];
yTop = [Gmag(i-1,j) Gmag(i-1,j+1)];
x_est = abs(Gx(i,j)/Gmag(i,j));
if (Gmag(i,j) >= ((yBot(2)-yBot(1))*x_est+yBot(1)) && ...
Gmag(i,j) >= ((yTop(2)-yTop(1))*x_est+yTop(1)))
output(i,j)= Gmag(i,j);
else
output(i,j)=0;
end
elseif (angle(i,j)>135 && angle(i,j)<=180) || ...
(angle(i,j)<0 && angle(i,j)>=-45)
yBot = [Gmag(i,j-1) Gmag(i+1,j-1)];
yTop = [Gmag(i,j+1) Gmag(i-1,j+1)];
x_est = abs(Gx(i,j)/Gmag(i,j));
if (Gmag(i,j) >= ((yBot(2)-yBot(1))*x_est+yBot(1)) && ...
Gmag(i,j) >= ((yTop(2)-yTop(1))*x_est+yTop(1)))
output(i,j)= Gmag(i,j);
else
output(i,j)=0;
end
end
end
end
Gmag = NormalizeMatrix(output);
figure; imshow(Gmag);
title('Non Maximum Suppression');
%if saveImage
%imwrite(Gmag, 'Output_Photos\7_non_maximum_suppression.jpg');
%end
% Perform double thresholding
highThreshold = max(max(Gmag))*highThresholdRatio;
lowThreshold = highThreshold*lowThresholdRatio;
strongEdgesRow = zeros(1,h*w); % Keep track of the strong edge row index
strongEdgesCol = zeros(1,h*w); % Keep track of the strong edge col index
weakEdgesRow = zeros(1,h*w); % Keep track of the weak edge row index
weakEdgesCol = zeros(1,h*w); % Keep track of the weak edge col index
strongIndex = 1;
weakIndex = 1;
for i=2:h-1 % row
for j=2:w-1 % col
if Gmag(i,j) > highThreshold % Strong edge
Gmag(i,j) = 1;
strongEdgesRow(strongIndex) = i;
strongEdgesCol(strongIndex) = j;
strongIndex = strongIndex + 1;
elseif Gmag(i,j) < lowThreshold % No edge
Gmag(i,j) = 0;
else % Weak edge
weakEdgesRow(weakIndex) = i;
weakEdgesCol(weakIndex) = j;
weakIndex = weakIndex + 1;
end
end
end
figure; imshow(Gmag);
title('Double Threshold');
%if saveImage
%imwrite(Gmag, 'Output_Photos\8_double_threshold.jpg');
%end
% Perform edge tracking by hysteresis
set(0,'RecursionLimit',10000)
for i=1:strongIndex-1
% Find the weak edges that are connected to strong edges and set
% them to 1
Gmag = FindConnectedWeakEdges(Gmag, strongEdgesRow(i),...
strongEdgesCol(i));
end
figure; imshow(Gmag);
title('Edge Tracking Before Clean Up');
%if saveImage
%imwrite(Gmag, 'Output_Photos\9_edge_tracking.jpg');
%end
% Remove the remaining weak edges that are not actually edges
% and is noise instead
for i=1:weakIndex-1
if Gmag(weakEdgesRow(i),weakEdgesCol(i)) ~= 1
Gmag(weakEdgesRow(i),weakEdgesCol(i)) = 0;
end
end
figure; imshow(Gmag);
title('Edge Tracking After Clean Up');
%if saveImage
%imwrite(Gmag, 'Output_Photos\10_final.jpg');
%end
% % MATLAB canny comparison
% im = imread('Test_Photos/test1.jpg');
% im = rgb2gray(im);
% im = edge(im, 'canny');
% figure; imshow(im);
% title('MATLAB');
end
% Normalize matrix
function[A] = NormalizeMatrix(A)
A = A/max(A(:));
end
% Perform sobel filter
function[A] = SobelFilter(A, filterDirection)
switch filterDirection
case 'x'
Gx = [-1 0 +1; -2 0 +2; -1 0 +1];
A = imfilter(A, double(Gx), 'conv', 'replicate');
case 'y'
Gy = [-1 -2 -1; 0 0 0; +1 +2 +1];
A = imfilter(A, double(Gy), 'conv', 'replicate');
otherwise
error('Bad filter direction - try inputs ''x'' or ''y''');
end
end
% Find weak edges that are connected to strong edges and set them to 1
function[Gmag] = FindConnectedWeakEdges(Gmag, row, col)
for i = -3:1:3
for j = -3:1:3
if (row+i > 0) && (col+j > 0) && (row+i < size(Gmag,1)) && ...
(col+j < size(Gmag,2)) % Make sure we are not out of bounds
if (Gmag(row+i,col+j) > 0) && (Gmag(row+i,col+j) < 1)
Gmag(row+i,col+j) = 1;
Gmag = FindConnectedWeakEdges(Gmag, row+i, col+j);
end
end
end
end
end

 Réponse acceptée

I'd use bwferet and get the min angle. Let me know if you can't figure it out.

16 commentaires

Can you help me using the code i attached above?
A = imread('toothlotion.png');
A = rgb2gray(A)>128;
% get properties
S = regionprops(A,'minferetproperties','centroid');
th = vertcat(S.MinFeretAngle);
C = vertcat(S.Centroid); % just used for placing labels
% reshape coordinates for plotting diameters
mfcoords = vertcat(S.MinFeretCoordinates);
x = reshape(mfcoords(:,1),2,[]).';
y = reshape(mfcoords(:,2),2,[]).';
% plot everything
imshow(A); hold on
for k = 1:size(x,1)
plot(x(k,:),y(k,:),'linewidth',3);
t = text(C(k,1),C(k,2),num2str(round(th(k))));
t.Color = 'r';
t.HorizontalAlignment = 'center';
end
Same thing. Instead of having 5 angles, you'll just have one angle.
Thank you,so i just can simply use the code? im sorry im still new to matlab. but why i get output like this
Probably because you need to get a clean binary image with only one object in it. You may be able to use bwareafilt() to do so, though it may take some other work to get the object edges cleaned up. It's hard to tell.
@DGM Sir, I want to deploy my coding to Nvidia Jetson but I just found out that minFeretProperties function are not supported for code generation in MATLAB Coder. Can you help me with another function that i can use to find the angle of object that can produce the same output like the above example ?
I'm sorry, but I really have no idea what will work for code generation. I don't think I even have those tools.
@DGMIts okay sir you help me a lot. Sir can you show me how to find the angle using regionprops(__,'Orientation'). I have tried this simple code below and the output angle is sometimes not correct but I will try. And I wish you could also help me producing an output image with an angle display on it like your example above.
regionprops looks at the angle of an ellipse fitted to the blob boundary. It's not always accurate for other shapes, especially rectangles. That's why I suggested bwferet instead of regionprops.
I already use that, like DGM's example above which is the minFeretProperties but the problem is, this function is not supported for code generation. bwferet also not supported for code generation. Now im trying to edit DGM's example above using other function that can also detect the angle of object. Can you help me sir. @Image Analyst
Then I suggest you find the algorithm for feret diameter and program up your own version. I can't help you with that.
Like @Image Analyst says (and I think I saw in your other thread), using "orientation" tends to orient rectangles along their diagonals, so I really doubt that's what you want.
regionprops(..., 'minferetproperties') relies on bwferet(). Not knowing what works with coder, I'm not sure what part of bwferet() is incompatible. It seems to all be straightforward, but maybe convhull() is the problem.
I don't have another implementation of bwferet().
Did you know how to find angle without calling function? using formula maybe?
If you open up bwferet(), you can basically see all the math involved in finding the feret properties. You might be able to make some simplifications if you could rely on your objects always being rectangles, or at least always being nominally convex.

Connectez-vous pour commenter.

Plus de réponses (1)

yes,sir,may be use minboundrect to get the rect area,such as

2 commentaires

Thank you but can i know how to get the rotation angle of the box ? It will be good if its also displaying the axis
Nevermind ,how about creating bounding box that exactly foloow the shape of the box? Please, I really need help

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by