Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtain plot data from JPEG or png file

I'm writing a program that obtains plot data from a graph(JPEG). Vertical axis is logarithmic. I successfully made a program that understands horizontal and vertical axes as linear (not logarithmic), see the code below:

enter image description here

%matplotlib inline

from PIL import Image
from scipy import *
from pylab import *

im = array(Image.open('fig1.jpg'))

hh = im.shape[0]
ww = im.shape[2]

imshow(im)    
print(im[100,0,:])

Col = array([255,0,0])#赤
bnd = 30

yax = linspace(0.5,2e-4,hh)
xax = linspace(1,14,ww)

for i in range(hh):
    for j in range(ww):
        im[i,j,:] = 255*(any(im[i,j,:]>Col+bnd) or any(im[i,j,:]<Col-bnd))

mapim = abs(im[:,:,0]/255-1).astype(bool)

yval = array([average(yax[mapim[:,t]]) for t in range(ww)])

rlt = interp(range(100),xax,yval)

I have no idea how to modify it to make it understand logarithmic axis. Please help me.

like image 319
Atsushi Takahashi Avatar asked Oct 01 '16 13:10

Atsushi Takahashi


1 Answers

You just need to convert the y max and min to log scale:

ymax_lin = log10(0.5)
ymin_lin = log10(2e-4)

yax = linspace(ymax_lin,ymin_lin,hh)

and convert back to linear the yval values at the end:

yval = 10**yval

The full working code is here:

%matplotlib inline

from PIL import Image
from scipy import *
from pylab import *

im = array(Image.open('fig1.jpg'))

hh = im.shape[0]
ww = im.shape[1]

imshow(im)    
print(im[100,0,:])

Col = array([255,0,0])
bnd = 30


ymax_lin = log10(0.5)
ymin_lin = log10(2e-4)

yax = linspace(ymax_lin,ymin_lin,hh)
xax = linspace(1,14,ww)

for i in range(hh):
    for j in range(ww):
        im[i,j,:] = 255*(any(im[i,j,:]>Col+bnd) or any(im[i,j,:]<Col-bnd))

mapim = abs(im[:,:,0]/255-1).astype(bool)


yval = array([average(yax[mapim[:,t]]) for t in range(ww)])

yval = 10**yval

rlt = interp(range(100),xax,yval)
like image 95
Andrea Avatar answered Nov 12 '22 03:11

Andrea