OpenCV is a convenient library for working with images. Basic image handling using cv2
Reads the image and outputs it. To load an image
OpenCV Use the cv2.imread () function.
cv2.imread("file name")
To output an image Use the cv2.imshow () function. The size of the window that displays the image is automatically adjusted.
cv2.imshow("Window name",Read image data)
Read the image and try to output it.
#Import the library
import numpy as np
import cv2
#Load the image
img = cv2.imread("./4050_data_cleansing_data/sample.jpg ")
#The name of the window that displays the image is"sample"
#The loaded image is"img"is
cv2.imshow("sample", img)
How to create and save a monochrome image. When processing images with cv2, the order of color specification is not RGB Please note that it will be BGR.
Create a NumPy matrix to create an image Using the np.array () function Furthermore, by multiple loops that combine the for statement and the range () function Performs processing such as drawing the color information of each pixel side by side. The sample code below specifies values for [B, G, R] 512 images horizontally with the first for The second for is a multiple loop that creates 512 images vertically. The data type is an 8-bit unsigned integer type with a value range of 0 to 255. Specify uint8
np.array([[[B, G, R] for _ in range(Horizontal image size)] for _ in range(Vertical image size)], dtype="uint8")
_ In "for _ in range" is used when the variable name corresponding to _ is not specified when repeating the process in the for statement.
To save an image
OpenCV
cv2.imwrite()Use a function.
cv2.imwrite("file name",Created image data)
For example, create a solid red image with an image size of 512px in height and 512px in width.
import numpy as np
import cv2
#Determine the size of the image
img_size = (512, 512)
#Create a matrix with image information with NumPy
#Since it is a red image, each element is[0, 0, 255]Of, 512*Consider making a matrix of 512
#Note that the matrix is transposed
#Each element of image data is 0~Since it only takes a value of 255, specify the data type with the dtype option to make this clear.
my_img = np.array([[[0, 0, 255] for _ in range(img_size[1])] for _ in range(img_size[0])], dtype="uint8")
#display
cv2.imshow("sample", my_img)
#Save
#The file name is"my_red_img.jpg "
cv2.imwrite("my_red_img.jpg ", my_img)
How to crop and resize an image. Cropping is an operation to extract a part of an image. Resizing is to resize (enlarge or reduce) an image.
Trimming is done by specifying the range of the matrix that represents the image. Cuts the specified part of the image into a square. When specifying the range, the upper left of the image is 0.
img[y start point: y end point,x start point: x end point]
Resize uses the cv2.resize () function.
cv2.resize(image data, (Width after resizing,Height after resizing))
import numpy as np
import cv2
import matplotlib.pyplot as plt
#Load image
# cv2.imread reads an image with NumPy's ndarray type
img = cv2.imread("./4050_data_cleansing_data/sample.jpg ")
#The read image data is (height x width x number of colors))Triple array of
size = img.shape #size(1000, 667, 3)
#Extract by specifying a range (fixed value or slice is possible for range specification)
#If you want to divide it into n equal parts, take the quotient of size, but truncate after the decimal point.
my_img = img[: size[0] // 2, : size[1] // 3]
#When specifying a new size(width,height)Will be in the order of
#When doubling the width and height while maintaining the original magnification
my_img = cv2.resize(my_img, (my_img.shape[1] * 2, my_img.shape[0] * 2))
#display
#Display using Matplotlib, which displays scales for easy understanding of the range specified by trimming and resizing
my_img = cv2.cvtColor(my_img, cv2.COLOR_BGR2RGB)
plt.imshow(my_img)
plt.show()
# cv2.When displaying with imshow
# cv2.imshow("sample", my_img)
As an application of resizing, if you reduce the image to reduce the number of pixels and then return to the original size You can mosaic the image.
To rotate the image
cv2.warpAffine()Use a function.
This function performs a geometric transformation called an affine transformation. The affine transformation is represented by a combination of translation and linear transformation (scaling, shearing, rotation).
cv2.warpAffine(Original image data,Transformation matrix,Output image size)
The parameters are as follows.
First argument: Rotating original image data (NumPy array ndarray)
Second argument: transformation matrix (NumPy array ndarray)
Third argument: Output image size (tuple)
The transformation matrix specified in the second argument is
cv2.getRotationMatrix2D()Get it with a function.
cv2.getRotationMatrix2D(Coordinates of the center of the image,The angle you want to rotate,Scale ratio)
The parameters are as follows.
First argument: Coordinates of the center of the image (tuple)
Second argument: The angle you want to rotate (not radians)
Third argument: Enlargement / reduction magnification (1x is 1).0)
import numpy as np
import cv2
img = cv2.imread("./4050_data_cleansing_data/sample.jpg ") #size(1000, 667, 3)
# warpAffine()The second argument of the function`Transformation matrix`To get
mat = cv2.getRotationMatrix2D(tuple(np.array([img.shape[1], img.shape[0]]) / 2), 180, 2.0)
#Rotates 180 degrees at 2x magnification
my_img = cv2.warpAffine(img, mat, img.shape[::-1][1:3])
cv2.imshow("sample", my_img)
Inversion is
cv2.flip()Use a function.
cv2.flip(image data,axis)
The parameters are as follows.
First argument: Original image data to be inverted
Second argument: Target axis
When the target axis is 0, it is centered on the x-axis (upside down), and when it is positive, it is centered on the y-axis (left-right reversal).
When negative, it flips around both axes.
import numpy as np
import cv2
img = cv2.imread("./4050_data_cleansing_data/sample.jpg ")
#x-axis y-axis Inverts around both axes
my_img = cv2.flip(img, -1)
cv2.imshow("sample", my_img)
Converts an image to another color space. OpenCV supports various color spaces and can be converted to each other using the cv2.cvtColor () function.
cv2.cvtColor(image data,Code to convert to another color space)
For example, convert an image in BGR color space to Lab color space. Lab color space is a color system that is excellent in that it is designed to approximate human vision.
import numpy as np
import cv2
img = cv2.imread("./4050_data_cleansing_data/sample.jpg ")
#Convert BGR to Lab color space
my_img = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
cv2.imshow("BGR to LAB", my_img)
cv2.imread reads images in BGR color space by default For example, Matplotlib and SciPy default to RGB.
You can use COLOR_BGR2GRAY to convert to grayscale. For other conversion codes, refer to the official OpenCV website.
Inverting the color of an image is called negative-positive inversion. Black-and-white inversion is performed for monochrome images, and complementary color inversion is performed for full-color images.
Since the RGB value consists of 0 to 255, you can invert the color by replacing a certain pixel value x with 255-x. The pixel values read by OpenCV are a three-dimensional numpy array. Rewrite the array pixel by pixel from x to 255-x for each RGB channel.
#Coordinates of each pixel(i, j)And RGB channels(k)To get the pixel value before conversion by specifying
img[i][j][k] = x
Get the coordinates with the for statement and len (img [i]), access each pixel in order, and convert the pixel value for each RGB channel.
import numpy as np
import cv2
img = cv2.imread("./4050_data_cleansing_data/sample.jpg ")
# img.shape is(1000, 667, 3)
#Each i, j,Is the value of k
#Pixel coordinates(i, j)Get
for i in range(len(img)):
for j in range(len(img[i])):
#RGB channel(k:0-2)Get
for k in range(len(img[i][j])):
#Coordinate(i, j)RGB channel(k:0-2)Convert pixel values for each
img[i][j][k] = 255 - img[i][j][k]
cv2.imshow("sample", img)
#Cv2 in OpenCV.bitwise_not()Invert using a function.
img = cv2.bitwise_not(image data)
# cv2.bitwise()The function can manipulate the bit of each pixel represented by 8 bits.
#Invert each bit with not.
Brighter than a certain amount to reduce the size of the image Or make all colors darker than a certain value the same value In short, the process of binarizing to white and black
This is called threshold processing.
Threshold processing is cv2.threshold()Use a function.
cv2.threshold(image data,Threshold,Maximum value maxValue,Threshold処理の種類)
The parameters are as follows.
First argument: Original image file name to be binarized
Second argument: Number of pixels to be the threshold
Third argument: Maximum concentration value for threshold
Fourth argument: OpenCV threshold processing type (THRESH)_BINARY, THRESH_BINARY_INV, THRESH_TRUNC, THRESH_TOZERO, THRESH_TOZERO_Any of INV)
The types of threshold processing for the fourth argument are as follows.
THRESH_BINARY: Pixels that exceed the threshold value will be maxValue, and other pixels will be 0.
THRESH_BINARY_INV: Pixels above the threshold will be 0, other pixels will be maxValue
THRESH_TRUNC: Pixels that exceed the threshold are set to the threshold, and other pixels are not changed.
THRESH_TOZERO: Pixels that exceed the threshold are not changed, and other pixels are 0.
THRESH_TOZERO_INV: Pixels above the threshold are set to 0, other pixels are unchanged
Various threshold processing can be performed depending on the type of threshold processing.
import numpy as np
import cv2
#Read a color image in grayscale (cv2).IMREAD_GRAYSCALE or 0)
img = cv2.imread("./4050_data_cleansing_data/sample.jpg ", 0)
# THRESH_Use TOZERO to set the threshold to 150 and the maximum to 255
#The threshold is also returned, so receive it with retval
retval, my_img = cv2.threshold(img, 150, 255, cv2.THRESH_TOZERO)
cv2.imshow("original", img)
cv2.imshow("sample", my_img)
print(retval)
A part of the original image is displayed as if it was hollowed out in the area drawn in white of another image. For masking processing, prepare an image for masking with one black and white channel. Mask the original image with the black part of the mask image Only the white part is displayed. The masking process uses the cv2.bitwise_and () function.
cv2.bitwise_and(Original image data 1,Original image data 2(Original image data 1と同じで可), mask=Image for mask)
import numpy as np
import cv2
#Load the original image
img = cv2.imread("./4050_data_cleansing_data/sample.jpg ")
#Converts the mask image to an image with 1 channel (grayscale image) by specifying 0 as the second argument and loads it.
mask = cv2.imread("./4050_cleansing_data/mask.png ", 0)
#Resize the mask image to the same size as the original image
mask = cv2.resize(mask, (img.shape[1], img.shape[0]))
#Specify the masking image in the third argument and mask
my_img = cv2.bitwise_and(img, img, mask = mask)
cv2.imshow("sample", my_img)
There are several ways to blur the image Among them, n✕n pixels around a certain pixel (pixel) are averaged and blurred. Blur the entire image with a Gaussian filter. The blur process uses the cv2.GaussianBlur () function.
cv2.GaussianBlur(image data,Kernel size(n, n), 0)
The parameters are as follows.
First argument: Name of the original image file to be blurred
Second argument: Kernel size (n ✕ n value (n is odd))
Third argument: Gaussian kernel standard deviation in x direction (usually 0)
The larger the kernel size and standard deviation values, the stronger the blur.
import numpy as np
import cv2
img = cv2.imread("./4050_data_cleansing_data/sample.jpg ")
my_img = cv2.GaussianBlur(img, (51, 51), 0)
cv2.imshow("sample", my_img)
To remove noise in color images Using the cv2.fastNlMeansDenoisingColored () function For noise reduction in grayscale images Use the cv2.fastNlMeansDenoising () function.
#Color image
cv2.fastNlMeansDenoisingColored(image data)
#Grayscale image
cv2.fastNlMeansDenoising(image data)
import numpy as np
import cv2
img = cv2.imread("./4050_data_cleansing_data/sample.jpg ")
my_img = cv2.fastNlMeansDenoisingColored(img)
cv2.imshow("sample", my_img)
By non-local Means Filter I removed noise from color and grayscale images. Noise can also be removed by expansion / contraction processing.
Expansion and contraction are methods that are often used mainly in binary images. The expansion is centered on a pixel, and the maximum value (= white) in the filter is the center value.
On the contrary, the contraction has the minimum value (= black) as its center value. The filter uses four methods, top, bottom, left, and right of the center pixel. There are two main methods that use eight surrounding yourself.
Expansion uses the cv2.dilate () function The contraction uses the cv2.erode () function.
#expansion
cv2.dilate(image data,filter)
#Shrink
cv2.erode(image data,filter)
The parameters are as follows.
First argument: Original image file name
Second argument: Filter (kernel size)
You can specify either an array of numpy ndarray type or a tuple.
#Read in grayscale
img = cv2.imread("./4050_data_cleansing_data/sample.jpg ", 0)
#Convert to binary image
retval, img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
#Filter definition
filt = np.array([[0, 1, 0],
[1, 0, 1],
[0, 1, 0]], np.uint8)
# filt = np.ones((3, 3), np.uint8)But OK
#expansion
my_img = cv2.dilate(img, filt)
cv2.imshow("sample", my_img)
np.uint8 represents the type of data, which is an unsigned integer type represented by 8 bits.
Recommended Posts