[PYTHON] 100 image processing knocks !! (011 --020) Early game

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! 001 --010 is from the link on the right 100 image processing knocks !! (001 --010) Carefully and carefully

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.11. Smoothing filter

Implement a smoothing filter (3x3). The smoothing filter is a filter that outputs the average value of the pixels in the filter.

A11


"""
Smoothing filter
cv2.filter2D(src, -1, kernel)
src input image
kernel filter kernel(* Give as a NumPy array)
"""

#Filter kernel
kernel = np.ones((3,3),np.float32)/9
#Smoothing filter
img11 = cv2.filter2D(img, -1, kernel)

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

img11.png

Reference: [Python / OpenCV] Smoothing / contour detection by spatial filtering Reference: Aiming to be a beginner

Q.12. Motion filter

Implement a motion filter (3x3).

A12


def motion_filter(img, k_size=3):
    """
Motion filter(A filter that takes a diagonal mean)

    parameters
    -------------------------
    param1: numpy.ndarray format image
    param2:Kernel size

    returns
    -------------------------
    (130x130)Numpy.ndarray format image
    """
    #Get image height, width and color
    H, W, C = img.shape

    #kernel(numpy.diag()Extract diagonal components with)
    K = np.diag([1] * k_size).astype(np.float)  # array([[1., 0., 0.],[0., 1., 0.],[0., 0., 1.]])
    K /= k_size                                # array([[0.33333333, 0., 0.],[0., 0.33333333, 0.],[0., 0., 0.33333333]])

    #Zero padding
    pad = k_size // 2
    out = np.zeros((H + pad * 2, W + pad * 2, C), dtype=np.float)     #out.shape >>> (130, 130, 3)
    out[pad:pad+H, pad:pad+W] = img.copy().astype(np.float)
    tmp = out.copy()

    #filtering
    for y in range(H):
        for x in range(W):
            for c in range(C):
                out[pad + y, pad + x, c] = np.sum(K * tmp[y:y+k_size, x:x+k_size, c])
    out = out[pad: pad + H, pad: pad + W].astype(np.uint8)

    return out

#Motion filter
img12 = motion_filter(img, k_size=3)
#Save the result
cv2.imwrite(OUT_DIR + 'ans12.jpg', img12)
#Display image
cv2_imshow(img12)
cv2.waitKey(0)
cv2.destroyAllWindows()

img12.png

Q.13. MAX-MIN filter

Implement the MAX-MIN filter (3x3). The MAX-MIN filter is a filter that outputs the difference between the maximum value and the minimum value of the pixels in the filter, and is one of the edge detection filters. Edge detection is to detect lines in an image, and such an operation of extracting information in an image is called feature extraction. Edge detection often filters grayscale images.

A13


def max_min_filter(img, k_size=3):
    """
    Max-Min filter(A filter that takes a diagonal mean)
Since it is a grayscale process, it is divided into cases for color images and cases.

    parameters
    -------------------------
    param1: numpy.ndarray format image
    param2:Kernel size

    returns
    -------------------------
    (130x130)Numpy.ndarray format image
    """
    #When the input image is color
    if len(img.shape) == 3:
        # H(height), W(width), C(color)
        H, W, C = img.shape

        #Zero padding
        pad = k_size // 2
        out = np.zeros((H + pad * 2, W + pad * 2, C), dtype=np.float)     #out.shape >>> (130, 130, 3)
        out[pad:pad+H, pad:pad+W] = img.copy().astype(np.float)
        tmp = out.copy()

        #filtering
        for y in range(H):
            for x in range(W):
                for c in range(C):
                    #Subtract the minimum from the maximum in the 3x3 kernel
                    out[pad + y, pad + x, c] = np.max(tmp[y:y+k_size, x:x+k_size, c]) - np.min(tmp[y:y+k_size, x:x+k_size, c])
        out = out[pad: pad + H, pad: pad + W].astype(np.uint8)

    #When the input image is grayscale
    else:
        H, W = img.shape
        #Zero padding
        pad = k_size // 2
        out = np.zeros((H + pad * 2, W + pad * 2), dtype=np.float)
        out[pad:pad+H, pad:pad+W] = img.copy().astype(np.float)
        tmp = out.copy()

        #filtering
        for y in range(H):
            for x in range(W):
                #Subtract the minimum from the maximum in the 3x3 kernel
                out[pad + y, pad + x] = np.max(tmp[y:y+k_size, x:x+k_size]) - np.min(tmp[y:y+k_size, x:x+k_size])

        out = out[pad: pad + H, pad: pad + W].astype(np.uint8)

    return out

# Max-Min filter
img13 = max_min_filter(img, k_size=3)
#Save the result
cv2.imwrite(OUT_DIR + 'ans13.jpg', img13)
#Display image
cv2_imshow(img13)
cv2.waitKey(0)
cv2.destroyAllWindows()

