Python application: Data cleansing # 3: Use of OpenCV and preprocessing of image data

Basics of image data

OpenCV basics

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)

Create and save images

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)

Trimming and resizing

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)

mosaic

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.

Rotation / reversal

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)

Color conversion / color inversion

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.

Use of OpenCV

Threshold processing (binarization)

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)

masking

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)

Blur (smoothing)

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)

Noise removal

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)

Expansion / contraction

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

Python application: Data cleansing # 3: Use of OpenCV and preprocessing of image data
Data cleansing 3 Use of OpenCV and preprocessing of image data
Environment construction of python and opencv
Python application: Data cleansing # 2: Data cleansing with DataFrame
Easy introduction of python3 series and OpenCV3
Python: Preprocessing in machine learning: Handling of missing, outlier, and imbalanced data
python development environment -use of pyenv and virtualenv-
cv2 functions and data types (OpenCV python bindings)
[Python + OpenCV] Whiten the transparent part of the image
Preprocessing of prefecture data
Application of Python 3 vars
Use data class for data storage of Python 3.7 or higher
Full-width and half-width processing of CSV data in Python
List of Python libraries for data scientists and data engineers
[OpenCV / Python] I tried image analysis of cells with OpenCV
Story of image analysis of PDF file and data extraction
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Let's use the open data of "Mamebus" in Python
Rotate and scale the image before cropping [python] [OpenCV]
Overview of natural language processing and its data preprocessing
What to use for Python stacks and queues (speed comparison of each data structure)
[Python of Hikari-] Chapter 08-03 Module (Import and use of standard library)
I tried "gamma correction" of the image with Python + OpenCV
Pre-processing and post-processing of pytest
Image editing with python OpenCV
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
Get rid of dirty data with Python and regular expressions
[Python] Summary of how to use split and join functions
Python application: Data handling Part 1: Data formatting and file input / output
Python application: data visualization # 2: matplotlib
Return the image data with Flask of Python and draw it to the canvas element of HTML
Application of CNN2 image recognition
Data cleansing 1 Convenient Python notation such as lambda and map
Comparison of how to use higher-order functions in Python 2 and 3
I made a function to crop the image of python openCV, so please use it.
[Introduction to Data Scientists] Basics of Python ♬ Functions and classes
Use and integration of "Shodan"
Implement normalization of Python training data preprocessing with scikit-learn [fit_transform]
Image data type conversion [Python]
Source installation and installation of Python
[Python] Accessing and cropping image pixels using OpenCV (for beginners)
[Python] Easy reading of serial number image files with OpenCV
Recommended books and sources of data analysis programming (Python or R)
processing to use notMNIST data in Python (and tried to classify it)
Automatic image interpolation with OpenCV and Python (Fast Marching Method, Navier-Stokes)
Automatic acquisition of gene expression level data by python and R
[Image processing] Edge detection using Python and OpenCV makes Poo naked!
Practice of data analysis by Python and pandas (Tokyo COVID-19 data edition)
I want to use both key and value of Python iterator
Aligning scanned images of animated video paper using OpenCV and Python
[Introduction to Udemy Python3 + Application] 69. Import of absolute path and relative path
[Introduction to Udemy Python3 + Application] 12. Indexing and slicing of character strings
[Introduction to Data Scientists] Basics of Python ♬ Conditional branching and loops
[Introduction to Data Scientists] Basics of Python ♬ Functions and anonymous functions, etc.
Shining life with Python and OpenCV
The story of Python and the story of NaN
Image capture of firefox using python
[Python] Using OpenCV with Python (Image Filtering)
Neural network with OpenCV 3 and Python 3
Installation of SciPy and matplotlib (Python)
Judgment of backlit image using OpenCV