How can i slip and rotate a color image?

Hi,how can i slip and rotate the color image img like the binary image bw?
I=imread('image.jpg'); %color image
bw=segmentation(I); % the result is a binary image where the object detected (ROI) is white
img = bsxfun(@times, I, cast(bw,class(I)));
phy = regionprops(bw, 'Orientation')
[barx,bary]=barycentre(edge);
% slip the region to the center of the image
edge = recentre(edge,barx,bary);
I2 = recentre(bw,barx,bary);
phy1=phy.Orientation;
edge=imrotate(edge,-phy,'loose');
I3=imrotate(I2,-phy,'loose');
thanks

4 Comments

To split the color region to the center of the image, I tried to do this but I get a binary image img2 like I2 although img is a color image :(
r = recentre(img(:,:,1),barx,bary);
g = recentre(img(:,:,2),barx,bary);
b = recentre(img(:,:,3),barx,bary);
img2 = cat(3, r, g, b);
Why isn't imrotate working for you?
For more fancy transformations, look into imtransform.
The function recenter and imrotate work with the binary image bw where the ROI is centred and rotated very well but when i want to center and rotate the ROI of the color image img it doesn't work :( . I couldn't use imtransform to center the region and rotate the image with a given angle -phy
use imrotate after separating rgb planes and after that combine those 3 planes

Sign in to comment.

 Accepted Answer

Have you tried circshift() to slide the image over?

7 Comments

I didn't understand how I can use circshift() to slide the color image in order to center the region of interest detected in the image
Try this code as an example:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'onion.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]);
% Ask the user to click the point they want to be at the center.
userPrompt = 'Click the point you want to be at the center';
promptMessage = sprintf('Click the point you want to be at the center,\nor Cancel to abort processing?');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmp(button, 'Cancel')
return;
end
[x, y] = ginput(1)
% Determine the middle of the picture.
middleX = columns/2
middleY = rows/2
deltaX = fix(middleX - x)
deltaY = fix(middleY - y)
% Use circshift to shift the image.
% Move white onion to the middle of the image.
shiftsize = [deltaY deltaX 0]
shiftedImage = circshift(rgbImage, shiftsize);
subplot(2, 2, 2);
imshow(shiftedImage, []);
title('Shifted Color Image', 'FontSize', fontSize);
impixelinfo();
axis on;
thanks, it slides the region but not to the center of the image. So how can I fix the shiftsize for example using the calculation of the barycenter, centroid... of the image
I don't think it's beyond you to figure out what the shift needs to be in the two directions. Give it a try. If really you can't figure out how to subtract the desired x position from the starting x position, write back.
Okay - I've edited it for you to let the user click on a point that she wants to move to the center of the image. Show the older comments above to get the edited code. Let me know if that works for you. With circshift, the edge of the image wraps around, which should not be a problem if it's black. If you want the stuff shifted out of the image to be cut off and the stuff shifted in to be black (instead of from the opposite side of the image) then it's an easy adaptation that you can do.
thanks it works with some modifications knowing that I want to move the centroid of the object to the center of the image
OK great. Go ahead and mark it as "Answered" then.

Sign in to comment.

More Answers (1)

imrotate can rotate color images just fine:
imshow(imrotate(imread('peppers.png'),42,'crop'))

2 Comments

It seems that the problem is in the function recentre because it works with the binary image bw but it doesn't work with the color image img
I=imread('image.jpg'); %color image
bw=segmentation(I); % the result is a binary image where the object detected (ROI) is white
img = bsxfun(@times, I, cast(bw,class(I)));
edge2 = edge(bw, 'prewitt');
edge2 = 1 - edge2;
[barx,bary]=barycentre(edge2);
edge2 = recentre(edge2,barx,bary);
I2 = recentre(bw,barx,bary);
I3 = recentre(img,barx,bary);
The function
function im_trans=recentre(image,barx,bary)
[X,Y]=size(image);
tx=floor(X/2)-barx;
ty=floor(Y/2)-bary;
im_trans = zeros(X, Y);
if tx>=0
if ty>=0
for i=1 : X-tx
for j=1 : Y-ty
im_trans(i+tx, j+ty) = image(i, j);
end
end
elseif ty<0
for i=1 : X-tx
for j=abs(ty)+1 : Y
im_trans(i+tx, j+ty) = image(i, j);
end
end
end
elseif tx<0
if ty>=0
for i=abs(tx)+1 : X
for j=1 : Y-ty
im_trans(i+tx, j+ty) = image(i, j);
end
end
elseif ty<0
for i=abs(tx)+1 : X
for j=abs(ty)+1 : Y
im_trans(i+tx, j+ty) = image(i, j);
end
end
end
end
can you help me to correct it please

Sign in to comment.

Categories

Find more on Read, Write, and Modify Image in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!