how to detect the value angle of rotation object ?

i have a image with 4 object, i want to know how much the angel of rotation object. please help, thanks

 Risposta accettata

Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox'); % license('test','Statistics_toolbox'), license('test','Signal_toolbox')
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
%===============================================================================
% Read in gray scale demo image.
folder = pwd
baseFileName = '1.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Let's compute and display the histogram.
subplot(2, 2, 2);
histogram(grayImage, 0:256);
grid on;
title('Histogram of original image', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
xlim([0 255]); % Scale x axis manually.
% Threshold and binarize the image
binaryImage = grayImage > 128;
% Display the image.
subplot(2, 2, 3);
imshow(binaryImage, []);
axis on;
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Label the image
labeledImage = bwlabel(binaryImage);
% Get the orientation
measurements = regionprops(labeledImage, 'Orientation', 'MajorAxisLength', 'Centroid');
allAngles = -[measurements.Orientation]
hold on;
for k = 1 : length(measurements)
fprintf('For blob #%d, the angle = %.4f\n', k, allAngles(k));
xCenter = measurements(k).Centroid(1);
yCenter = measurements(k).Centroid(2);
% Plot centroids.
plot(xCenter, yCenter, 'r*', 'MarkerSize', 15, 'LineWidth', 2);
% Determine endpoints
axisRadius = measurements(k).MajorAxisLength / 2;
x1 = xCenter + axisRadius * cosd(allAngles(k));
x2 = xCenter - axisRadius * cosd(allAngles(k));
y1 = yCenter + axisRadius * sind(allAngles(k));
y2 = yCenter - axisRadius * sind(allAngles(k));
fprintf('x1 = %.2f, y1 = %.2f, x2 = %.2f, y2 = %.2f\n\n', x1, y1, x2, y2);
plot([x1, x2], [y1, y2], 'r-', 'LineWidth', 2);
end

25 Commenti

thank you for answare my question, i've tried your code. But, I'm confused in this section allAngles = -[measurements.Orientation]. Whether allAngles mean the angle for all each object ?
It's the angles for all the blobs. Each blob has one angle. If there are 4 blobs, then there will be 4 angles.
ooh i see. thanks but, whether to seek orientation angle of the object, using this method? allAngles = -[measurements.Orientation] ?? why it was negative sign ?
Because the bottom of the image is at the top. Line 1 is at the top for images and arrays, not the bottom like traditional cartesian coordinates. When I had it positive, the line was going along the minor axis, not the major axis.
but, if i only want to find the angle of the object, whether i should find the majorAxesLength for each object??
No, if you only want the angle, then ask only for 'Orientation' and nothing else.
okay, thank you so much .. God Bless :)
ElizabethR
ElizabethR il 23 Apr 2016
Modificato: ElizabethR il 24 Apr 2016
@Image Analyst , may i ask again ? if this image is like this
how to detect the value of angle for the 4 biggest object ? thanks
call bwareafilt(binaryImage, 4) to extract the 4 largest blobs. Then label and call regionprops
binaryImage = bwareafilt(binaryImage, 4);
binaryImage = imfill(binaryImage, 'holes');
labeledImage = bwlabel(binaryImage);
measurements = regionprops(labeledImage, 'Orientation');
allAngles = [measurements.Orientation]
thanks for answare ... i try but, i get the error >> a=imread('31.jpg'); >> bw=im2bw(a); >> imshow(a) >> b = bwareafilt(bw, 4); Undefined function 'bwareafilt' for input arguments of type 'double'. how to fix it ? thanks
To fix your formatting, read this link
To get the bwareafilt() function you'll have to upgrades because you must have an old version. Alternatively, you can use my function, attached.
ElizabethR
ElizabethR il 24 Apr 2016
Modificato: ElizabethR il 24 Apr 2016
ooh yes, i see. i have tried your code and it working perfectly. Thank you so Image Analyst for always helping me. But, how to get the index of the object ? wheter the index of the object is same with labelled of the object ? and may i ask ? what the meaning of this code ?
biggestBlob = ismember(labeledImage, sortIndexes(1:numberToExtract));
thanks
There are lots of blobs there. Surely you must know which ones you want to merge. I don't think you want to merge random blobs. That wouldn't be any good. So you must know which are to be merged with which.
ElizabethR
ElizabethR il 24 Apr 2016
Modificato: ElizabethR il 24 Apr 2016
but, how to know the index for each blobs ? so, what the the meaning of this code ? biggestBlob = ismember(labeledImage, sortIndexes(1:numberToExtract)); sorry but I do not understand with your answer.
Again, you must know which blobs you want to merge. Why do you not know this? If you don't know or don't care, then just merge the 13th blob with the 42nd blob and be done with it.
ismember() gets the specified number of blobs. Basically ismember() pulls out pixels in the image that have the same value as the second input argument to ismember. So if the biggest blobs are #3, 5, 11, and 18, then ismember() will give you a labeled image with only those numbered blobs in it and all others will be removed/gone.
yes you are right. thanks for your explanation. But, i mean, How to know the index for each blob ? for example the first big blobs is blobs with index 5 or the second blobs is blobs with index 10. How to get the index ? i hope you are understand with my question. Thank you
One way is to call impixelinfo() after you display the image. Then you can mouse around over the image and see what the label numbers are. Another way is to simply give the (row, column) coordinate/index to the labeled matrix. Or you could use ginput().
thaks for answare. but i mean is like this code
[sortedAreas, sortIndexes] = sort(allAreas, 'descend');
how to get the value of sortIndexes ??
The function sort() returns it as one of the output arguments, so you already have it.
You need to use watershed to break them apart. Start your own question for this.
Hi @Image Analyst , can i use your code above to know the rotation angle for my image below? rotation angle of a box. What part do i need to change?
No, the angle of the major axis of that object does not correspond to the angle of the edges to the edges of the image. I suggest you try hough or houghlines
Can you help me with some referrence or example? Please.
I know for a fact that there is a demo in the help documentation for houghlines. Did you overlook it? If you need further help, start your own question with your image and attempt at code attached.

Accedi per commentare.

Più risposte (1)

You can find the major axes of all the four objects. The direction of each of their major axis gives the angle of rotation.

2 Commenti

thanks Meghana for answare my question.. but, how to find the major axes of all the four object ?
help me please :(

Accedi per commentare.

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by