Calculate and crop image based on coordinates
3 ビュー (過去 30 日間)
古いコメントを表示
Hi,
How can I calculate using Matlab the coordinates marked with an X only if I have the red point coordinates as I can crop the image according to them?
Thanks
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/145377/image.png)
Rotate version of the image:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/145378/image.png)
4 件のコメント
Joseph Cheng
2014 年 9 月 12 日
depends on what you have. are you working before or after the rotation? in the rotated image then you can see you can build the corners based on the red points. So they you have the corners being x1=x4=xa y1 = yb=y2.. and so on.
回答 (2 件)
Image Analyst
2014 年 9 月 12 日
編集済み: Image Analyst
2014 年 9 月 12 日
There is no unique box that goes through all the points. For example, here is another box I drew that is just as valid as yours:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/175630/image.png)
My red box goes through all 4 red spots just like your dashed blue box does.
What you might want to do is to binarize the image into black/white, true/false, 1/0. Then call bwlabel and regionprops to get the orientation. Then call imrotate() to rotate it by the negative of that angle.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_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 a color demo image.
folder = 'D:\Temporary stuff';
baseFileName = 'plug3.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage);
axis on;
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
grayImage = rgb2gray(rgbImage);
subplot(2, 2, 2);
imshow(grayImage);
title('Gray Scale Image', 'FontSize', fontSize);
binaryImage = grayImage < 250;
binaryImage = bwareaopen(binaryImage, 1000); % Get rid of small blobs.
subplot(2, 2, 3);
imshow(binaryImage);
title('Binary Image', 'FontSize', fontSize);
labeledImage = bwlabel(binaryImage);
measurements = regionprops(labeledImage, 'Orientation');
rotatedImage = imrotate(rgbImage, -measurements(1).Orientation);
subplot(2, 2, 4);
imshow(rotatedImage);
title('Rotated Image', 'FontSize', fontSize);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/175631/image.jpeg)
1 件のコメント
Image Analyst
2014 年 9 月 24 日
編集済み: Image Analyst
2014 年 10 月 1 日
Dan, what's the story? Are you still alive? Did my code do the rotation like you want?
Joseph Cheng
2014 年 9 月 12 日
the documentation on imcrop (correct function name) should tell you if it is correct, but i think it looks to be correct.
I would try this instead, use the original image and use the outer most points to find a temporary set of "cropped" the image corners. Then use the corners to find the image rotation angle to get the red points to be inline with cropped image edge then use matlab to rotate and crop.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!