[PYTHON] Noise removal and background transparency of binary images

Introduction

Earlier, I wrote an article Basics of Binary Image Processing with Python. This is the development of this article.

About execution processing

This time, as the name of the article says, __ · Noise removal __ __ ・ Background transparency __ in addition, __ ・ Inverted image processing __ Also run

Source code


import cv2
import numpy as np

#Loading images
img = cv2.imread('sample2-1.png')

#Grayscale conversion
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

#Threshold setting
threshold_value = 110

#Array creation (for output)
threshold_img = gray.copy()

#Implementation(numpy)
threshold_img[gray < threshold_value] = 0
threshold_img[gray >= threshold_value] = 255

#Output:sample2-2
cv2.imwrite(f'C:\\Users\\[username]\\python\\project1\\sample2-2.png',threshold_img)

#Importing grayscale converted images
img = cv2.imread("sample2-2.png ")
#Noise removal processing
ksize=3
#Median filter
img_mask = cv2.medianBlur(img,ksize)
#Black and white inverted image
img2 = cv2.bitwise_not(img_mask)

#output:sample2-3,2-4
cv2.imwrite("C:\\Users\\[username]\\python\\project1\\sample2-3.png ",img_mask)
cv2.imwrite("C:\\Users\\[username]\\python\\project1\\sample2-4.png ",img2)

#Background transparency

from PIL import Image

#Loading images
org = Image.open( 'sample2-3.png' )

#Create images of the same size
trans = Image.new('RGBA', org.size, (0, 0, 0, 0))

#Vertical and horizontal size
width = org.size[0]
height = org.size[1]
#Process with for statement
for x in range(width):
    for y in range(height):
        pixel = org.getpixel( (x, y) )
        
        #No white treatment
        if pixel[0] == 255 and pixel[1] == 255 and pixel[2] == 255:
            continue
        
        #Writing other than white
        trans.putpixel( (x, y), pixel )
        
# output:sample2-5
trans.save('sample2-5.png')

instructions

From import statement to output: sample2-2

This is almost the same as the code of Basics of Binary Image Processing with Python described above. Only the threshold is changed on the image.

Noise removal

#Noise removal
ksize=3
#Median filter
img_mask = cv2.medianBlur(img,ksize)

This part is a place to set the size of the vicinity where image processing is performed.

Black and white inversion processing

#Black and white inverted image
img2 = cv2.bitwise_not(img_mask)

Inversion processing is built into opencv, and inversion processing is possible only with this statement. By the way, this time. Although it is black and white, it can also be executed with a color image.

Transparent image creation

#Loading images
org = Image.open( 'sample2-3.png' )

#Create images of the same size
trans = Image.new('RGBA', org.size, (0, 0, 0, 0))

#Vertical and horizontal size
width = org.size[0]
height = org.size[1]
#Process with for statement
for x in range(width):
    for y in range(height):
        pixel = org.getpixel( (x, y) )
        
        #No white treatment
        if pixel[0] == 255 and pixel[1] == 255 and pixel[2] == 255:
            continue
        
        #Writing other than white
        trans.putpixel( (x, y), pixel )

Since the process is as described in # above, the description is omitted.

Put these results sample2-1 ~ 2-5.

Original image (sample2-1.png)

sample2-1.png This image is often used in image processing, so I think many people are familiar with it. It is part of a nude image of a woman called Lena. [Explanation about Lena from Wikipedia](https://ja.wikipedia.org/wiki/%E3%83%AC%E3%83%8A_(%E7%94%BB%E5%83%8F%E3%83] % 87% E3% 83% BC% E3% 82% BF)

Binary image processing (sample2-2.png)

sample2-2.png

Noise removal processing (sample2-3.png)

sample2-3.png

Black and white inversion processing (sample2-4.png)

sample2-4.png

Transparency processing other than white (sample2-5.png)

sample2-5.png It's hard to understand in this article, so you can try it yourself by copying the code.

Source code simplification

The previous code made each process easier to see. However, by changing the order, the number of steps can be reduced.


import cv2
import numpy as np
from PIL import Image

#Loading images
img = cv2.imread('sample2-1.png')
#Grayscale conversion
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
#Threshold setting
threshold_value = 110
#Array creation (for output)
threshold_img = gray.copy()
#Implementation(numpy)
threshold_img[gray < threshold_value] = 0
threshold_img[gray >= threshold_value] = 255

#Noise removal processing
ksize=3
#Median filter
img_mask = cv2.medianBlur(img,ksize)

#Black and white inverted image
img2 = cv2.bitwise_not(img_mask)

#Background transparency
#Loading images
org = Image.open('sample2-3.png')

#Create images of the same size
trans = Image.new('RGBA', org.size, (0, 0, 0, 0))

#Vertical and horizontal size
width = org.size[0]
height = org.size[1]
#Process with for statement
for x in range(width):
    for y in range(height):
        pixel = org.getpixel( (x, y) )
        
        #No white treatment
        if pixel[0] == 255 and pixel[1] == 255 and pixel[2] == 255:
            continue
        
        #Writing other than white
        trans.putpixel( (x, y), pixel )
        
#Output:sample2-2~2-5
cv2.imwrite(f'C:\\Users\\[username]\\python\\project1\\sample2-2.png',threshold_img)
cv2.imwrite("C:\\Users\\[username]\\python\\project1\\sample2-3.png ",img_mask)
cv2.imwrite("C:\\Users\\[username]\\python\\project1\\sample2-4.png ",img2)
trans.save('sample2-5.png')

When it is a batch process, an Error may occur, but it was possible with jupyter notebook. Since the image in the process can be read directly without reading, it will be close to the error but faster.

Recommended Posts

Noise removal and background transparency of binary images
Automatically generate images of koalas and bears
Wavelet transform of images with PyWavelets and OpenCV
Display embedded images of mp3 and flac with mutagen
Create a batch of images and inflate with ImageDataGenerator
Search and save images of Chino Kafu from Twitter
Collection and automation of erotic images using deep learning