Previous portal:
“Python Image Processing OpenCV (1) : Getting Started”
“Python Image Processing OpenCV (2) : Pixel Processing with Numpy manipulation and Matplotlib Display images”
“Python Image Processing OpenCV (3) : Image Properties, ROI Regions of Interest and Channel Processing”
“Python Image Processing OpenCV (4) : Image Arithmetic and Color Space Modification”
“Python Image Processing OpenCV (5) : Geometric Transformations of Images”
The threshold of the image
You may be confused by this word, why there is a threshold in image processing.
Image threshold processing in plain English is to transform the image into a binary image (black and white), the purpose is to extract the target object in the image, distinguish the background and noise (it can be considered that all except the target is noise).
A threshold T is usually set, by which the pixels of the image are divided into two categories: pixel groups larger than T and pixel groups smaller than T.
First, the image can be converted to grayscale image, because in grayscale image, each pixel has only one grayscale value to represent the brightness of the current pixel.
The next binarization process can divide the pixels in the image into two types of colors, one is greater than the threshold T, and the other is less than the threshold T.
For example, the most common binary image:
When the gray value is less than the threshold T, the pixel can be set to 0, indicating black.
When the gray value is greater than the threshold T, the pixel can be set to 255, indicating white.
In OpenCV, the threshold function threshold() is provided to help us realize binary image processing.
The function is as follows:
retval, dst = threshold(src, thresh, maxval, type, dst=None)
Copy the code
- Retval: threshold
- DST: processed image
- SRC: the original image
- Thresh: threshold
- A maximum very much:
- Type: indicates the processing type
Common 5 processing types are as follows:
- CV.THRESH_BINARY: Binary processing
- CV.THRESH_BINARY_INV: reverse binary processing
- Cv. THRESH_TRUNC: truncated thresholding
- CV.THRESH_TOZERO: The threshold is 0
- CV.THRESH_TOZERO_INV: Reverse threshold to 0
Let’s take a look at the differences between each of these processing types.
Binary processing
This binary processing starts with a threshold T, between 0 and 255, and I chose the middle number 127.
The next processing rule looks like this:
- The gray value of pixels greater than or equal to 127 is set to the maximum, i.e. 255 white
- Pixels less than 127 have a gray value of 0, which is black
So let’s start coding and watch Mario (I don’t know if you remember Mario) :
import cv2 as cv
src = cv.imread("maliao.jpg")
# BGR Image grayscale
gray_img = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
# Binary image processing
r, b = cv.threshold(gray_img, 127.255, cv.THRESH_BINARY)
# display image
cv.imshow("src", src)
cv.imshow("result", b)
# wait for display
cv.waitKey(0)
cv.destroyAllWindows()
Copy the code
Inverse binary processing
This is very similar to the binary processing above, except that the processing rules are reversed:
- The gray value of pixels greater than or equal to 127 is set to 0, that is, white
- The gray value of pixels less than 127 is set to the maximum, which is 255 white
The complete code is as follows:
import cv2 as cv
src = cv.imread("maliao.jpg")
# BGR Image grayscale
gray_img = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
# Binary image processing
r, b = cv.threshold(gray_img, 127.255, cv.THRESH_BINARY_INV)
# display image
cv.imshow("src", src)
cv.imshow("result", b)
# wait for display
cv.waitKey(0)
cv.destroyAllWindows()
Copy the code
As you can see from the image, the color is the opposite of the binary image above, with most of the locations turned white.
Truncation thresholding
This method still needs to first select a threshold T, and pixels larger than this threshold in the image are set as this threshold, while those smaller than this threshold remain unchanged.
The complete code is as follows:
import cv2 as cv
src = cv.imread("maliao.jpg")
# BGR Image grayscale
gray_img = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
# Binary image processing
r, b = cv.threshold(gray_img, 127.255, cv.THRESH_TRUNC)
# display image
cv.imshow("src", src)
cv.imshow("result", b)
# wait for display
cv.waitKey(0)
cv.destroyAllWindows()
Copy the code
In this way, the brighter pixels of the image are treated as thresholds, leaving the rest unchanged.
The threshold is set to 0
In this way, we still need to select a threshold T first, set the pixels less than T to 0 black, and keep the rest unchanged.
The complete code is as follows:
import cv2 as cv
src = cv.imread("maliao.jpg")
# BGR Image grayscale
gray_img = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
# Binary image processing
r, b = cv.threshold(gray_img, 127.255, cv.THRESH_TOZERO)
# display image
cv.imshow("src", src)
cv.imshow("result", b)
# wait for display
cv.waitKey(0)
cv.destroyAllWindows()
Copy the code
The method is to leave the light parts unchanged and change the dark parts to 0.
The inverse threshold is set to 0
This is similar to the previous inverse binary image, which also inverts the threshold value to 0, turning pixels greater than or equal to T to 0, and leaving the rest unchanged.
The complete code is as follows:
import cv2 as cv
src = cv.imread("maliao.jpg")
# BGR Image grayscale
gray_img = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
# Binary image processing
r, b = cv.threshold(gray_img, 127.255, cv.THRESH_TOZERO_INV)
# display image
cv.imshow("src", src)
cv.imshow("result", b)
# wait for display
cv.waitKey(0)
cv.destroyAllWindows()
Copy the code
The method is to leave the dark parts unchanged and change the lighter parts to 0.
You and your family
Next, we will take a family photo of the image processed by these threshold values, so that we can have an intuitive feeling. The code is also given as follows:
import cv2 as cv
import matplotlib.pyplot as plt
# fetch image
img=cv.imread('maliao.jpg')
lenna_img = cv.cvtColor(img,cv.COLOR_BGR2RGB)
gray_img=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
# thresholding processing
ret1, thresh1=cv.threshold(gray_img, 127.255, cv.THRESH_BINARY)
ret2, thresh2=cv.threshold(gray_img, 127.255, cv.THRESH_BINARY_INV)
ret3, thresh3=cv.threshold(gray_img, 127.255, cv.THRESH_TRUNC)
ret4, thresh4=cv.threshold(gray_img, 127.255, cv.THRESH_TOZERO)
ret5, thresh5=cv.threshold(gray_img, 127.255, cv.THRESH_TOZERO_INV)
# display result
titles = ['Gray Img'.'BINARY'.'BINARY_INV'.'TRUNC'.'TOZERO'.'TOZERO_INV']
images = [gray_img, thresh1, thresh2, thresh3, thresh4, thresh5]
# matplotlib drawing
for i in range(6):
plt.subplot(2.3, i+1), plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
Copy the code
The sample code
If you need to get the source code, you can reply “OpenCV” on the public account to get it.
reference
Blog.csdn.net/Eastmount/a…
www.woshicver.com/