Get pixel of data coordinate

조회 수: 29 (최근 30일)
Jonathan Mole
Jonathan Mole 2019년 7월 29일
답변: Markus Lindblom 2020년 9월 4일
I want to get the pixel value of a selected data point on a figure (.m matlab figure).
For example in figure; plot(2,2,'*')
what is the pixel associated with the (2,2) data point?
I do not wish to use imtool since I generate many figures inside the same code and I wish to put a '\rightarrow' at the (2,2) data point.
And, the axis changes for these figures so the (2,2) data point also changes its pixel values in my code.
Cheers

답변 (3개)

Image Analyst
Image Analyst 2019년 7월 30일
You can use getpixel() to get the pixel value, or use indexing, like pixelValue = yourImage(y, x, :).
To put an arrow into the overlay, use annotation().
  댓글 수: 2
Jonathan Mole
Jonathan Mole 2019년 7월 30일
Hi,
is getpixel a matlab function? And for the part pixelValue = yourImage(y, x, :), this does not seem to work. Maybe because the figure is a .m format?
Image Analyst
Image Analyst 2019년 7월 30일
Maybe it's imgetpixel(). Try that. But actually, I just use indexing all the time.
A figure cannot be an m-file. It can be generated by an m-file, but cannot be an m-file itself. You either
  1. used image(), imagesc(), pcolor(), or imshow() to display a digital image, OR
  2. you plotted something with plot(), scatter(), surf(), bar(), etc.
So, which was it?

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


Jonathan Mole
Jonathan Mole 2019년 7월 31일
imgetpixel does not work... neither does pixelvalue.
I used plot as for the example described above plot(2,2,'*')
  댓글 수: 2
Image Analyst
Image Analyst 2019년 7월 31일
Jonathan Mole
Jonathan Mole 2019년 8월 3일
I think that the correct answer to this would be: if you do not know the answer, it would be rather preferable not to comment.

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


Markus Lindblom
Markus Lindblom 2020년 9월 4일
Hi, I just stumbled across this thread. I recently had a similar problem and came up with an (extremely) crude solution. The followig code finds the pixel values of points with a specific color, if your plots are simple with distinct colors in them then it should work. I have for example reserved the color red for certain data points and avoid using similar colors such as orange and pink. The code basically saves the current figure as a JPG image and imports the pixel values in table form (a 3D-matrix called 'Idata'). It then tries to find distinct points (further apart than 5 image pixels) that have an RGB value as close to the desired as possible (in my case [255 0 0]). The resolution of my monitor is hardcoded into the script so you might have to play around with that in order to get it to work. I hope at least some of it helps you! Good luck /Markus
PS: it is worth noting that the java.robot program counts from the upper left corner (pixel: (1,1)) and down to the right corner (pixel: (1920,1080)) in "real" pixels. Why I say "real" is because matlab also counts some things in pixels, such as the position of a figure window, try typing: "hfig = gcf; hfig.Position" and you will get the current figure position in pixels. However matlab seems to assume the screen resolution is 1280x720 (2/3's of the "real"). Furthermore when the figure is saved as a JPG image the colors are combined to conform to the resolution standard that is inherent to JPG pictures. So try keeping in mind that "real" pixels, "Matlab" pixels and JPG pixels are not the same (in number)!
saveas(gcf,'temp_pic.jpg');
Idata = double(imread('temp_pic.jpg'));
c = [255,0,0];
cc = zeros(1,1,3);
cc(1,1,:) = c;
C = repmat(cc,size(Idata,1),size(Idata,2),1);
val = rms(C - Idata,3);
d = 5;
tempCoord = [];
count = 0;
while true
count = count + 1;
min_val = -inf;
while min_val < d
[min_val,row] = min(val);
[min_val,col] = min(min_val);
row = row(col);
val(row,col) = inf;
ok = 1;
for i = 1:size(tempCoord,1)
if norm([row,col]-tempCoord(i,:),2) < 5
ok = 0;
break
end
end
if ok
tempCoord = [tempCoord;[row,col]]; %#ok<*AGROW>
end
end
nop(count) = size(tempCoord,1);
d = d + 5;
n = 5;
if count > n
if nop(count)-nop(count-n+1) == 0
break
end
end
end
hfig = gcf;
width = hfig.Position(3);
height = hfig.Position(4);
width_factor = width/size(C(:,:,1),2);
height_factor = height/size(C(:,:,1),1);
pixel_row = height - round(tempCoord(:,1).*height_factor);
pixel_col = round(tempCoord(:,2).*width_factor);
Coord = [pixel_row,pixel_col];

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by