[PYTHON] 100 image processing knocks !! (001 --010) Carefully and carefully

1.First of all

Practice this to improve the technical capabilities of image preprocessing 100 knocks for image processing !! I will do it with Colaboratory so that it is easy to get started. We will work to complete the goal in two weeks. I will explain it carefully. Please ask a question!

2. Preparation

Introduced libraries etc. as follows.

python


#Import library
from google.colab import drive
import numpy as np
import matplotlib.pyplot as plt
import cv2
from google.colab.patches import cv2_imshow

#Loading images
img = cv2.imread('Image path/imori.jpg')
img_noise = cv2.imread('Image path/imori_noise.jpg')
#For image storage
OUT_DIR = 'Output destination path/OUTPUT/'

3. Explanation

Q.1. Channel replacement

Load the image and swap RGB in BGR order. To extract the red component of the image, you can use the following code. Note that in the cv2.imread () function, the channels are in BGR order! This will put only the red component of imori.jpg in the variable red.

A1


#OpenCV function cvtColor()Convert BGR and RGB with
img1 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#Save the result
cv2.imwrite(OUT_DIR + 'ans1_a.jpg', img1)
#Display image
cv2_imshow(img1)
cv2.waitKey(0)
cv2.destroyAllWindows()

img1.png

Reference: cvtColor that converts BGR and RGB with Python, OpenCV

Q.2. Grayscale

Make the image grayscale. Grayscale is a kind of image brightness expression method and is calculated by the following formula. Y = 0.2126 R + 0.7152 G + 0.0722 B

A2


#OpenCV function cv2.cvtColor(), cv2.COLOR_Convert with BGR2GRAY
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#Save the result
cv2.imwrite(OUT_DIR + 'ans2_.jpg', img2)
#Display image
cv2_imshow(img2)
cv2.waitKey(0)
cv2.destroyAllWindows()

img2.png

Reference: Convert color image to black and white (grayscale) with Python, OpenCV, NumPy

Q.3. Binarization

Binarize the image. Binarization is a method of expressing an image as a binary of black and white. Here, the threshold value is set to 128 in gray scale, and binarization is performed by the following equation.

A3


#image(grayscale)copy of
img3 = img2.copy()

#Threshold setting
threshold = 128

#Binarization(The number of pixels that exceed the threshold value of 128 is set to 255.)
ret, img3 = cv2.threshold(img3, threshold, 255, cv2.THRESH_BINARY)

#Save the result
cv2.imwrite(OUT_DIR + 'ans3_.jpg', img3)
#Display image
cv2_imshow(img3)
cv2.waitKey(0)
cv2.destroyAllWindows()

img3.png

Reference: Binarization of OpenCV images Reference: Image threshold processing

Q.4. Binarization of Otsu

Implement Otsu's binarization. Otsu's binarization is called a discriminant analysis method, which is a method of automatically determining the separation threshold in binarization. This is calculated from the ratio of intraclass variance to interclass variance.

A4


#Implemented with OpenCV
#image(grayscale)copy of
gray = img2.copy()
#Binarization process of Otsu
ret, image4 = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU)

#Save the result
cv2.imwrite(OUT_DIR + 'ans4.jpg', img4)
#Display image
cv2_imshow(img4)
cv2.waitKey(0)
cv2.destroyAllWindows()

img4.png

Reference: [Python / OpenCV] Binarization processing by Otsu's method

Q.5. HSV conversion

Implement HSV conversion to invert hue H. HSV transformation is a method of expressing colors with Hue (hue), Saturation (saturation), and Value (brightness). Hue ... Hue is expressed from 0 to 360 degrees, and indicates the type of color such as red or blue. (0 <= H <360) Hue corresponds to the following colors. Red Yellow Green Light Blue Blue Purple Red 0 60 120 180 240 300 360 Saturation ... Color vividness. When the saturation is low, the gray color becomes noticeable and the color becomes dull. (0 <= S <1) Value ... Color brightness. The higher the Value, the closer to white, and the lower the Value, the closer to black. (0 <= V <1)

A5


#Copy the image
img5 = img.copy()
# RGB ->Convert to HSV
hsv = cv2.cvtColor(img5, cv2.COLOR_BGR2HSV)
#Add 180 to Hue
hsv[..., 0] = (hsv[..., 0] + 180) % 360
# HSV ->Convert to RGB
img5 = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB)
#Save the result
cv2.imwrite(OUT_DIR + 'ans5.jpg', img5)
#Display image
cv2_imshow(img5)
cv2.waitKey(0)
cv2.destroyAllWindows()

