How can I store pixel position in two different array ?

1 回表示 (過去 30 日間)
Rajesh  Gothwal
Rajesh Gothwal 2013 年 6 月 21 日
コメント済み: KaMu 2013 年 12 月 10 日
Hi , I want to store the position of the pixel where Intensity value is 255. There are two arrays A and B for rows and columns respectively. I am using the below code, although code is error free but not returning expected result.
I=imread('img.jpg');
A=[];
B=[];
k=0;
for i=1:80
for j=1:120
if I(i, j)==255
A(k)=i;
B(k)=j;
end
k=k+1;
end
end
Image dimension is 80*120. Thanx.....
  2 件のコメント
Mark
Mark 2013 年 6 月 21 日
Just a side note... it appears that you are indexing k regardless of whether your criteria (I(i,j)==255) or not. This may be why the result is not expected? You could always look to use
A=cat(1,A,i) B=cat(1,B,j)
KaMu
KaMu 2013 年 12 月 10 日
Why dont u use find, it is very infromative: http://www.mathworks.com/help/matlab/ref/find.html

サインインしてコメントする。

採用された回答

Mark
Mark 2013 年 6 月 21 日
編集済み: Mark 2013 年 6 月 21 日
This should help...
I=imread('img.jpg'); %Assuming this is a gray-scale image
idx=find(I==255);
[B,A]=ind2sub(size(I),idx);
figure; imshow(I); hold on
plot(A,B,'r.');
hold off
  1 件のコメント
Rajesh  Gothwal
Rajesh Gothwal 2013 年 6 月 22 日
thanx... Eexpected result obtained

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2013 年 6 月 23 日
You need to have k=k+1 INSIDE your "if", not outside it. That's the cause of your problem.
Of course you can just get rows and columns directly from find() without using linear indexes and ind2sub, or a nested "for" loop:
[rows, columns] = find(I == 255);
rows is what you call A, and columns is what you call B. I encourage you to use more descriptive variable names, like I did. Also, don't use I as a variable name - it looks too much like 1 or l. Use a descriptive name like grayImage or binaryImage or something.

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by