This is image processing that is often used to process and analyze images. If you remember, you can reduce the number of image files, so it's good to remember.
Also called a grayscale image. To be black and white. It is the basis of image processing.
・ Jupyter notebook ・ Python version == 3.7.4 ・ Sample.jpg (from http://free-photo.net/archive/entry10252.html)
#import of opencv and numpy
import cv2
import numpy as np
#Loading images
img = cv2.imread("sample1.jpg ")
#Grayscale conversion
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
#Threshold setting
threshold_value = 150
#Array creation (for output)
threshold_img = gray.copy()
#Implementation(numpy)
threshold_img[gray < threshold_value] = 0
threshold_img[gray >= threshold_value] = 255
#Output
cv2.imwrite("C:\\Users\\[username]\\python\\sample1-2.jpg ",threshold_img)
At the threshold setting, if you try using a different number instead of "150", you will see a black and white position change. imageJ(URL:"https://imagej.nih.gov/ij/") If you use software such as, you can see such changes in real time, so you should try it.
Recommended Posts