Effacer les filtres
Effacer les filtres

Assigning values ta set of points in 2d image

9 vues (au cours des 30 derniers jours)
Suhas Deshpande
Suhas Deshpande le 16 Juil 2011
After performing an operation on an image I get a set of points, the Cartesian coordinates are stored in a variable 'xy' of dimension n by 2. So xy(:,1) is x coordinate of each point and xy(:,2)is the y coordinate.
I want to create a new image of the same dimensions as the original image with these set of points being 1 and other points 0.
Let me create a problem to explain it further.
A = magic(10);
[x,y] = find(A<=20);
xy = [x,y];
B = zeros(size(A));
% now assign B as 1 for all points A<=20
Please help with the assignment command.
B(x,y) doesn't work i would like to know why ?

Réponse acceptée

Image Analyst
Image Analyst le 17 Juil 2011
Then just do it this way:
A = magic(10)
[y,x] = find(A<=20);
% Note: y = rows, x = cols
B = zeros(size(A));
linearInd = sub2ind(size(A), y, x)
B(linearInd) = 1

Plus de réponses (2)

Image Analyst
Image Analyst le 17 Juil 2011
Why not just simply say
B = A<=20;
You can cast B from logical to another data type after that if you want.
  1 commentaire
Suhas Deshpande
Suhas Deshpande le 17 Juil 2011
No Because in the real problem it is not as easy as that. The problem i have put forward here is just a random one which will help you guys answer my question better. I have got coordinates using an algorithm i cannot discuss here. And i have to use the 'm by 2 matrix' to assign the new variable.

Connectez-vous pour commenter.


Vishwajith Upendra
Vishwajith Upendra le 17 Juil 2011
for i=1:1:numel(x)
B(x(i),y(i))=1
end
This should do it. Number of elements in x and y are gonna be the same. So, took one of them.
Hope, that helped.
  1 commentaire
Suhas Deshpande
Suhas Deshpande le 17 Juil 2011
I am trying to avoid any loops to improve the code performance, but this works. But I think it should be possible without a loop.
Thanks anyways

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by