moving the center of an image from point to another?

Hi All
i have this image
i want to moving it to the center of the whole figure , how this can be done in matlab i.e how can i raise this image to the center of the whole figure.
for example the first center point coordinates is 160 173 and the second center point coordinates is 160 121
how can i moving the center from first point to the second point?
i tried this code
y=imread('.......bmp');
[r c]=size(y);
for i=0:1:r
for j=1:1:c
y(i,j)=y(i,j+50);
end
end
imshow(y);
but the result was this error:
??? Attempted to access y(0,52); index must be a positive integer or logical.
any help? my regards

 Akzeptierte Antwort

Image Analyst
Image Analyst am 29 Jan. 2012
Copy and paste this:
% Demo to shift a region in a binary image.
% By ImageAnalyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;
grayImage = peaks(200);
imshow(grayImage, []);
subplot(2,2,1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
axis on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% imtool(z)
binaryImage = grayImage > 5;
subplot(2,2,2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
axis on;
% Find the centroid of that binary region
measurements = regionprops(binaryImage, 'Centroid')
[rows columns] = size(binaryImage);
rowsToShift = round(rows/2- measurements.Centroid(2))
columnsToShift = round(columns/2 - measurements.Centroid(1))
% Call circshift to move region to the center.
shiftedImage = circshift(binaryImage, [rowsToShift columnsToShift]);
subplot(2,2,3);
imshow(shiftedImage, []);
title('Shifted Binary Image', 'FontSize', fontSize);
axis on;

Weitere Antworten (2)

Image Analyst
Image Analyst am 29 Jan. 2012

0 Stimmen

Is zero a positive integer? No. Indexing starts at 1 in MATLAB. Anyway, that won't move it. That's more like a copy and paste rather than a cut and paste. Try circshift
out = circshift(in, [-50, 0]);

8 Kommentare

mmm ssss
mmm ssss am 29 Jan. 2012
thanks
but when i implement cirshift(), and then multiplying it's result by the original image to continue implementing other operation on the shifted gray scale image ,the result will be similar to this
http://postimage.org/image/evle83zsh/5168b229/
please , can you inform me how this error can be corrected.
I don't understand what the "error" is. It shifted the images up by 50 pixels. That is what you said you wanted to do. What's the problem? Why do you want to shift the image anyway? Anything you want to do can be done on the unshifted image.
mmm ssss
mmm ssss am 29 Jan. 2012
i don't now how can i explain my question , but for example if i have an image at the buttom of the figure , then i implement many operations on it, after that i want to obtain its skeleton by morphological operation , i obtained the skeleton in addition to the effect of the buttom boundary, so i want to center the image(shifted above) before skeleton to get rid of the effect of the buttom boundary .i wish that my goal now is obvious.if it is not i can send you the resultant image that i got for more illustration.
Not sure what the problem is. Is it that you don't know how to figure out the "50" - the amount to shift the image?
mmm ssss
mmm ssss am 29 Jan. 2012
i am not sure that i understand your comment
Try my demo code I just submitted here.
mmm ssss
mmm ssss am 29 Jan. 2012
i try it and it worked on your image , now how can i make it work on my images , i have about 250 image.
please , give me simple and specific explanation
mmm ssss
mmm ssss am 29 Jan. 2012
also , why you use these
rowsToShift = round(rows/2- measurements.Centroid(2))
columnsToShift = round(columns/2 - measurements.Centroid(1))

Melden Sie sich an, um zu kommentieren.

mmm ssss
mmm ssss am 29 Jan. 2012

0 Stimmen

clear all
img=imread(.........);%reading image.
imshow(img);
level=graythresh(img);
x=im2bw(img,level);
figure,imshow(x);
measurements = regionprops(im2double(x), 'Centroid')
[rows columns] = size(x);
rowsToShift = round(rows/2- measurements.Centroid(2))
columnsToShift = round(columns/2 - measurements.Centroid(1))
shiftedImage = circshift(x, [rowsToShift columnsToShift]);
figure,
imshow(shiftedImage, []);
nnew=img.*cast(x,class(img));
medimage=medfilt2(nnew,[5 5]);%median Filtering image
figure,imshow(medimage);
z=adapthisteq(medimage);
figure,imshow(z);
H = fspecial('unsharp');
y=imfilter(z,H);
figure,imshow(y);
mIm=imfilter(y,fspecial('average',31),'replicate');
sIm=y-mIm;
bw=im2bw(sIm,0); % Convert to binary image
ALT_img=imcomplement(bw); % Complement binary image
figure,imshow(ALT_img);
%%%%%%morphological on binary images.
open_img =bwmorph(ALT_img,'open' ,Inf);
figure,imshow(open_img);title('open_img');
major_img =bwmorph(open_img,'majority' ,Inf);
figure,imshow(major_img);title('major_img');
ske_img = bwmorph(major_img,'skel',100);
figure,imshow(ske_img);
please only coy and paste the above code

2 Kommentare

mmm ssss
mmm ssss am 29 Jan. 2012
*please only copy and paste the above code on this image to see the error by your eyes
http://www.2shared.com/photo/SAsZ1IeT/0002hv3.html
really i am confused , i dont know what can i do to manipulate this error
mmm ssss
mmm ssss am 30 Jan. 2012
Image Analyst , what are your opinion on my code

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by