In [2]:
import matplotlib.pyplot as plt
import numpy as np
from skimage import io,color,data,filters,exposure,util,transform

otsu


In [3]:
im=data.coins()
plt.imshow(im)


Out[3]:
<matplotlib.image.AxesImage at 0x7f3f53e39ef0>

In [4]:
f=np.zeros(255)
minf=0
mini=0
for i in range(100,200):
    c1=im[im<=i]
    c2=im[im>i]
    m1=np.mean(c1)
    m2=np.mean(c2)
    std1=np.std(c1)
    std2=np.std(c2)
    std3=np.std([m1,m2])
    f[i]=std3/(1+std1*std2)
    if f[i] > minf :
        minf=std3
        mini=i

plt.imshow(np.uint8(im>mini)*255)


Out[4]:
<matplotlib.image.AxesImage at 0x7f3f52566e10>

line detect


In [5]:
im=data.text()
plt.imshow(im)
seg=im<100
plt.imshow(seg)


Out[5]:
<matplotlib.image.AxesImage at 0x7f3f52553cc0>

In [21]:
r=transform.radon(seg)
rho,theta=np.unravel_index(np.argmax(r),r.shape)
rho=rho-r.shape[0]/2
x=np.int(rho*np.cos((theta+90)*np.pi/180)+im.shape[0]/2)
y=np.int(rho*np.sin((theta+90)*np.pi/180)+im.shape[1]/2)
dx=np.cos((theta)*np.pi/180)
dy=np.sin((theta)*np.pi/180)

l=1000
res=im.copy()
for l in range(1000):
    n=np.int64(y+dy*l)
    m=np.int64(x+dx*l)
    if m>0 and n>0 and m< seg.shape[0] and n< seg.shape[1]:
        res[m:m+3,n:n+3]=255
    else:
        break
for l in range(1000):
    n=np.int64(y+dy*l*-1)
    m=np.int64(x+dx*l*-1)
    if m>0 and n>0 and m< seg.shape[0] and n< seg.shape[1]:
        res[m:m+3,n:n+3]=0
    else:
        break
#cv2.line(res,(np.int(y-dy*l),np.int(x-dx*l)),(np.int(y+dy*l),np.int(x+dx*l)),255,2)
plt.imshow(res)


/home/xing/.local/lib/python3.6/site-packages/skimage/transform/radon_transform.py:58: UserWarning: The default of `circle` in `skimage.transform.radon` will change to `True` in version 0.15.
  warn('The default of `circle` in `skimage.transform.radon` '
Out[21]:
<matplotlib.image.AxesImage at 0x7f3f51864d68>