img13.png

Reference: Murakami Laboratory Image Processing-Image Processing / Understanding Laboratory

Q.14. Derivative filter

Implement a differential filter (3x3). The differential filter is a filter that extracts the edge of the portion where the sudden change in brightness occurs, and takes the difference between adjacent pixels. It is the part where the brightness changes drastically that becomes an edge in the image. The color of the red part hardly changes, but the color of the blue frame changes drastically. This change becomes the edge.

A14


"""
cv2.Sobel(src, ddepth, dx, dy[, dst[, ksize[, scale[, delta[, borderType]]]]])
src:Input image
ddepth:Output color depth
dx:Derivative order in the x direction
dy:Derivative order in the y direction
ksize:Kernel size, 1, 3, 5,Specify any of 7
"""
#Lateral direction
dx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
#Longitudinal direction
dy = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)

#Save the result
cv2.imwrite(OUT_DIR + 'ans14_v.jpg', dy)
cv2.imwrite(OUT_DIR + 'ans14_h.jpg', dx)

#Display image
cv2_imshow(dy)
cv2_imshow(dx)
cv2.waitKey(0)
cv2.destroyAllWindows()

img14_h.pngimg14_v.png

Impression that the white part is clearer than the image of the model answer

Reference: Edge extraction with python + OpenCV (Sobel filter, Laplacian filter)

Q.15. Prewitt filter

Implement the Prewitt filter (3x3). The Prewitt filter is a type of edge extraction filter and is defined by the following equation. This is an extension of the differential filter to 3x3.

A15


"""
Prewitt(Previt)Is one of the spatial filters that extracts contours from an image
cv2.filter2D(src, -1, kernel)
src input image
cv2.CV_64F float64
kernel filter kernel(* Give as a NumPy array)
"""

#Kernel (for horizontal and vertical contour detection)
kernel_x = np.array([[1, 0, -1],
                     [1, 0, -1],
                     [1, 0, -1]])

kernel_y = np.array([[1, 1, 1],
                     [0, 0, 0],
                     [-1, -1, -1]])

dx = cv2.filter2D(gray, cv2.CV_64F, kernel_x)
dy = cv2.filter2D(gray, cv2.CV_64F, kernel_y)

#Save the result
cv2.imwrite(OUT_DIR + 'ans15_v.jpg', dy)
cv2.imwrite(OUT_DIR + 'ans15_h.jpg', dx)

#Display image
cv2_imshow(dy)
cv2_imshow(dx)
cv2.waitKey(0)
cv2.destroyAllWindows()

img15_h.pngimg15_v.png

Reference: First-order differential filter Prewitt filter --edge extraction of image Reference: [Python / OpenCV] Contour detection with Prewitt filter

Q.16. Sobel filter

Implement the Sobel filter (3x3). The Sobel filter is also a filter that extracts edges, and is defined by the following equation. This is a filter with a weight in the center of the prewitt filter.

A16


"""
The Sobel filter also emphasizes edges with little difference in brightness.
cv2.filter2D(src, -1, kernel)
src input image
cv2.CV_64F float64
kernel filter kernel(* Give as a NumPy array)
"""

#Kernel (for horizontal and vertical contour detection)
kernel_x = np.array([[1, 0, -1],
                     [2, 0, -2],
                     [1, 0, -1]])

kernel_y = np.array([[1, 2, 1],
                     [0, 0, 0],
                     [-1, -2, -1]])

dx = cv2.filter2D(gray, cv2.CV_64F, kernel_x)
dy = cv2.filter2D(gray, cv2.CV_64F, kernel_y)

#Save the result
cv2.imwrite(OUT_DIR + 'ans16_v.jpg', dy)
cv2.imwrite(OUT_DIR + 'ans16_h.jpg', dx)

#Display image
cv2_imshow(dy)
cv2_imshow(dx)
cv2.waitKey(0)
cv2.destroyAllWindows()

img16_h.pngimg16_v.png

Reference: 1st derivative filter Sobel filter --edge extraction of image Reference: [Python / OpenCV] Contour detection with Prewitt filter

Q.17. Laplacian filter

Implement the Laplacian filter. The Laplacian filter is a filter that detects edges by taking the second derivative of brightness. Since digital images are discrete data, the first derivative in the x and y directions is expressed by the following equations, respectively. (Same as differential filter)

A17


"""
Laplacian filter(Laplacian Filter)Is a spatial filter that uses quadratic differentiation to extract contours from an image
cv2.filter2D(src, -1, kernel)
src input image
cv2.CV_64F float64
kernel filter kernel(* Give as a NumPy array)
"""

#kernel
kernel = np.array([[0, 1, 0],
                     [1, -4, 1],
                     [0, 1, 0]])

