How it is possible to change all images orientation to same i.e. to vertical

My folder consisting of 1000 binary images with different orientation. I want to make all of them to vertically align. How it will be possible through a single code ? Later I want to save all the vertically aligned images to a new folder. Note:few images are already in vertical orientation . I want to keep them as it is .

댓글 수: 2

How do you define the orientation of your image? Is it based on the content of the image (e.g. there's a line in the image) or simply on the size of the image (i.e it's taller than wide)?
I am finding images orientation by regionprops

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

 채택된 답변

regionprops() is not needed. Simply use size(). Rotate if the image is wider than tall:
[rows, columns, numberOfColorChannels] = size(yourImage);
if columns > rows
yourImage= imrotate(yourImage, 90);
end

댓글 수: 8

Image Analyst: I have tried your code. It's not able to change the orientation of those images which are not actually vertically align.
Let's quit wasting our time and just post your images. From your ambiguous statements we can't tell if it's the orientation of the entire image, or the orientation of one or more regions inside the image. Of course the later is the more difficult since you may have multiple regions, each with it's own orientation.
As one example, see how I used the radon transform to rotate an image to align a football in the demo image.
Image Analyst: I am attaching few of my images here. Few images are already in vertically align(considering their orientation by regionprops). I want to set a condition where the program will leave those images as it is where they are already vertically align and will change rest images orientation to vertical which are having different orientations
folder = 'D:\folder1';
% folder = pwd;
filepattern = fullfile(folder, '*.png');
srcFiles = dir(filepattern);
numImages = length(srcFiles);
for k = 1 : numImages
fullFileName = fullfile(folder, srcFiles(k).name);
a = imread(fullFileName);
bwimg =bwareafilt(~a, 1);
s1=regionprops(bwimg,'Orientation','Centroid');
angle = s1(1).Orientation;
angleToRotateBy = 90 - angle;
rotatedImage = imrotate(bwimg, angleToRotateBy);
verticalProfile = sum(rotatedImage, 2);
firstLine = find(verticalProfile > 0, 1, 'first');
lastLine = find(verticalProfile > 0, 1, 'last');
topSum = sum(verticalProfile(firstLine:firstLine+50));
bottomSum = sum(verticalProfile(lastLine-50:lastLine));
if topSum > bottomSum
rotatedImage = imrotate(rotatedImage, 180);
end
path='D:\folder2\';
imwrite(rotatedImage,[path,num2str(k),'.png']);
end
Even I have tried your this above code too but not able to change all images orientation to the same.
Please help me in doing this . I have posted all the details here .
It might not work because you did this extremely unwise thing:
path='D:\folder2\';
Never overwrite the MATLAB search path or you'll get into trouble! Once you fix that, it seems to work fine!
% Initialization steps.
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;
% folder = 'D:\folder1';
folder = pwd;
filepattern = fullfile(folder, 'P*.png');
directoryInfo = dir(filepattern);
numImages = length(directoryInfo);
for k = 1 : numImages
baseFileName = directoryInfo(k).name
fullFileName = fullfile(folder, baseFileName);
grayImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 3
% It's not gray scale! Convert to gray scale.
grayImage = grayImage(:, :, 1); % Use red channel only.
end
% Bring up a new figure for each image.
figure;
subplot(2, 2, 1);
imshow(grayImage, []);
axis('on', 'image');
title(baseFileName, 'FontSize', fontSize, 'Interpreter', 'none');
drawnow;
binaryImage = grayImage < 128;
binaryImage = bwareafilt(binaryImage, 1); % Take largest blob only
subplot(2, 2, 2);
imshow(binaryImage, []);
axis('on', 'image');
title('Binary Image', 'FontSize', fontSize);
drawnow;
% Measure angle.
props=regionprops(binaryImage, 'Orientation', 'Centroid');
% Get slopes and angles.
angle = props.Orientation
angleToRotateBy = 90 - angle
slope = tand(angle);
% Draw line through centroid.
xCentroid = props.Centroid(1);
yCentroid = props.Centroid(2);
hold on;
plot(xCentroid, yCentroid, 'r*', 'MarkerSize', 13, 'LineWidth', 2);
plot(xCentroid, yCentroid, 'ro', 'MarkerSize', 13, 'LineWidth', 2);
% Fit line
x = linspace(1, columns);
y = -slope * (x - xCentroid) + yCentroid;
% Clip y values to image
y = max(y, 0);
y = min(y, rows);
line(x, y, 'Color', 'r', 'LineWidth', 2);
drawnow;
rotatedImage = imrotate(binaryImage, angleToRotateBy);
verticalProfile = sum(rotatedImage, 2);
firstLine = find(verticalProfile > 0, 1, 'first');
lastLine = find(verticalProfile > 0, 1, 'last');
topSum = sum(verticalProfile(firstLine:firstLine+50));
bottomSum = sum(verticalProfile(lastLine-50:lastLine));
if topSum > bottomSum
rotatedImage = imrotate(rotatedImage, 180);
end
subplot(2, 2, 3);
imshow(rotatedImage, []);
axis('on', 'image');
title('Rotated Image', 'FontSize', fontSize);
drawnow;
outputFileName = fullfile('D:\folder2\', [num2str(k),'.png'])
% imwrite(rotatedImage,outputFileName);
end
I fancied it up a bit also, and improved the variable names to make them more descriptive.
Image Analyst: this code is good going well for most of the images. But there are few images where I am getting the wrist of the hand gesture to the up side that I don't want. How to resolve that ? I mean how to modify this above code that will serve for all of images. Here again I am attaching some images. I am not the wrist portion of these gestures to upside down.
You may have to put in some ad hoc code to recognize and handle pathological image, like ones that are too roundish. For example you may have to track the angle across frames and do something special if the angle changed too much from one frame to the next. Sorry, I can't write a turnkey and robust gesture recognition system for you - it's just way too much work.
okey. Thank you for your time and response.

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

추가 답변 (1개)

Matt J
Matt J 2018년 6월 21일
Use imrotate() with the Orientation angle given by regionprops().

댓글 수: 4

My folder consisting of 1000 images it's not possible to apply imrotate for each individual images . I want a generalized code that will be applicable for all and will set those images orientation to vertical
Matt J
Matt J 2018년 6월 21일
편집: Matt J 2018년 6월 21일
My folder consisting of 1000 images it's not possible to apply imrotate for each individual images .
Why not?
You mean you don't want to load 1000 files? How did you test Image Analyst's solution if you didn't load the images into MATLAB?
Zara Khan commented:
I have already accessed and loaded them in matlab. But each image having different orientation and then how it will be possible to set all them to vertically align ? It is possible through a code ?
You loop through them and apply regionprops and imrotate to each one separately.

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

질문:

2018년 6월 21일

댓글:

2018년 6월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by