Basic Image Processing using the Python program
Transcript of Basic Image Processing using the Python program
Basic Image Processing using the Python program
โดย อาจารยย์โอฬารริก สสุรรินตต๊ะJan 19, 2017
Preparing Linux Systems for Installation• OpenCv 2.4
–http://opencv.org/• Scikit-image
–http://scikit-image.org/• Scikit-learn
–http://scikit-learn.org/
OpenCV: Installation in Linux• Following this link
–http://www.samontab.com/web/2014/06/installing-opencv-2-4-9-in-ubuntu-14-04-lts/
OpenCV: Installation in Linux$ sudo apt-get update$ sudo apt-get upgrade
# You need to install many dependencies librariws.$ sudo apt-get install build-essential libgtk2.0-dev libjpeg-dev libtiff4-dev libjasper-dev libopenexr-dev cmake python-dev python-numpy python-tk libtbb-dev libeigen3-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev libqt4-dev libqt4-opengl-dev sphinx-common texlive-latex-extra libv4l-dev libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev default-jdk ant libvtk5-qt4-dev
OpenCV: Installation in Linux• In Linux 16.xx You may get any problem with a library, such as “libtiff4-dev” –Change “libtiff4-dev” to “libtiff5-dev”
OpenCV: Installation in Linux• Time to get the OpenCV 2.4.9 source code:
$ cd ~$wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.9/opencv-2.4.9.zip $ unzip opencv-2.4.9.zip$ cd opencv-2.4.9
OpenCV: Installation in Linux• We have to generate the Makefile by using cmake.
$ mkdir build$ cd build$ cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_VTK=ON ..
OpenCV: Installation in Linux• Now, you are ready to compile and install OpenCV 2.4.9
$ make$ sudo make install
OpenCV: Installation in Linux• Configure OpenCV.$ sudo nano /etc/ld.so.conf.d/opencv.cof• Add the following line at the end of the file and then save it.
/usr/local/lib• Run the followin code to configure the library
$ sudo ldconfig
OpenCV: Installation in Linux• Open another file$ sudo nano /etc/bash.bashrc
• Add these two lines at the end of the file and save it.
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfigexport PKG_CONFIG_PATH
Checking your OpenCV version using Python$ python Python 2.7.6 (default, Oct 26 2016, 20:32:47) [GCC 4.8.4] on linux2
>>> import cv2>>> cv2.__version__'2.4.8'
Scikit-image: Installation in Linux• Building from source$ sudo pip install scikit-image #or$ sudo pip install -U scikit-image
• If you have any problem with “pip” $ sudo apt-get install python-pip$ sudo pip install –upgrade pip
Checking your Scikit-image version>>> python>>> import skimage>>> print('The scikit-image version is {}.'.format(skimage.__version__))The scikit-image version is 0.9.3.
Scikit-image: First programfrom skimage import data, io, filters
image = data.coins()edges = filters.sobel(image)io.imshow(edges)io.show()
#so, if you have any problem please try another way.#ถถ้ายยังไมม่สามารถแสดงรรูปไดถ้ หรรือยยังตติดปยั ญาททที่ filters ใหถ้ลองดรูวติธทตม่อไปนทน
Scikit-image: First programfrom skimage import data, io, filter
image = data.coins()edges = filter.sobel(image)io.imshow(edges)io.show()
This is because, the version of the scikit-image is out of date.
Scikit-image: update$ sudo pip install -U scikit-image
Scikit-learn: Installation in Linux$ sudo pip install -U scikit-learn
Checking your Scikit-learn version>>> python>>> import sklearn>>> print('The scikit-learn version is {}.'.format(sklearn.__version__))The scikit-learn version is 0.17.1.
Scikit-learn: First program$ python>>> from sklearn import datasets>>> iris = datasets.load_iris()>>> digits = datasets.load_digits()>>> print(digits.data)[[ 0. 0. 5. ..., 0. 0. 0.] [ 0. 0. 0. ..., 10. 0. 0.] [ 0. 0. 0. ..., 16. 9. 0.] ..., [ 0. 0. 1. ..., 6. 0. 0.] [ 0. 0. 2. ..., 12. 0. 0.] [ 0. 0. 10. ..., 12. 1. 0.]]
Scikit-learn: First program>>> digits.images[0]array([[ 0., 0., 5., 13., 9., 1., 0., 0.], [ 0., 0., 13., 15., 10., 15., 5., 0.], [ 0., 3., 15., 2., 0., 11., 8., 0.], [ 0., 4., 12., 0., 0., 8., 8., 0.], [ 0., 5., 8., 0., 0., 9., 8., 0.], [ 0., 4., 11., 0., 1., 12., 7., 0.], [ 0., 2., 14., 5., 10., 12., 0., 0.], [ 0., 0., 6., 13., 10., 0., 0., 0.]])
Showing Image with Different Library: OpenCV#!/usr/bin/python
import cv2image = 'image/lena.png'img = cv2.imread(image)
cv2.imshow('image', img)cv2.waitKey(0);cv2.destroyAllWindows()
Showing Image with Different Library: Skikit-Image#!/usr/bin/python
from skimage import data, io, filter
image = data.coins()io.imshow(image)io.show()
Showing Image with Different Library: Matplotlibimport matplotlib.pyplot as pltimport matplotlib.image as mpimgimport numpy as np
imgName = 'image/lena.png'img = mpimg.imread(imgName)plt.imshow(img)<matplotlib.image.AxesImage object at 0xb3b8cacc>plt.show()
Showing Image with Different Library: Matplotlibimport matplotlib.pyplot as imshowimport numpy as npfrom PIL import Image
imgName = 'image/lena.png'pil_im = Image.open(imgName,'r')pil_im.show()
Arithmetic Operations on Images• Learn several arithetic operations on images like addition, subtraction, bitwise operations etc.
• You will learn these functions: cv2.add(), cv2.addWeighted(), etc.
Arithmetic Operations on Images• Image Addition• Image Blending• Bitwise Operations
Image Addition• You can add two images by OpenCV function cv2.add() or simple by numpy operation, res = img1 + img2.
• Both images should be of same depth and type, or second image can just be a scalar value.
Image Addition>>> import cv2>>> import numpy as np>>> x = np.uint8([250])>>> y = np.uint8([10])
>>> print cv2.add(x,y) [[255]] # 250 + 10 = 260 ==> 255>>> print x+y[4] # 250 + 10 = 260 % 256 = 4
How do I check the variable type>>> x = np.array([1, 2])>>> print x.dtypeint32>>> x = np.uint8([250])>>> print x.dtypeUint8>>> x = np.array([1, 2], dtype=np.int64)int64
How do I check the variable type>>> import numpy as np>>> np.arange(10000)array([ 0, 1, 2, ..., 9997, 9998, 9999])>>> np.set_printoptions(threshold=np.inf)>>> x = np.arange(10000)>>> print x
Image Blending• This is also image addition, but different weights are given to images so that it gives a feeling of blending or transparency.
• Images are added as per the equation below:
Image Blending: image size: 512x512
Image Blending: Example#!/usr/bin/python
import cv2import numpy as np
img = cv2.imread(imgName)img1 = cv2.imread('image/watermark.png')alpha = 0.8beta = (1.0 - alpha)dst = cv2.addWeighted(img, alpha, img1, beta, 0.0, 0)
cv2.imshow('blend',dst)cv2.waitKey(0)cv2.destroyAllWindows()
Image Blending: result• Try to change a parameter to another values.
Bitwise Operations• This includes bitwise AND, OR, NOT and XOR operations.
• They will highly useful while extracting any part of the image, defining and working with non-rectangular ROI etc.
Bitwise Operations: example#!/usr/bin/python
import cv2 # Using OpenCVimport numpy as np
# Load an color imageimgName = 'image/lena.png'imgWatermark = 'image/watermark-small.png'img1 = cv2.imread(imgName)img2 = cv2.imread(imgWatermark)
# want to put watermark on top-left corner, then create a ROI (Region of Interest)rows,cols,channels = img2.shaperoi = img1[0:rows, 0:cols]
Bitwise Operations: example# Now create a mask of watermark and create its #inverse mask alsoimg2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)ret, mask = cv2.threshold(img2gray, 10, 255, \ cv2.THRESH_BINARY)mask_inv = cv2.bitwise_not(mask)
# Now black-out the area of logo in ROIimg1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)
Bitwise Operations: example# Take only region of logo from logo image.img2_fg = cv2.bitwise_and(img2,img2,mask = mask)
# Put logo in ROI and modify the main imagedst = cv2.add(img1_bg, img2_fg)img1[0:rows, 0:cols] = dst
cv2.imshow('res',img1)cv2.waitKey(0)cv2.destroyAllWindows()
Bitwise Operations
Trick: Connect to web camera• Using VLC player to capture device such as web camera.–VLC: Media >> Open Capture Device–Capture Device tap: Select Video device name from a list (/dev/video0)
–Then, click at “play” button
Trick: Connect to web cameraimport numpy as npimport cv2
cap = cv2.VideoCapture(0) # (/dev/video0)while (True): # Capture frame-by-frame ret, frame = cap.read()
# Our operations on the frame come here gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame cv2.imshow('frame',gray) if cv2.waitKey(1) & 0xFF == ord('q'): break
# When everything done, release the capturecap.release()cv2.destroyAllWindows()
Trick: Connect to web camera
• https://media.readthedocs.org/pdf/opencv-python-tutroals/latest/opencv-python-tutroals.pdf•
References• http://biop.epfl.ch/pdf/Basic%20Image%20Processing.pdf
• http://matplotlib.org/users/image_tutorial.html
• http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_core/py_image_arithmetics/py_image_arithmetics.html
• http://cs231n.github.io/python-numpy-tutorial/#numpy-datatypes
• https://media.readthedocs.org/pdf/opencv-python-tutroals/latest/opencv-python-tutroals.pdf
•