MATLAB: How to Automatically digitize a plot image

data importdigitizergraphsimageimread

% read in tiff image and convert it to double format
close all;
clear all
myimage = imread('wave.png');
% myimage = myimage(:,:,1);
% allocate space for thresholded image
image_thresholded = zeros(size(myimage));
% loop over all rows and columns
for ii=1:size(myimage,1)
for jj=1:size(myimage,2)
color=[myimage(ii,jj,1),myimage(ii,jj,2),myimage(ii,jj,3)];
cmax=max(color);
cmin=min(color);
if (cmax<200 && cmin >80)
new_pixel=0;
else
new_pixel=255;
end
image_thresholded(ii,jj)=new_pixel;
end
end
% figure()

% % subplot(1,2,1)
% imshow(myimage)
% title('original image')
% figure()
% % subplot(1,2,2)
% imshow(image_thresholded)
% title('thresholded image')
Data=[];
% Xmin=input('\nEnter Xmin:')
% Xmax=input('\nEnter Xmax:')
% Ymin=input('\nEnter Ymin:')
% Ymax=input('\nEnter Ymax:')
Xmin=0;Xmax=600;Ymin=-3000;Ymax=3000;
for ii=1:size(myimage,2)
for jj=5:size(myimage,1)
pixel=image_thresholded(jj,ii);
if pixel==0
Data=[Data;ii,size(myimage,2)-jj];
end
end
end
imxmax=max(Data(:,1));imxmin=min(Data(:,1));
imymax=max(Data(:,2));imymin=min(Data(:,2));
figure()
scatter(Data(:,1),Data(:,2))
title('Extacted Original Image Co-ordinate data')
%Scrapout Extra lines
Test=Data(Data(:,1)>imxmin+5,:);
Test=Test(Test(:,1)<imxmax-5,:);
Test=Test(Test(:,2)>imymin+5,:);
Test=Test(Test(:,2)<imymax-5,:);
X=Test(:,1);Y=Test(:,2);
% X=Data(:,1);Y=Data(:,2);
X1=[(X-imxmin)/(imxmax-imxmin)].*(Xmax-Xmin)+Xmin;
Y1=[(Y-imymin)/(imymax-imymin)].*(Ymax-Ymin)+Ymin;
XData=[(Data(:,1)-imxmin)/(imxmax-imxmin)].*(Xmax-Xmin)+Xmin;
YData=[(Data(:,2)-imymin)/(imymax-imymin)].*(Ymax-Ymin)+Ymin;
figure()
plot(X1,Y1);
hold on
scatter(XData,YData);
% title('Final Data extracted to plot scale')
Hi All I have tried to automatically extact the data from picture plot (attached image).
My idea is to extract the color of picture some how if the color of the pixel is with in the specific range store its x,y co-ordinates in the matrix.
Then use this matrix and transform the data in the matrix from image co-ordinates to plot co-ordinates.
I am relatively new to matlab coding so sorry for my rough syntax.
Now the problem is same image with minor variation is not able to transform the image co-ordinates.
Request you to help me understand where I am going wrong.
Thanks & Regards
Sriramakrishna Turaga

Best Answer

Well the plot doesn't have enough resolution to get all the data. For example on some of the noisier areas, there is a bunch of dark values for a single column. What do you want to pick? The top one? The bottom one? The average? All of them?
I'd just scan them looking for dark values
[rows, columns, numberOfColorChannels] = size(rgbImage)
grayImage = min(rgbImage, [], 3);
binaryImage = grayImage < 128;
topRows = zeros(1, columns);
bottomRows = zeros(1, columns);
meanRows = zeros(1, columns);
for col = 1 : columns
nonZeroRows = find(binaryImage(:, col));
if ~isempty(nonZeroRows)
topRows(col) = nonZeroRows(1);
bottomRows(col) = nonZeroRows(end);
meanRows(col) = mean(nonZeroRows)
end
end
% Now make x and y
x = 1 : columns;
y = topRows; % or bottomRows or meanRows, whatever you want.