This is the second day of my participation in Gwen Challenge
OpenCV is a C++ library, currently popular computer vision programming library, for real-time processing of computer vision problems, it covers a lot of modules in the field of computer vision. In Python, the OpenCV library is often used for image processing.
This article will show how to use OpenCV in Python3 to implement grayscale transformation for image processing:
- Grayscale processing
- Binary processing
- Gamma transform
- Logarithmic transformation
- The reverse transformation
Computer Environment Preparation
Python version:Python3.7
OpenCV version:OpenCV4.1
Knowledge reserves
A picture is composed of pixel matrix, we operate on the picture is to operate on the pixel matrix of the picture. We just have to find the location of this pixel in this pixel matrix, like row x, column y. So the position of this pixel in this pixel matrix can be represented as (x, y).
At the same time, because the color of a pixel usually includes R, G and B components, showing red, green and blue colors respectively, grayscale is the process of making R, G and B components of a color image equal. Each pixel in a grayscale image has only one sample color, and its grayscale is a multi-level color depth between black and white. The pixel with a large grayscale value is bright, while the pixel with a large grayscale value is dark. The maximum pixel value is 255 (white), and the minimum pixel value is 0 (black).
Grayscale processing
Opencv image grayscale: the three color variables of a pixel are equal, R=G=B, and this value is called gray value.
There are two methods of grayscale processing provided in this paper (grayscale processing is more than these two)
Method 1: Read the original image directly into grayscale image
img1=cv2.imread('girl.png',0)
Method 2: Grayscale the original image
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
importImg =cv2.imread('girl.png'.1) # Grayscale processing1: read the grayscale image img1=cv2.imread('girl.png'.0) # Grayscale processing2Cv2.imshow () : gray= cv2.cvtcolor (img, cv2.color_bgr2gray)'img',img)
cv2.imshow('gray1',img1)
cv2.imshow('gray2'Cv2.waitkey (gray) # Pause the cv2 module or the image window will disappear immediately.0)
Copy the code
After running the program, the following information is displayed:
Binary processing
The binarization of the image in Opencv is to set the gray value of the pixels on the image to 0 or 255, that is, the whole image presents an obvious visual effect of only black and white.
The cv.threshold () function is an image binarization function
The second parameter is the critical value to determine the pixel point. Above this point, it will be rated 255, below this point, it will be rated 0. Parameters 0 to 255 can be adjusted as required.
importImg =cv2.imread('girl.png'.1Gray = cv2.cvtcolor (img, cv2.color_bgr2gray) # cv2.threshold(gray,140.255.0The first parameter is the window name and the second parameter is the image variable cv2.imshow().'img',img)
cv2.imshow('gray',img1)
cv2.imshow('Binarization'Cv2.waitkey (gray) # Pause the cv2 module or the image window will disappear immediately.0)
Copy the code
After running the program, the following information is displayed:
Gamma transform
The gamma transform in Opencv is used to enhance the image and improve the dark details. In short, the nonlinear transformation changes the image from a linear response to the intensity of exposure to a response more similar to that experienced by the human eye. The bleached (camera exposure) or darkened (underexposure) image is corrected.
Gamma less than 1
, will stretch the area with lower gray level in the image, and compress the part with higher gray levelGamma is greater than 1
, will stretch the image with a higher gray level, and compress the part with a lower gray level
import cv2
importImg =cv2.imread('girl.png'.1Img1 =cv2.imread('girl.png'.0Gray = cv2.cvtcolor (img, cv2.color_bgr2gray) # Gamma transform gamma= copy.deepCopy (gray) rows=img.shape[0]
cols=img.shape[1]
for i in range(rows):
for j in range(cols):
gamma[i][j]=3*pow(gamma[i][j],0.8The first parameter is the window name and the second parameter is the image variable cv2.imshow('img',img)
cv2.imshow('gray',img1)
cv2.imshow('gamma'Gamma) # Pause the cv2 module or the image window will disappear in a moment and cv2.waitKey(0)
Copy the code
After running the program, the following information is displayed:
Logarithmic transformation
Logarithmic transformation in Opencv: Since the slope of the logarithmic curve is high in the region with low pixel value and low in the region with high pixel value, the contrast of the dark region of the image will be improved after logarithmic transformation. Can be used to enhance dark details in an image.
The logarithmic transformation of grayscale images is generally expressed as follows: DB=C*log(1+ DA)
Among themc
Is the scale comparison constant,DA
Is the gray value of the original image,DB
Is the target gray value after transformation.
As shown in the figure below, it represents the change of gray value under the logarithmic curve.
Since the logarithmic curve has a high slope in the region with a low pixel value and a low slope in the region with a high pixel value, the contrast of the dark region of the image will be improved after logarithmic transformation.
This transformation can be used to enhance the dark details of the image, which can be used to expand the dark pixels in the compressed high-value image.
Logarithmic transform is widely used in spectrum image display because it can expand low gray value and compress high gray value.
A typical application is Fourier spectrum, whose dynamic range may be as wide as 0 ~ 106 directly display spectrum, the dynamic range of image display equipment often cannot meet the requirements, thus losing a lot of dark details; After logarithmic transformation, the dynamic range of the image is reasonably nonlinear compressed, so that it can be clearly displayed.
import cv2
import copy
importImg =cv2.imread('girl.png'.1Img1 =cv2.imread('girl.png'.0Gray = cv2.cvtcolor (img, cv2.color_bgr2gray) # Log transform logc = copy.deepCopy (gray) rows=img.shape[0]
cols=img.shape[1]
for i in range(rows):
for j in range(cols):
logc[i][j] = 3 * math.log(1+ logc[I][j]) # logc[I][j]) # logc[I]'img',img)
cv2.imshow('gray',img1)
cv2.imshow('logc',logc) # pause cv2 or the image window will disappear in a moment.0)
Copy the code
After running the program, the following information is displayed:
The reverse transformation
Opencv invert color transform: reverse the color of the original image pixel value, that is, black to white, white to black.
import cv2
import copy
importImg =cv2.imread('girl.png'.1Img1 =cv2.imread('girl.png'.0Gray = cv2.cvtcolor (img, cv2.color_bgr2gray) # Cover = copy.deepCopy (gray) rows=img.shape[0]
cols=img.shape[1]
for i in range(rows):
for j in range(cols):
cover[i][j]=255-cover[I][j] # cover[I][j] # cover[I]'img',img)
cv2.imshow('gray',img1)
cv2.imshow('cover'Cv2.waitkey () # Pause the cv2 module or the image window will disappear in a moment.0)
Copy the code
After running the program, the following information is displayed:
conclusion
A series of articles will follow this month,
Wonderful article, please pay attention.