Image processing with Python (Part 1)

Until now, I used ImageJ, R, Matlab, etc. for image processing, but I tried something with Python (+ OpenCV).

Python installation

Since library dependencies are troublesome, I will enjoy installing anaconda with homebrew.

% brew cask install anaconda

For Windows, use chocolatey to do choco install anaconda3. After installing anaconda, install OpenCV using the anaconda package system.

% conda install --channel https://conda.anaconda.org/menpo opencv3

Start jupyter and check that OpenCV is installed. 20160830001.jpg

jupyter is too convenient. I want to install anaconda just for jupyter. I want to use it with perl and R.

Load and display images

The database is scott / tiger emp, and the human face image is Lenna.

In search of Lenna

First, download the image 4.2.04.tiff from SPI Image Database. In terms of copyright, it is written as "Scans of magazine pictures. Copyright belongs to original publisher or photographer." If you have installed image processing tools in advance, try find / usr / local -name ‘Lenna.tiff’ -type f -print, and it may be saved in some directory unexpectedly.

Image display

You can see how to load and display images using OpenCV by looking at OpenCV documentation. That's why I'll try it on jupyter notebook. In addition, cv2.waitKey (0) and then cv2.destroyAllWindows (), but this means waiting for key input for 0 milliseconds, and 0 seconds in this case means "no time specified", that is, "all the time" It seems to mean "wait for the next key input". In other words, the instruction to keep the image window open until there is some key input.

import numpy as np
import cv2
import os.path
lenna = "4.2.04.tiff"
if os.path.exists(lenna):
    img = cv2.imread(lenna)
    cv2.imshow("Lenna", img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

20160830002.jpg

If you want to know more about this Lenna, look at img.shape or img.size.

Image binarization

If you look at OpenCV Tutorial, you will find the code for binarization, so let's do it.

I tried to put a line to check if there is an image file by doing os.path.exiss ().

Also, as in the tutorial, matplotlib is used to display images.

import cv2
import numpy as np
import os.path
from matplotlib import pyplot as plt
lenna = "4.2.04.tiff"

if os.path.exists(lenna):
    img = cv2.imread(lenna,0)
    img = cv2.medianBlur(img, 5)
    ret, th1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
    th2 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
    th3 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THERSH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
    
    titles = ['Original Image', 'Global Thresholding (v = 127)', 'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
    images = [img, th1, th2, th3]

    for i in range(0, 4):
        plt.subplot(2, 2, i+1), plt.imshow(images[i], 'gray')
        plt.title(titles[i])
        plt.xticks([]), plt.yticks([])

    plt.show()

20160830003.jpg

Similarly, try binarization by the Otsu method. The code is almost unchanged.

import cv2
import numpy as np
import os.path
from matplotlib import pyplot as plt
lenna = "4.2.04.tiff"
if os.path.exists(lenna):
    img = cv2.imread(lenna,0)
    img = cv2.medianBlur(img,5)
    ret,th = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    cv2.imshow("Lenna", th)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

20160830004.jpg

That's why I was able to do the basics for the time being.

This code

Recommended Posts

Image processing with Python (Part 1)
Image processing with Python (Part 3)
Image processing with Python
[Python] Image processing with scikit-image
python image processing
Image processing with Python 100 knocks # 3 Binarization
Image processing with Python 100 knocks # 2 Grayscale
Basics of binarized image processing with Python
Image processing with Python 100 knock # 10 median filter
Image processing with MyHDL
Image processing with Python 100 knocks # 8 Max pooling
First Python image processing
100 Language Processing Knock with Python (Chapter 2, Part 2)
Image processing with Python 100 knock # 12 motion filter
100 Language Processing Knock with Python (Chapter 2, Part 1)
Drawing with Matrix-Reinventor of Python Image Processing-
Easy image processing in Python with Pillow
Image processing with Python 100 knocks # 7 Average pooling
Light image processing with Python x OpenCV
Image processing with Python 100 knocks # 9 Gaussian filter
Image Processing with PIL
Image processing from scratch with python (5) Fourier transform
Image processing from scratch with python (4) Contour extraction
Image Processing with Python Environment Setup for Windows
100 Language Processing with Python Knock 2015
Studying Python with freeCodeCamp part1
Bordering images with python Part 1
Image processing with PIL (Pillow)
"Apple processing" with OpenCV3 + Python3
Scraping with Selenium + Python Part 1
Image editing with python OpenCV
Acoustic signal processing with Python (2)
Acoustic signal processing with Python
Sorting image files with Python (3)
Studying Python with freeCodeCamp part2
Tweet with image in Python
Sorting image files with Python
Solving Sudoku with Python (Part 2)
Image processing by python (Pillow)
Image Processing Collection in Python
Scraping with Selenium + Python Part 2
Notes on HDR and RAW image processing with Python
Cut out an image with python
Real-time image processing basics with opencv
Playing handwritten numbers with python Part 1
[Python] Using OpenCV with Python (Image Filtering)
[Python] Easy parallel processing with Joblib
[Automation with python! ] Part 1: Setting file
100 Language Processing Knock with Python (Chapter 1)
[Python] Using OpenCV with Python (Image transformation)
100 Language Processing Knock with Python (Chapter 3)
Personal notes for python image processing
Let's do image scraping with Python
Find image similarity with Python + OpenCV
Automate simple tasks with Python Part0
[Automation with python! ] Part 2: File operation
Send image with python, save with php
Excel aggregation with Python pandas Part 1
Gradation image generation with Python [1] | np.linspace
[Let's play with Python] Image processing to monochrome and dots
[Image processing] Posterization