Gray value for each point plot over image

2 views (last 30 days)
Emre Kayahan
Emre Kayahan on 24 Mar 2020
Answered: Rik on 24 Mar 2020
Hi,
currently I am facing a question and hope that you guys could help me on that one.
I have plotted some datapoints (coordinates) over an image (Citymap of a city in Germany) and I do need to get the gray values for each point. The PNG is already converted to gray. I will attach you some screenshots, so that you are able to understand it. There are 808 points plotted in total. The file which is showing you the plot is called "MapPunkte" and also I am showing you the set of coordinates which are plotted named in the file "Punkte". Below you can see the code I already have written
clc
clear all
% Points to be plotted
load('Referenzpunkte.mat')
fig=figure('rend','painters','pos',[10 10 560*2*1.15 420*2])
img = imread('MapBW.png');
% Converting to gray
colormap(gray(256))
% Boundaries figure
min_x = 6.4037;
max_x = 6.7041;
min_y = 51.2852;
max_y = 51.4048;
imagesc([min_x max_x], [min_y max_y], flipud(img));
hold on
scatter(Punkte(:,1),Punkte(:,2),'filled');
% Set squares for each point
plot(Punkte(:,1),Punkte(:,2),'s','MarkerSize',24,'MarkerEdgeColor','r')
  2 Comments
Rik
Rik on 24 Mar 2020
Do you need the mean value of everything inside the square? Or just on the dot itself? In the latter case you could round Punkte and use that to index your image (use sub2ind first).
Emre Kayahan
Emre Kayahan on 24 Mar 2020
Hi, Rik!
Thank you very much for your comment. I do only need the gray value of where the dot itself is lying on.
Could you please explain it that a Matlab newbie like me would understand? This is literally my very first project with matlab :(
Thanks!

Sign in to comment.

Answers (1)

Rik
Rik on 24 Mar 2020
Since the Punkte array contains coordinates, you can convert them to indices. You need a way to translate the x and y positions to the positions in your image. I will leave that part of the correction for now. Let me know if you need help with that. Once you have converted Punkte to the image coordinates you can use the code below.
ind=sub2ind(size(img),round(Punkte(:,1)),round(Punkte(:,2)));
result=img(ind);

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!