Python White's topic report - OpenCV matting project practice

Python Xiaobai's topic report - actual combat of OpenCV matting project (5)

This series is Python Xiaobai's task "image segmentation and matting based on OpenCV".
It should be noted that this series is not a matting project tutorial of OpenCV, but a topic report on this topic. It includes a relatively complete PyQt project.
From the perspective of students' project homework report, it can still be dried out for your reference.

Welcome to pay attention "Python Xiaobai's project practice @ youcans" Original works
Python Xiaobai's topic report - actual combat of OpenCV matting project (1)
Python Xiaobai's topic report - actual combat of OpenCV matting project (2)
Python Xiaobai's topic report - actual combat of OpenCV matting project (3)
Python Xiaobai's topic report - actual combat of OpenCV matting project (4)
Python Xiaobai's topic report - actual combat of OpenCV matting project (5)


Chapter 3 threshold matting

The threshold processing method is intuitive and simple. It is a basic image segmentation method.

The threshold value is appropriately selected according to the overall or partial information of the image. When the pixel value is higher than the threshold value, it is set to 1 / 255, and when it is lower than the threshold value, it is set to 0. In this way, the region of interest in the image is filtered out to generate a mask, and then combined with the original image to obtain the matting image.

Because the gray level series and gray level histogram of the image to be processed are uncertain, it is necessary to select an appropriate threshold processing method for image segmentation for different images and different target prospects. In this chapter, fixed threshold, adaptive threshold and color range are used for image matting.


3.2 adaptive threshold matting

In the application of image matting, noise and uneven illumination will affect the performance of threshold algorithm. For many images, it is difficult to obtain understandable matting effect by using fixed threshold, so we need to use adaptive threshold processing method and variable threshold processing.

Adaptive threshold processing can calculate the local threshold according to the brightness distribution of different regions of the image, using Gaussian filtering, moving average and other methods, so as to obtain the matting image.

OpenCV provides the function cv Adaptive threshold() implements image threshold processing.

Function Description:

cv.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst]) → dst

Parameter Description:

  • scr: input image, 8-bit single channel image
  • dst: output image with the same size and type as the input image
  • maxValue: the maximum value setting for binary processing, usually 255
  • Adaptive method: adaptive threshold algorithm
    • cv.ADAPTIVE_THRESH_MEAN_C: The threshold is the mean of the pixel values in the rectangular neighborhood
    • cv.ADAPTIVE_THRESH_GAUSSIAN_C: The threshold value is the weighted mean value of the pixels in the rectangular neighborhood, and the default weight follows Gaussian distribution
  • thresholdType: threshold type; can be THRESH_BINARY or THRESH_BINARY_INV
  • blockSize: adaptively calculated neighborhood size, such as 3, 5, 7, etc
  • C: Offset

The basic procedure of image matting using adaptive threshold is as follows:

# MattingAdaptThresh.py
# Copyright 2021 youcans, XUPT
# Crated: 2021-12-10

import cv2
import numpy as np
from matplotlib import pyplot as plt

# 1. Read the original image
imgOri = cv2.imread("../images/lady983Green.jpg")  # Read original image
height, width, channels = imgOri.shape

# 2. Extract the green channel from the original image
imgGray = cv2.cvtColor(imgOri, cv2.COLOR_BGR2GRAY)  # Convert color image to gray image
imgGreen = imgOri[:, :, 1]  # imgGreen is the color intensity map of the green channel (note that it is not the gray conversion result of the original image)
print(imgOri.shape, imgGray.shape, imgGreen.shape)

# 3. The green channel is converted into a binary image to generate Mask mask and inverse Mask MaskInv
# colorThresh = 245  # Color threshold of green screen background (pay attention to the influence of threshold)
# ret, binary = cv2.threshold(imgGreen, colorThresh, 255, cv2.THRESH_BINARY)  # Convert to binary image, generate mask, and black mask the matting area

# Adaptive thresholding can adaptively change the threshold according to the brightness distribution of different regions of the image
# cv.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst]) -> dst
# Parameter adaptivemethod: adaptive_ THRESH_ MEAN_ C (mean method), adaptive_ THRESH_ GAUSSIAN_ C (Gaussian method)
# Parameter thresholdtype: thresh_ Binary (less than 0 threshold), thresh_ BINARY_ Inv (0 above threshold)
# Parameter blockSize: neighborhood size, positive odd
# Parameter C: offset, generally 0
binary = cv2.adaptiveThreshold(imgGreen, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 5, 0)
binaryInv = cv2.bitwise_not(binary)  # By bit non (black-and-white transpose), an inverse mask is generated, the matting area is white, the window is opened, and the area outside the matting is black

# 4. Matting and changing background with mask
# Generate matting image (foreground reserved, background black)
imgMatte = cv2.bitwise_and(imgOri, imgOri, mask=binaryInv)  # Generate matting foreground, and output black in the inverse mask area other than standard matting
# Change the background color to red: modify the inverse mask (black outside the matting area)
imgReplace = imgOri.copy()
imgReplace[binaryInv == 0] = [0, 0, 255]  # The black area (0 / 0 / 0) is changed to red (BGR:0/0/255)

plt.figure(figsize=(12, 8))
plt.subplot(231), plt.imshow(cv2.cvtColor(imgOri, cv2.COLOR_BGR2RGB)), plt.title("Origin image"), plt.axis('off')
plt.subplot(232), plt.imshow(imgGray, cmap='gray'), plt.title("Gray image"), plt.axis('off')
plt.subplot(233), plt.imshow(imgGreen, cmap='gray'), plt.title("Green channel level"), plt.axis('off')
plt.subplot(234), plt.imshow(binary, cmap='gray'), plt.title("binary mask"), plt.axis('off')
plt.subplot(235), plt.imshow(binaryInv, cmap='gray'), plt.title("inv-binary mask"), plt.axis('off')
plt.subplot(236), plt.imshow(cv2.cvtColor(imgReplace, cv2.COLOR_BGR2RGB)), plt.title("BgColor changed"), plt.axis('off')
plt.tight_layout()

The result of matting monochrome background image using adaptive threshold method is shown in Figure 3.4.
For the blue screen matting to determine the color background, the result of the adaptive threshold method is not better, but it can still extract the image features well. If other methods are used for further processing, a satisfactory matting effect can also be obtained.
For natural background image matting, the performance of adaptive threshold method is better than that of fixed threshold method.



Figure 3.4 adaptive threshold matting method

[end of this section]


Copyright notice:

Welcome to pay attention "Python Xiaobai's project practice @ youcans" Original works

Original works, reprint must be marked with the original link: https://blog.csdn.net/youcans/article/details/122296231

Copyright 2022 youcans, XUPT

Crated: 2022-01-01


Welcome to pay attention "Python Xiaobai's project practice @ youcans" Original works
Python Xiaobai's topic report - actual combat of OpenCV matting project (1)
Python Xiaobai's topic report - actual combat of OpenCV matting project (2)
Python Xiaobai's topic report - actual combat of OpenCV matting project (3)
Python Xiaobai's topic report - actual combat of OpenCV matting project (4)
Python Xiaobai's topic report - actual combat of OpenCV matting project (5)

Keywords: Python OpenCV image processing PyQt5

Added by mattison on Thu, 06 Jan 2022 02:45:46 +0200