Copy object´s pixels to ones-matrix

1 回表示 (過去 30 日間)
Mariana
Mariana 2014 年 2 月 12 日
コメント済み: Image Analyst 2014 年 2 月 16 日
Hello, I try following (for example):
[r, c] = find(bwlabel(BW)==2)
but I´ve found out that "r" and "c" vectors are the same length and they (probably) represent some sort of frame around an object rather then object itself. I wanted to use this function for replacing one-elements (pixel with value = 1) in ones matrix (matrix filled with ones (sorry for poor english) with pixel values of object copied... sth like: image2(r, c) = image1 (r, c) but the problem is that the background is "copied" also (and is unwanted, I want to have only the object copied)
Can you give an advice what is wrong (whether it is) or which better function I could use?

採用された回答

Walter Roberson
Walter Roberson 2014 年 2 月 12 日
idx = bwlabel(BW) == 2;
newmatrix = ones(size(originalmatrix));
newmatrix(idx) = originalmatrix(idx);
Or, following your existing code more closely,
[r, c] = find(bwlabel(BW)==2);
newmatrix = ones(size(originalmatrix));
nematrix(sub2ind(size(newmatrix),r,c)) = originalmatrix(sub2ind(size(newmatrix),r,c));

その他の回答 (2 件)

Image Analyst
Image Analyst 2014 年 2 月 12 日
Another way using ismember()
labeledImage = bwlabel(BW);
twoValuedPixels = ismember(labeledImage, 2) > 0;
This gives a logical matrix use int32(twoValuedPixels) if you want int32 or double(twoValuedPixels) if you want a double.
  1 件のコメント
Image Analyst
Image Analyst 2014 年 2 月 16 日
Regarding your "Answer"...No problem. You almost never want a list of individual row and column numbers for every single binary pixel. See my Image Segmentation Tutorial, BlobsDemo: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862

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


Mariana
Mariana 2014 年 2 月 16 日
Very thanks :) I´m new to matlab, sorry if it was a silly question

カテゴリ

Help Center および File ExchangeFeature Detection and Extraction についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by