SoFunction
Updated on 2024-11-17

20 lines of code to teach you to use python to change the background color of the ID card photo method example

1. Photo credits

This image is from Baidu Images, if infringement, please contact me to remove! The image is for knowledge exchange only.

在这里插入图片描述

2. Read the picture and display

  • imread(): read the image;
  • imshow(): show the picture;
  • waitkey(): set the window to wait, if not, the window will flash;
import cv2
import numpy as np
# Read the photos
img=('')

# Display image
('img',img)

# Command for window wait, 0 means infinite wait
(0)

The effect is as follows:

在这里插入图片描述

3.Picture scaling

resize(): resize the image, where fx and fy means scale, 0.5 means scale to half of the previous one.

import cv2
import numpy as np
# Read the photos
img=('')

# Image scaling
img = (img,None,fx=0.5,fy=0.5)
rows,cols,channels = 
print(rows,cols,channels)

# Display image
('img',img)

# Command for window wait, 0 means infinite wait
(0)

The results are as follows:

在这里插入图片描述

4. Convert pictures to grayscale images

Tri-color images have three color channels of RGB, which can't perform the operation of erosion and expansion. This one requires us to convert the color image to hsv grayscale image and then complete the operation of erosion and expansion.

(img,cv2.COLOR_BGR2HSV) can convert color image to hsv grayscale image.

import cv2
import numpy as np
# Read the photos
img=('')

# Image scaling
img = (img,None,fx=0.5,fy=0.5)
rows,cols,channels = 
print(rows,cols,channels)
('img',img)

# Pictures converted to binarized graphs
hsv = (img,cv2.COLOR_BGR2HSV)

# Display image
('hsv',hsv)

# Command for window wait, 0 means infinite wait
(0)

The results are as follows:

在这里插入图片描述

5. Binarize the image

Binarization is done to convert the image to black and white. Binarization is similar to 1 for male and 2 for female, for image processing we also need to customize a minimum and maximum value, here with lower_blue and upper_blue respectively

  • lower_blue = ([90,70,70])
  • upper_blue = ([110,255,255])
  • inRange(hsv, lower_blue, upper_blue) binarizes the image.
import cv2
import numpy as np
# Read the photos
img=('')

# Image scaling
img = (img,None,fx=0.5,fy=0.5)
rows,cols,channels = 
print(rows,cols,channels)
('img',img)

# Pictures converted to grayscale
hsv = (img,cv2.COLOR_BGR2HSV)
('hsv',hsv)

# Binarization of images
lower_blue = ([90,70,70])
upper_blue = ([110,255,255])
mask = (hsv, lower_blue, upper_blue)


# Display image
('mask',mask)

# Command for window wait, 0 means infinite wait
(0)

The results are as follows:

在这里插入图片描述

Disadvantages: We observe the third chapter of the picture, found that the black area will sometimes appear some noise (white spots), here may not show very obvious, some pictures show very obvious, which requires us to erosion or expansion.

6. Corrosion and expansion of the image

After binarization of the above image, some noise appeared, we can use corrosion or expansion for the processing of the picture, and observe which one has a better processing effect.

  • erode(mask,None,iterations=1) to perform the erosion operation.
  • dilate(erode,None,iterations=1) performs the dilation operation.
import cv2
import numpy as np
# Read the photos
img=('')

# Image scaling
img = (img,None,fx=0.5,fy=0.5)
rows,cols,channels = 
print(rows,cols,channels)
('img',img)

# Pictures converted to grayscale
hsv = (img,cv2.COLOR_BGR2HSV)
('hsv',hsv)

# Binarization of images
lower_blue=([90,70,70])
upper_blue=([110,255,255])
mask = (hsv, lower_blue, upper_blue)


#Corrosion Expansion
erode=(mask,None,iterations=1)
('erode',erode)

dilate=(erode,None,iterations=1)
('dilate',dilate)


# Command for window wait, 0 means infinite wait
(0)

The results are as follows:

在这里插入图片描述

Observe the above picture: for this picture, either corrosion or expansion, both play a good operation to remove the noise from the picture, we use the corroded picture can also be, we use the expansion of the picture can also be.

7. Iterate over each pixel point for color replacement

The image is made up of every pixel, we just want to find the pixel at the white background color of the image after corrosion, and then replace the pixel at the corresponding position in the original image with the red color.

import cv2
import numpy as np
# Read the photos
img=('')

# Image scaling
img = (img,None,fx=0.5,fy=0.5)
rows,cols,channels = 
print(rows,cols,channels)
('img',img)

# Pictures converted to grayscale
hsv = (img,cv2.COLOR_BGR2HSV)
('hsv',hsv)

# Binarization of images
lower_blue=([90,70,70])
upper_blue=([110,255,255])
mask = (hsv, lower_blue, upper_blue)


#Corrosion Expansion
erode=(mask,None,iterations=1)
('erode',erode)

dilate=(erode,None,iterations=1)
('dilate',dilate)

# Iterative replacement
for i in range(rows):
 for j in range(cols):
  if erode[i,j]==255: # A pixel point of 255 means it's white, and we're replacing the pixel point at the white color with a red color.
   img[i,j]=(0,0,255) # Replace the color here, with a BGR channel, not an RGB channel
('res',img)

# Command for window wait, 0 means infinite wait
(0)

The effect is as follows:

在这里插入图片描述

to this article on the 20 lines of code to teach you to use python to change the background color of the photo ID method example of the article is introduced to this, more related python photo ID to change the background color of the contents of the search for my previous posts or continue to browse the following related articles I hope that you will support me in the future more!