Image processing 100 knocks ①

I have recently started working on 100 image processing knocks , so some implementation examples. This time, I picked up some from Q1-10.

Q6 Color reduction processing

The processing formula is as follows

val = \left\{
\begin{array}{ll}
32 & (0 \leq x < 64) \\
96 & (64 \leq x < 128) \\ 
96 & (128 \leq x < 192) \\ 
96 & (192 \leq x < 256) \\ 
\end{array}
\right.

python


def reduce(pix):
    if 0 <= pix < 64:
        return 32
    elif 64 <= pix < 128:
        return 96
    elif 128 <= pix <192:
        return 160
    else:
        return 224

def reduce_color(img):
    return np.vectorize(reduce)(img)

Q7 Average Pooling

python


def average_pool(img, f=8):
    height, width, channel = img.shape
    n = int(width/f)
    out = np.empty(shape=(n, n, channel))
    
    print(out.shape)
    
    
    for h in range(n):
        for w in range(n):
            for c in range(channel):
                vert_start = h * f
                vert_end = vert_start + f
                horiz_start = w * f
                horiz_end = horiz_start + f
                
                val = np.sum(img[vert_start:vert_end, horiz_start:horiz_end, c]) / f**2
                
                out[h, w, c] = val
    
    return out.astype(np.int32)

Q8: Max Pooling

python


def max_pool(img, f=8):
    height, width, channel = img.shape
    n = int(width/f)
    out = np.empty(shape=(n, n, channel))
    
    print(out.shape)
    
    
    for h in range(n):
        for w in range(n):
            for c in range(channel):
                vert_start = h * f
                vert_end = vert_start + f
                horiz_start = w * f
                horiz_end = horiz_start + f
                
                val = np.max(img[vert_start:vert_end, horiz_start:horiz_end, c])
                
                out[h, w, c] = val
    
    return out.astype(np.int32)

Q9 Gaussian filter

I thought it was simple to do, but unexpectedly I had a hard time due to a mistake in the subscript. .. .. For the kernel implementation, I referred to the answer example.

python


def gaussian_filter(img, size=3, sigma=1.3):
    height, width, channel = img.shape
    
    # padding
    pad = size//2
    img_pad = img.copy()
    img_pad = np.pad(img_pad, [(pad, pad), (pad, pad), (0, 0)], 'constant', constant_values=(0,0))
    
    # PREPARE KERNEL
    K = np.zeros(shape=(size, size), dtype=np.float32)
    for x in range(-pad, -pad+size):
        for y in range(-pad, -pad+size):
            K[y+pad, x+pad] = (1/((2 * np.pi * sigma * sigma)) * np.exp(-(x**2+y**2) / (2*(sigma**2))))
    K /= K.sum()
    
    tmp = img_pad.copy()
    
    for h in range(height):
        for w in range(width):
            for c in range(channel):
                tmp[h+pad, w+pad, c] = np.sum(K*img_pad[h:h+size,  w:w+size, c])
    
    tmp = tmp[pad:pad+height, pad:pad+width, :].astype(np.int32)
                
    return tmp

Q10 Median filter

python


def median_filter(img, size=3):
    height, width, channel = img.shape
    out = img.copy()
    
    # padding 
    pad = size//2
    out = img.copy()
    out = np.pad(out, [(pad, pad), (pad, pad), (0, 0)], 'constant', constant_values=(0,0))
    
    assert out.shape == (img.shape[0]+2*pad, img.shape[1]+2*pad, img.shape[2])
    
    # Apply filter
    tmp = out.copy()
    
    for h in range(height):
        for w in range(width):
            for c in range(channel):
                out[h+pad, w+pad, c] = np.median(tmp[h:h+size, w:w+size, c])
    
    out = out[pad:H, pad:W, :].astype(np.uint8)
    
    return out

Recommended Posts

Image processing 100 knocks ①
100 image processing knocks !! (011 --020) Early game
100 image processing knocks !! (001 --010) Carefully and carefully
100 language processing knocks 03 ~ 05
100 language processing knocks (2020): 40
100 language processing knocks (2020): 32
python image processing
100 language processing knocks (2020): 47
100 language processing knocks (2020): 22
100 language processing knocks (2020): 26
100 language processing knocks (2020): 34
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
Image processing with Python 100 knocks # 3 Binarization
Image processing 100 knocks Q9, Q10 (filter) speedup
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 # 2 Grayscale
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
100 language processing knocks (2020): 36
Image processing with Python 100 knocks # 7 Average pooling
Image processing with Python 100 knocks # 9 Gaussian filter
100 amateur language processing knocks: 41
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: 92
100 amateur language processing knocks: 06
100 amateur language processing knocks: 81
100 amateur language processing knocks: 46
100 amateur language processing knocks: 88
100 amateur language processing knocks: 89
Flat Field image processing
100 amateur language processing knocks: 43
100 amateur language processing knocks: 55
100 amateur language processing knocks: 94
100 amateur language processing knocks: 54
First Python image processing
100 amateur language processing knocks: 63
100 amateur language processing knocks: 78