Extract data from contour plot of .png file?

34 views (last 30 days)
Surendra Gupta
Surendra Gupta on 12 Oct 2020
Commented: jay shah on 2 Jun 2021
Hi,
I have the attached contour plot figure in .png format. I need to extract the data points form the plot (x,y,z)?
Would someone please help me with the same?
Thanks in advance.
  6 Comments
Rik
Rik on 12 Oct 2020
I hard-coded the coordinates for this example image. It looks like there is an issue with rescaling the data, but I will leave that for you to untangle. Note that this solution does not use the actual colorbar, but convert everything to grayscale first. That is probably done to avoid having to match the actual colors, which can be quite tricky for low resolution images like yours.
%// Import the data:
imdata = importdata('ir_image.png');
Gray = rgb2gray(imdata.cdata);
colorLim = [300 1220]; %// this should be set manually
%// Get the area of the data:
da_tp_lft = [25 13];
da_btm_rgt = [231 287];
dat_area = double(Gray(da_tp_lft(2):da_btm_rgt(2),da_tp_lft(1):da_btm_rgt(1)));
%// Get the area of the colorbar:
ca_tp_lft = [246 13];
ca_btm_rgt = [246 287];
cmap_area = double(Gray(ca_tp_lft(2):ca_btm_rgt(2),ca_tp_lft(1):ca_btm_rgt(1)));
%// Convert the colormap to data:
data = dat_area./max(cmap_area(:)).*range(colorLim)-abs(min(colorLim));
figure(1),clf(1)
imshow(data,[])
colorbar

Sign in to comment.

Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 12 Oct 2020
Edited: Bjorn Gustavsson on 12 Oct 2020
In case the conversion of the colormap to grayscale doesn't work well (i.e. multiple colours mapping to the same gray-level) I once had to solve this problem, and came up with a solution working reasonably well with the functions attached.
HTH
  3 Comments
Bjorn Gustavsson
Bjorn Gustavsson on 2 Nov 2020
Ok, the scan2D2data function expects an rgb-image as input, not a string with path to an image-file. So call it something like this:
imfilename = 'your-png-filename.png';
im_rgb = imread(imfilename);
[M_out] = scan2D2data(img_in);
Then the function will interactively query you about the colour-map and the range the data, and the image-region of the data.
HTH
jay shah
jay shah on 2 Jun 2021
Can you please explain in detail how to use the function? like how to select diagonal points and color map etc.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!