Error with image analysis

조회 수: 1 (최근 30일)
Frank
Frank 2011년 5월 18일
Hello!
I've been trying to get this script working, but I don't know how. What it should be able to do is make the image black and white and be able to pick out the centroid of the object in the image.
Image
Code
i = imread('343A #1.tif');
j = size(i);
imtool(i);
for k = 1:j(1)
for l = 1:j(2)
if i(k,l,1) > 160 & i(k,l,2) > 160
d(k,l) = 1;
else
d(k,l) = 0;
end
end
end
imtool(d);
label = bwlabel(d);
imagesc(label);
[label,num] = bwlabel(d,4);
stats = regionprops(label,'basic');
stats(1).Area
stats(1).Centroid
max_area = max([stats.Area]);
biggrain = find([stats.Area]== max_area);
stats(biggrain).Centroid
Error
??? Attempted to access i(2,51,2); index out of bounds because size(i)=[256,64,1].
Error in ==> TestingCentroid3 at 10 if i(k,l,1) > 160 & i(k,l,2) > 160
  댓글 수: 1
Frank
Frank 2011년 5월 18일
I've changed the image format to a tiff file to suit my problem better, however I am still receiving the same error.

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

답변 (2개)

Sean de Wolski
Sean de Wolski 2011년 5월 18일
i is apparently a 2d image. what is j (size(i))?
d = I > 160; %don't name your variables i,j as they are the sqrt(-1)
  댓글 수: 2
Frank
Frank 2011년 5월 18일
j = size(i); % compute the size of the ball
Sean de Wolski
Sean de Wolski 2011년 5월 18일
I meant what were the values...

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


Frank
Frank 2011년 5월 18일
Got it.
clc;
close all;
i = imread('343c001.tif');
j = size(i);
imtool(i);
for k = 1:j(1)
for l = 1:j(2)
if i(k,l,1) > 160 & i(k,l) > 160 %I removed the 2
d(k,l) = 1;
else
d(k,l,1) = 0;
end
end
end
imtool(d);
label = bwlabel(d);
imagesc(label);
[label,num] = bwlabel(d,4);
stats = regionprops(label,'basic');
stats(1).Area
stats(1).Centroid
max_area = max([stats.Area]);
biggrain = find([stats.Area]== max_area);
stats(biggrain).Centroid
  댓글 수: 2
Sean de Wolski
Sean de Wolski 2011년 5월 18일
i(k,l,1) is the same location as i(k,l).
hence instead of both for-loops you could do
d = i>160;
Sean de Wolski
Sean de Wolski 2011년 5월 18일
And I'll repeat: Don't name your variables i,j!

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

카테고리

Help CenterFile Exchange에서 Import, Export, and Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by