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

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

Image Analyst
Image Analyst 2013년 2월 21일
편집: Image Analyst 2013년 2월 21일
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.

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

답변 (0개)

카테고리

도움말 센터File Exchange에서 Images에 대해 자세히 알아보기

질문:

2013년 2월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by