#!/usr/bin/env python # coding: utf-8 #
#
ESF projekt Západočeské univerzity v Plzni reg. č. CZ.02.2.69/0.0/0.0/16 015/0002287
# In[1]: get_ipython().run_line_magic('pylab', 'inline --no-import-all') # In[2]: import scipy import scipy.misc from scipy import stats import numpy as np import urllib import skimage import skimage.color import skimage.io import skimage.exposure import skimage.data import matplotlib.pyplot as plt from skimage import data from skimage import transform as tf # # Gray-scale image # In[3]: # URL = "http://uc452cam01-kky.fav.zcu.cz/snapshot.jpg" URL = "http://www.chmi.cz/files/portal/docs/meteo/kam/pribram.jpg" # URL = "http://plzen.cz/kamera.php?0.8989779513794929" # In[4]: img = skimage.io.imread(URL, as_gray=False) plt.imshow(img) img.shape # In[5]: img.shape # In[6]: type(img) # In[7]: img.dtype # In[ ]: # In[8]: dir(img) # In[9]: np.min(img) # In[10]: # from skimage import color imggray = skimage.color.rgb2gray(img) plt.imshow(imggray, cmap='gray') plt.colorbar() # In[12]: plt.imshow(imggray, vmin=0, vmax=1, cmap="Spectral") plt.colorbar() # # Histogram # In[14]: data = np.random.random([3,2]) print(data) print(' ') print(data.ravel()) # In[15]: np.min(img) img.shape # In[28]: a, b, c = plt.hist(img.ravel(), 40, density=False) # In[29]: a, b, c = plt.hist(img.ravel(), 40, cumulative=True, density=False) # In[22]: a, b, c = plt.hist(img[::2,::2].ravel(), bins=[0,50,100,110,120,130,200,255], # normed=False ) # In[35]: a, b, c = plt.hist(img[::2,::2].ravel(), 10) # In[31]: img.flatten() img[::2,::2].ravel() import timeit timeit.timeit('import numpy as np; img = np.random.random([640,480]); img.ravel()', number=100) # timeit.Timer('for i in xrange(10): oct(i)', 'gc.enable()').timeit() # In[32]: timeit.timeit('import numpy as np; img = np.random.random([640,480]); img.flatten()', number=100) # In[36]: print(a.shape) print(b.shape) plt.plot(b[:-1], a, '*g--') # In[80]: # import cv2 import numpy as np from matplotlib import pyplot as plt import scipy import scipy.misc import urllib import matplotlib.pyplot as plt # scipy.misc.imread( URL = "http://uc452cam01-kky.fav.zcu.cz/snapshot.jpg" img = skimage.io.imread(URL) imgg = skimage.color.rgb2gray(img) plt.imshow(imggray, cmap='gray') plt.show() a, b, c = plt.hist(imgg.ravel(),255) # # Intensity manipulation # # ## Lightening # In[81]: plt.figure() plt.imshow(imgg, cmap='gray', clim=(0.0, 1.0)) plt.figure() plt.imshow(imgg + 0.5, cmap='gray', clim=(0.0, 1.0)) # plt.colorbar() plt.show() # ## Data type overflow # In[98]: img.dtype print(f"min: {np.min(img)}, max: {np.max(img)}") plt.figure(figsize=[15,10]) plt.subplot(221) plt.hist(img.ravel(), bins=20) img2 = img + 50 plt.subplot(223) plt.hist(img2.ravel(), bins=20) print(f"min: {np.min(img2)}, max: {np.max(img2)}") plt.subplot(222) plt.imshow(img) plt.subplot(224) plt.imshow(img2) # ## Contrast - Intensity rescale # # [doc skimage.exposure_rescale_intensity](https://scikit-image.org/docs/dev/api/skimage.exposure.html#skimage.exposure.rescale_intensity) # In[99]: # img = skimage.color.rgb2gray(skimage.data.astronaut()) im = skimage.data.astronaut() print(stats.describe(im, axis=None)) plt.imshow(im) im_rescal = skimage.exposure.rescale_intensity(im, in_range=(0,255), out_range=(30,100)) plt.figure() plt.imshow(im_rescal) # # Rotation and scale # In[103]: print(imgg[:5,:5]) imgg05 = imgg*0.5 print(imgg05[:5,:5]) plt.imshow(imgg05, cmap='gray', vmax=1, vmin=0) plt.show() # In[106]: imr = skimage.transform.rotate(imgg, 30) plt.imshow(imr, cmap='gray') plt.show() # In[105]: imgg_res = scipy.misc.imresize(imgg, 0.1) plt.imshow(imgg_res, cmap='gray') plt.show() # ## Transform matrix # # ![Affine](https://upload.wikimedia.org/wikipedia/commons/thumb/2/2c/2D_affine_transformation_matrix.svg/449px-2D_affine_transformation_matrix.svg.png) # # [Transformation matrix on wiky](https://en.wikipedia.org/wiki/Affine_transformation) # In[4]: lena = skimage.io.imread("https://i.stack.imgur.com/3T6Gc.jpg") # In[108]: theta = np.pi/16 mat = np.array( [[1.5, 0, 0], [0, 1.5, 0], [0, 0, 1]] ) tform0 = tf.ProjectiveTransform(matrix=mat) warped = tf.warp(lena, tform0) fig, ax = plt.subplots(nrows=2, figsize=(10, 8)) ax[0].imshow(lena, cmap=plt.cm.gray) ax[1].imshow(warped, cmap=plt.cm.gray) for a in ax: a.axis('off') plt.tight_layout() plt.show() # In[61]: theta = np.pi/16 mat = np.array( [[np.cos(theta), -np.sin(theta), 0], [np.sin(theta), np.cos(theta), 0], [0, 0, 1]] ) tform0 = tf.ProjectiveTransform(matrix=mat) warped = tf.warp(lena, tform0) #, output_shape=(600, 600)) fig, ax = plt.subplots(nrows=2, figsize=(8, 8)) ax[0].imshow(lena, cmap=plt.cm.gray) ax[1].imshow(warped, cmap=plt.cm.gray) for a in ax: a.axis('off') plt.tight_layout() plt.show() # ## Transformation matrix parameters detection # In[9]: text = data.text() src = np.array([[0, 0], [0, 50], [300, 50], [300, 0]]) dst = np.array([[155, 15], [65, 40], [260, 130], [360, 95]]) tform3 = tf.ProjectiveTransform() tform3.estimate(src, dst) warped = tf.warp(text, tform3, output_shape=(50, 300)) fig, ax = plt.subplots(nrows=2, figsize=(8, 3)) ax[0].imshow(text, cmap=plt.cm.gray) ax[0].plot(dst[:, 0], dst[:, 1], '.r') ax[1].imshow(warped, cmap=plt.cm.gray) for a in ax: a.axis('off') plt.tight_layout() plt.show() # In[11]: tform3.params # ## Usefull tool # In[11]: import matplotlib.pylab as plt import skimage.io get_ipython().run_line_magic('matplotlib', 'qt5') plt.imshow(lena) uu = plt.ginput(n=4) # plt.show() uu # # Thresholding # In[11]: imthr = imggray > 0.5 plt.imshow(imthr, cmap='gray') # # Image counting # In[ ]: import skimage.measure imlabel = skimage.measure.label(imthr, background=0) np.max(imlabel) + 1