#Laplacian filter
img17 = cv2.filter2D(gray, cv2.CV_64F, kernel)

#Save the result
cv2.imwrite(OUT_DIR + 'ans17.jpg', img17)

#Display image
cv2_imshow(img17)
cv2.waitKey(0)
cv2.destroyAllWindows()

img17.png

Reference: [Python / OpenCV] Contour detection with Laplacian filter (edge extraction)

Q.18. Emboss filter

Implement the Emboss filter. The Emboss filter is a filter that makes the outline part stand out, and is defined by the following equation.

A18


"""
Embossed filter(Emboss Filter)Is a spatial fill that highlights the contours
cv2.filter2D(src, -1, kernel)
src input image
cv2.CV_64F float64
kernel filter kernel(* Give as a NumPy array)
"""

#kernel
kernel = np.array([[-2, -1, 0],
                     [-1, 1, 1],
                     [0, 1, 2]])

#Laplacian filter
img18 = cv2.filter2D(gray, cv2.CV_64F, kernel)

#Save the result
cv2.imwrite(OUT_DIR + 'ans18.jpg', img18)

#Display image
cv2_imshow(img18)
cv2.waitKey(0)
cv2.destroyAllWindows()

img18.png

Reference: [Python / OpenCV] Process with emboss filter

Q.19. LoG filter

Implement a LoG filter (sigma = 3, kernel size = 5) and detect the edges of imori_noise.jpg. The LoG filter is the Laplacian of Gaussian, which is a filter that smoothes an image with a Gaussian filter and then extracts contours with a Laplacian filter. Since the Laplcian filter takes the second derivative, the noise is suppressed by the Gaussian filter in advance in order to prevent the noise from being emphasized. The LoG filter is defined by the following equation.

A19



"""
LoG filter(Laplacian Of Gaussian Filter)Is a filter that combines a Gaussian filter and a Laplacian filter.
After smoothing the image with a Gaussian filter to reduce noise, the Laplacian filter extracts the contour.

Gaussian filter
    cv2.GaussianBlur(src, ksize, sigmaX)
    src:Input image, ksize:Kernel size, sigmaX:Gaussian distribution sigma_x
Laplacian filter
    cv2.filter2D(src, -1, kernel)
src input image
    cv2.CV_64F float64
kernel filter kernel(* Give as a NumPy array)
"""

#Gaussian filter
gauss_img = cv2.GaussianBlur(gray_noise, ksize=(3, 3), sigmaX=1.3)

#kernel
kernel = np.array([[0, 0, 1, 0, 0],
                [0, 1, 2, 1, 0],
                [1, 2, -16, 2, 1],
                [0, 1, 2, 1, 0],
                [0, 0, 1, 0, 0]])

#Laplacian filter
img19 = cv2.filter2D(gauss_img, cv2.CV_64F, kernel)

#Save the result
cv2.imwrite(OUT_DIR + 'ans19.jpg', img19)

#Display image
cv2_imshow(img19)
cv2.waitKey(0)
cv2.destroyAllWindows()

img19.png

An image different from the answer. But when I use a Laplacian filter, I feel like this. Please let me know if you make a mistake.

Reference: [Image processing] LoG filter principle / features / calculation formula

Q.20. Histogram display

Use matplotlib to display the histogram of imori_dark.jpg. The histogram is a graph of the number of appearances of pixels. Since matplotlib already has a function called hist (), use it.

A


#histogram
"""
matplotlib.pyplot.hist(x, bins=10, range=None, normed=False, weights=None,
                       cumulative=False, bottom=None, histtype='bar',
                       align='mid', orientation='vertical', rwidth=None,
                       log=False, color=None, label=None, stacked=False,
                       hold=None, data=None, **kwargs)
x (Mandatory)An array of raw data for creating a histogram.
bins bins(Bar to display)Number of. Number of classes.(Default value: 10)
Specify the minimum and maximum values for the range bin.(Default value: (x.min(), x.max()))
rwidth Specify the width of each bar as a numerical value or an array.
"""
# ravel:Multidimensional list to one-dimensional list,Number of bins:255,Range 0~255
plt.hist(img_dark.ravel(), bins=255, rwidth=0.8, range=(0, 255))
plt.savefig("out.png ")
plt.show()

Reference: Data science in Python

Impressions

In the official version, numpy is used and it is done according to the principle, but OpenCV is used as much as possible to make it easy to implement.

Recommended Posts

100 image processing knocks !! (011 --020) Early game
Image processing 100 knocks ①
100 image processing knocks !! (001 --010) Carefully and carefully
Image processing with Python 100 knocks # 3 Binarization
Image processing 100 knocks Q9, Q10 (filter) speedup
Image processing with Python 100 knocks # 2 Grayscale
[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): 47
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 (2020): 49
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: 41
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
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