Until now, I used ImageJ, R, Matlab, etc. for image processing, but I tried something with Python (+ OpenCV).
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.
jupyter is too convenient. I want to install anaconda just for jupyter. I want to use it with perl and R.
The database is scott / tiger emp, and the human face image is 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.
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()
If you want to know more about this Lenna, look at img.shape or img.size.
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()
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()
That's why I was able to do the basics for the time being.
Recommended Posts