Effacer les filtres
Effacer les filtres

Get pixel of data coordinate

25 vues (au cours des 30 derniers jours)
Jonathan Mole
Jonathan Mole le 29 Juil 2019
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

Réponses (3)

Image Analyst
Image Analyst le 30 Juil 2019
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 commentaires
Jonathan Mole
Jonathan Mole le 30 Juil 2019
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 le 30 Juil 2019
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?

Connectez-vous pour commenter.


Jonathan Mole
Jonathan Mole le 31 Juil 2019
imgetpixel does not work... neither does pixelvalue.
I used plot as for the example described above plot(2,2,'*')
  2 commentaires
Image Analyst
Image Analyst le 31 Juil 2019
Jonathan Mole
Jonathan Mole le 3 Août 2019
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.

Connectez-vous pour commenter.


Markus Lindblom
Markus Lindblom le 4 Sep 2020
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];

Catégories

En savoir plus sur Graphics Performance dans Help Center et File Exchange

Tags

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by