from IPython.display import Image as imdisplay
imfile = 'sanpe300.png'
imdisplay(imfile)
from PIL import Image
from pprint import pprint
im = Image.open(imfile)
print(type(im))
print(im.getbands())
pprint(im.info)
import numpy as np
imarray = np.array(im)
imarray.shape
import xarray as xr
inch_mm = 25.4
def im2DataArray(im):
imarray = np.array(im)
dpi = im.info['dpi']
xmax_mm, ymax_mm = im.width/dpi[0]*inch_mm,im.height/dpi[1]*inch_mm
xs = np.linspace(0,xmax_mm,im.width)
ys = np.linspace(0,ymax_mm,im.height)
da = xr.DataArray(imarray,
coords = dict(y=ys,x=xs,channel = list(im.getbands())),
dims = ['y','x','channel'],
name='value')
return da
da = im2DataArray(im)
# to lower the memory footprint of imview () in some part of the notebook, I also
da_low = im2DataArray(Image.open('sanpe150.png'))
from IPython.core.display import display, HTML
from io import StringIO
import holoviews as hv
def show(h):
f = StringIO()
hv.renderer('bokeh').save(h,f,fmt = 'html')
display(HTML(f.getvalue()))
import hvplot.xarray
imview = da_low.hvplot.rgb('x','y','value',bands = 'channel',flip_yaxis=True,data_aspect=1)
show(imview)
import holoviews as hv
xmin,ymin,xmax,ymax = box = (22,65,32,75)
square = hv.Rectangles(data=box).opts(fill_alpha=0.1)
crop = da.sel(x=slice(xmin,xmax),y=slice(ymin,ymax),channel = ['R','G','B'])
cropview = crop.hvplot.rgb('x','y','value',bands = 'channel',flip_yaxis=True,data_aspect=1)
show((imview*square+cropview).opts(shared_axes=False).cols(1))
cropopts = dict(colorbar=False,cmap='gray',xaxis=None,yaxis=None,data_aspect=1,flip_yaxis=True)
show(crop.hvplot.image('x','y',**cropopts).layout('channel'))
hist = crop.hvplot.hist('value',by='channel',alpha=0.5)
show(hist)
boxes = dict()
boxes[0], boxes[1] = (22,65,32,75),(22,55,32,65)
square0 = hv.Rectangles(data=boxes[0]).opts(fill_alpha=0.1)
square1 = hv.Rectangles(data=boxes[1]).opts(fill_alpha=0.1)
selection = (imview*square0*square1)
show(selection)
def boxes2crop(boxes):
crops = dict()
for key,box in boxes.items():
xmin,ymin,xmax,ymax = box
sel = dict(x=slice(xmin,xmax),y=slice(ymin,ymax),channel = ['R'])
crops[key] = da.sel(**sel).squeeze()
return crops
crops = boxes2crop(boxes)
layout = hv.Layout([crop.hvplot.image('x','y',**cropopts,shared_axes=False)
for k,crop in crops.items()])
show(layout)
from skimage.registration import phase_cross_correlation
shift,_,_ = phase_cross_correlation(crops[0].data,crops[1].data,upsample_factor=10)
shift
from skimage.transform import SimilarityTransform, warp
t = SimilarityTransform(translation = (11.1,0))
h0 = hv.Image(crops[0].data).opts(tools = ['hover'])
im1translated = warp(crops[1].data,t,preserve_range=True)
h1translated = hv.Image(im1translated).opts(tools = ['hover'])
delta = crops[0].data-im1translated
hdelta = hv.Image(delta).opts(tools = ['hover'])
show((h0+h1translated+hdelta).cols(2))
10*300/inch_mm, 11.1*inch_mm/300
translation_unit = np.array([-11.1,118.11])/16
translation_unit
vignette = da.sel(channel='R').data
ims = []
for k in range(16):
translation = k*translation_unit
t = SimilarityTransform(translation = translation)
ims.append(warp(vignette,t,preserve_range=True))
im_median = np.median(np.array(ims),axis=0)
Image.fromarray(im_median>128)
boxes = dict()
boxes[0], boxes[1] = (22,65,32,75),(34.085,40.915,44.085,50.915)
square0 = hv.Rectangles(data=boxes[0]).opts(fill_alpha=0.1)
square1 = hv.Rectangles(data=boxes[1]).opts(fill_alpha=0.1)
selection = (imview*square0*square1)
show(selection)
transl = np.array([-12.085,24.085])
vnorm = np.linalg.norm(transl)
angle = np.arccos(transl[1]/vnorm)
print('norm:',vnorm)
print('angle:',angle*180/np.pi)
14./10.*vnorm/np.cos(angle),14.5/10.*vnorm/np.cos(angle)
translation_unit2 = transl/43*300/inch_mm
ims = []
for k in range(43):
translation = k*translation_unit2
t = SimilarityTransform(translation = translation)
ims.append(warp(vignette,t,preserve_range=True))
im_median2 = np.median(np.array(ims),axis=0)
Image.fromarray(im_median2>128)
from skimage import measure
crop = (im_median<128)[530:600,300:1000]
labels = measure.label(crop)
hlabel = hv.Image(labels).opts(cmap='nipy_spectral',data_aspect=0.1,frame_height=150)
show(hlabel)
subcrop = ((labels==1)*255).astype('uint8')
Image.fromarray(subcrop)
def repeat(im,translation_unit,k_repeat):
ims = []
for k in range(-10,k_repeat):
translation = k*translation_unit
t = SimilarityTransform(translation = translation)
ims.append(warp(im,t,preserve_range=True))
return np.sum(np.array(ims),axis=0)
curveblack = np.concatenate((np.zeros((300,700)),subcrop)).astype('uint8')
curve_sum = repeat(curveblack,translation_unit,40)
Image.fromarray(curve_sum.astype('uint8'))
lineblack = np.zeros(curve_sum.shape)
lineblack[131:133,:] = 255
rot = SimilarityTransform(rotation = -26.6*np.pi/180,translation=(0,100))
lineblack = warp(lineblack,rot,preserve_range = True)
line_sum = repeat(lineblack,translation_unit2,40)
Image.fromarray(line_sum.astype('uint8'))
Image.fromarray(~((curve_sum>128) | (line_sum>128)))