img5.png

For some reason, it is slightly different from the answer image. Please let me know if you make a mistake

Reference: [Python / OpenCV] Convert from RGB to HSV (cv2.cvtColor)](https://algorithm.joho.info/programming/python/opencv-rgb-to-hsv-color-space/)

Q.6. Color reduction processing

Here, reduce the value of the image from 256 ^ 3 to 4 ^ 3, that is, R, G, B in {32, 96, 160, 224}. This is a quantization operation. Each value is defined as follows.

A6


def decrease_color(img):
    """
    R,G,Set B to 256 and set a value to 4.
    [0, 64), [64, 128), [128, 192), (192, 256)Divide into 4 equal parts *[]: Closed section(Above and below), (): Open section(Big or small)
    [0, 64)->32,  [64, 128)->96,  [128, 192)->160,  (192, 256) ->Each range conversion to 2224

    parameters
    -------------------------
    param: numpy.ndarray format image

    returns
    -------------------------
    numpy.ndarray format Convert as shown on the right[132  80  67] >>> [160  96  96]
    """
    #Copy image
    out = img.copy()
    print(out)
    # 256/4 =Because it is 64, it is divided by 64+32 by doing 32, 96, 160,Convert to 224
    out = out // 64 * 64 + 32
    return out

img6 = img.copy()

img6 = decrease_color(img6)
#Save the result
cv2.imwrite(OUT_DIR + 'ans6.jpg', img6)
#Display image
cv2_imshow(img6)
cv2.waitKey(0)
cv2.destroyAllWindows()

img6.png

Reference: Somehow blog

Q.7. Average pooling

Here, the image is divided into grids (divided into certain fixed-length areas), and the values in the area are filled with the average value in the area (cell). The operation of dividing the grid in this way and finding the representative value in that area is called Pooling. These pooling operations play an important role in CNN (Convolutional Neural Network).

A7


def average_pooling(img, G=8):
    """
Average pooling

    parameters
    -------------------------
    param1: numpy.ndarray format image
    param2:Grid division into 8x8

    returns
    -------------------------
Average pooled numpy.ndarray format image
    """
    #Copy image
    out = img.copy()
    # H(Image height), W(Image width), C(color)Get
    H, W, C = img.shape
    #Grid division
    Nh = int(H / G)
    Nw = int(W / G)

    #Average pooling for each 8-divided range
    for y in range(Nh):
        for x in range(Nw):
            for c in range(C):
                out[G*y:G*(y+1), G*x:G*(x+1), c] = np.mean(out[G*y:G*(y+1), G*x:G*(x+1), c]).astype(np.int)
    
    return out

#Average pooling
img7 = average_pooling(img)
#Save the result
cv2.imwrite(OUT_DIR + 'ans7.jpg', img7)
#Display image
cv2_imshow(img7)
cv2.waitKey(0)
cv2.destroyAllWindows()

img7.png

Q.8. Max pooling

Here, pool with the maximum value instead of the average value.

A8


def max_pooling(img, G=8):
    """
Maximum pooling

    parameters
    -------------------------
    param1: numpy.ndarray format image
    param2:Grid division into 8x8

    returns
    -------------------------
Maximum pooled numpy.ndarray format image
    """
    #Copy image
    out = img.copy()
    # H(Image height), W(Image width), C(color)Get
    H, W, C = img.shape
    #Grid division
    Nh = int(H / G)
    Nw = int(W / G)

    #Average pooling for each 8-divided range
    for y in range(Nh):
        for x in range(Nw):
            for c in range(C):
                out[G*y:G*(y+1), G*x:G*(x+1), c] = np.max(out[G*y:G*(y+1), G*x:G*(x+1), c]).astype(np.int)
    
    return out

#Maximum pooling
img8 = average_pooling(img)
#Save the result
cv2.imwrite(OUT_DIR + 'ans8.jpg', img8)
#Display image
cv2_imshow(img8)
cv2.waitKey(0)
cv2.destroyAllWindows()

img8.png

Q.9. Gaussian filter

Implement a Gaussian filter (3x3, standard deviation 1.3) to remove the noise in imori_noise.jpg. A Gaussian filter is a type of filter that smoothes an image and is also used for noise removal. Other noise removal methods include a median filter (Q.10), a smoothing filter (Q.11), and a LoG filter (Q.19). The Gaussian filter smoothes the peripheral pixels of the pixel of interest by weighting with a Gaussian distribution, and is defined by the following equation. Such weights are called kernels and filters. However, since the edges of the image cannot be filtered as they are, the part with insufficient pixels is filled with 0. This is called 0 padding. And the weights are normalized. (sum g = 1)

A9


#Processed with OpenCV
# cv2.GaussianBlur(src, ksize, sigmaX)
# src:Input image, ksize:Kernel size, sigmaX:Gaussian distribution sigma_x
img9 = cv2.GaussianBlur(img_noise, ksize=(3, 3), sigmaX=1.3)

#Save the result
cv2.imwrite(OUT_DIR + 'ans9.jpg', img9)
#Display image
cv2_imshow(img9)
cv2.waitKey(0)
cv2.destroyAllWindows()

img9.png

Reference: [Python / OpenCV] Blur / Smooth with Gaussian Filter

Q.10 Median filter

Implement a median filter (3x3) to remove the noise in imori_noise.jpg. A median filter is a type of filter that smoothes an image. This is a filter that outputs the median value (median value) in the 3x3 area of the pixel of interest. This is also zero padding.

A10


#Processed with OpenCV
"""
cv2.medianBlur(src, ksize)
src input image
Kernel size of kernel filter (near 8 if 3)
"""
#Median filter
img10 = cv2.medianBlur(img_noise, ksize=3)
#Save the result
cv2.imwrite(OUT_DIR + 'ans10.jpg', img10)
#Display image
cv2_imshow(img10)
cv2.waitKey(0)
cv2.destroyAllWindows()

img10.png

Reference: [Python / OpenCV] Blur / Smooth / Noise Removal with Median Filter

Impressions

Preprocessing is important in machine learning. If you don't know the method, you won't come up with any ideas, so it's well worth the effort. Knock format makes it easy to work on

Recommended Posts

100 image processing knocks !! (001 --010) Carefully and carefully
Image processing 100 knocks ①
100 image processing knocks !! (011 --020) Early game
Image expansion and contraction processing
Image processing with Python 100 knocks # 3 Binarization
Image processing 100 knocks Q9, Q10 (filter) speedup
Image processing with Python 100 knocks # 2 Grayscale
100 language processing knocks 03 ~ 05
[Image processing] Posterization
100 language processing knocks (2020): 40
100 language processing knocks (2020): 32
100 language processing knocks (2020): 35
python image processing
100 language processing knocks (2020): 39
100 language processing knocks (2020): 22
100 language processing knocks (2020): 26
100 language processing knocks (2020): 34
Image processing with Python 100 knocks # 8 Max pooling
100 language processing knocks (2020): 42
100 language processing knocks (2020): 29
100 language processing knocks 06 ~ 09
100 language processing knocks (2020): 43
100 language processing knocks (2020): 24
100 language processing knocks (2020): 45
100 language processing knocks (2020): 10-19
100 language processing knocks (2020): 30
100 language processing knocks (2020): 00-09
100 language processing knocks (2020): 31
100 language processing knocks (2020): 48
100 language processing knocks (2020): 44
100 language processing knocks (2020): 41
100 language processing knocks (2020): 37
Image processing with Python 100 knocks # 7 Average pooling
100 language processing knocks (2020): 25
100 language processing knocks (2020): 23
100 language processing knocks (2020): 33
100 language processing knocks (2020): 20
100 language processing knocks (2020): 27
100 language processing knocks (2020): 46
100 language processing knocks (2020): 21
Image processing with Python 100 knocks # 9 Gaussian filter
100 language processing knocks (2020): 36
100 amateur language processing knocks: 71
100 amateur language processing knocks: 56
100 amateur language processing knocks: 24
100 amateur language processing knocks: 50
100 amateur language processing knocks: 59
100 amateur language processing knocks: 70
Image processing with MyHDL
100 amateur language processing knocks: 62
100 amateur language processing knocks: 60
100 amateur language processing knocks: 92
100 amateur language processing knocks: 30
100 amateur language processing knocks: 06
100 amateur language processing knocks: 84
100 amateur language processing knocks: 81
100 amateur language processing knocks: 33
100 amateur language processing knocks: 46
100 amateur language processing knocks: 88
100 amateur language processing knocks: 89
Flat Field image processing