error in imagesc - trying to shift an image after finding centroid

2 views (last 30 days)
I'm trying to find the centroid of an image that has one circle in it. Then use this coordinate along with the size of the image to decide how far to shift the image so that the circle is in the center of the screen. I'm getting all the information fine, but when I shift the image and try to display it, the colors are not the same when I use imshow and I get an error stating "Truecolor Cdata contains element out of range 0.0<=value<=1.0
My code is below, any help would be great!
BW = imread('imshift.png');
BW1=im2bw(BW,.9);
s = regionprops(BW1, 'centroid');
centroids = cat(1, s.Centroid);
axis tight
imagesc(BW)
hold on
plot(centroids(:,1), centroids(:,2), 'b*')
hold off
impixelinfo
centroids=round(centroids);
indr=sub2ind(size(BW1),centroids(:,2),centroids(:,1));
pixplane=size(BW1,1)*size(BW1,2);
indg=indr+pixplane;
indb=indg+pixplane;
center=reshape(BW([indr;indg;indb]),[],3);
[r c z]=size(BW);
rowcent=round(r/2);
colcent=round(c/2);
centc=centroids(1);
centr=centroids(2);
nr= (rowcent - centr);
nc = (colcent-centc);
imagesc(BW);
imz=zeros(size(BW));
mskr=(1:r)-nr;
ix=find((mskr>0)&(mskr<=r));
mskr=mskr(ix);
mskc=(1:c)-nc;
ix=find((mskc>0)&(mskc<=c));
mskc=mskc(ix);
r1=max([1,nr+1]);
r2=min([r,nr+r]);
c1=max([1,nc+1]);
c2=min([c,nc+c]);
imz(r1:r2,c1:c2,:)=BW(mskr(1):mskr(end),mskc(1):mskc(end),:);
imz=im2double(imz);
imagesc(imz);
Thanks again!
  1 Comment
Image Analyst
Image Analyst on 21 Feb 2013
Edited: Image Analyst on 21 Feb 2013
If you upload your image, we can run your code. Also, include the "imshow()" line of code where the error occurs (you did not include that code). The only display code I see is imagesc(), not imshow() like you said. Also, copy and paste the exact error message - don't paraphrase it, that's not helping.

Sign in to comment.

Answers (0)

Categories

Find more on Images 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!