how to plot coordinates of nonzero elements from matrix

hello, i want to ask something,
i have this binary image :
here is my code :
global A;
global rx;
global ry;
A = handles.q; %#ok
A = [500, 5000]; %#ok
A = [450:500, ;]; %#ok
[rx, ry] = size(A);
for A = [:,1] > 0
[rx,ry] = find(A[:,1] > 0);
end
guidata(hObject,handles);
axes(handles.axes3); %#ok
imshow(A);
msgbox('Process SUCCESSFUL !');
my question is :
how to find and plot coordinates of nonzero elements from the image?
please tell me if my code is wrong or not..
Thanks

 採用された回答

KSSV
KSSV 2016 年 11 月 17 日
編集済み: KSSV 2016 年 11 月 17 日

0 投票

clc; clear all;
K = imread('your image') ;
K = K(:,:,1) ;
% K = randi([0 1],[100 100]) ; % 100x100 matrix
[r,c] = size(K) ;
x = 1:c ;
y = 1:r ;
[X,Y] = meshgrid(x,y) ;
% get indices of non zero
idx = (K>0) ;
% plot only ones
plot3(X(idx),Y(idx),K(idx),'.r')
view(2)

6 件のコメント

Evan Weking
Evan Weking 2016 年 11 月 17 日
ok i'll try, thanks
Evan Weking
Evan Weking 2016 年 11 月 17 日
i already try your code, and it does work
but it only works from different plot,
how can i put the code into my code above? in order i can execute it from GUI. Thanks
KSSV
KSSV 2016 年 11 月 17 日
Make it a function and call inside GUI...
Guillaume
Guillaume 2016 年 11 月 17 日
編集済み: Guillaume 2016 年 11 月 17 日
The whole of
[r,c] = size(K) ;
x = 1:c ;
y = 1:r ;
[X,Y] = meshgrid(x,y) ;
% get indices of non zero
idx = (K>0) ;
% plot only ones
plot3(X(idx),Y(idx),K(idx),'.r')
can be replaced by
[Y, X, V] = find(K > 0);
plot3(X, Y, V, '.r');
and since K is binary, even simpler:
[Y, X] = find(K);
plot3(X, Y, 1, '.r');
Evan Weking
Evan Weking 2016 年 11 月 18 日
ok thank you Guillaume for the code
your first code is working, but the second code is showing error like this :
??? Error using ==> plot3
Vectors must be the same lengths.
Error in ==> coba at 6
plot3(X, Y, 1, '.r');
do you know why it's happen?
thanks
Guillaume
Guillaume 2016 年 11 月 22 日
replace the 1 by ones(size(X)):
plot3(X, Y, ones(size(X)), '.r');

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

その他の回答 (0 件)

質問済み:

2016 年 11 月 17 日

コメント済み:

2016 年 11 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by