how to detect the value angle of rotation object ?

조회 수: 13 (최근 30일)
ElizabethR 2016년 4월 15일
i have a image with 4 object, i want to know how much the angel of rotation object. please help, thanks

채택된 답변

Image Analyst
Image Analyst 2016년 4월 18일
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.
% 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);
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.
% 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);
  댓글 수: 25
Image Analyst
Image Analyst 2022년 5월 18일
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.
Nurul Farhana Mohd Fadzli
Nurul Farhana Mohd Fadzli 2023년 1월 4일
Thank you so much

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Meghana Dinesh
Meghana Dinesh 2016년 4월 15일
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
ElizabethR 2016년 4월 15일
thanks Meghana for answare my question.. but, how to find the major axes of all the four object ?
ElizabethR 2016년 4월 18일
help me please :(

댓글을 달려면 로그인하십시오.


Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by