Home >Backend Development >Python Tutorial >Detailed explanation of Python implementation of mask processing of images

Detailed explanation of Python implementation of mask processing of images

coldplay.xixi
coldplay.xixiforward
2020-09-14 13:13:154830browse

Detailed explanation of Python implementation of mask processing of images

Related learning recommendations: python tutorial

##Image mask (image mask): Use selected images, graphics or objects to block the image to be processed (partially or completely) to control the area or process of image processing. Since the specific image or object covered is called a mask, when doing image processing, there is a lot of demand for masking the image. Next, I will demonstrate it with the following picture of a cat and dog. I chose Kitten avatar.

First look at the renderings:


Detailed explanation of Python implementation of mask processing of images
Detailed explanation of Python implementation of mask processing of images
Detailed explanation of Python implementation of mask processing of images##Python implementation Mask the image

Import the required library
  • Create the mask image
  • Square mask
    • Circular mask
    Mask and original image are spliced
  • Display image
  • Effect display
  • Summary
  • Import the required libraries

The library resources required this time are

cv2

and numpy, which can be obtained through pip install xxx Download. <pre class="brush:php;toolbar:false">import cv2 import numpy as np复制代码</pre>Create mask image

Creating a mask depends on the size of the image. Create your own mask according to the size of the image. Of course, you can also choose the mask yourself. The masks I created here are square masks and circular masks.

Square mask

The mask coordinates are [10:170, 50:220].

# 创建掩膜
mask = np.zeros([img.shape[0], img.shape[1]], dtype=np.uint8)
mask[10:170, 50:220] = 255复制代码

Circular mask

Mask coordinates:

x = 140

y = 100
r = 80

# 创建掩膜
x = 140
y = 100
r = 80
mask = np.zeros(img.shape[:2], dtype=np.uint8)
mask = cv2.circle(mask, (x, y), r, (255, 255, 255), -1)复制代码

Mask and original Image splicing

Image merging uses cv2.add to splice and merge the mask with the original image.

image = cv2.add(img, np.zeros(np.shape(img), dtype=np.uint8), mask=mask)复制代码

Display image

# 展示原图
cv2.imshow("img", img)
# 展示掩膜图片
cv2.imshow("mask", mask)
# 展示添加掩膜效果图片
cv2.imshow("image", image)复制代码

Effect display

Original image:


Detailed explanation of Python implementation of mask processing of images
Square mask image:


Detailed explanation of Python implementation of mask processing of images
Square mask and original image merged image:


Detailed explanation of Python implementation of mask processing of images
Circular mask image:


Detailed explanation of Python implementation of mask processing of images##Circular mask Merge image with original image:

SummaryDetailed explanation of Python implementation of mask processing of images
The principle of occlusion mask is very simple. First, create a completely black image of the same size as the image. , then change the pixels in the area that needs to be displayed to white, and finally use cv2.add to overlay image and mask to achieve occlusion display of the image.

If you want to know more about programming learning, please pay attention to the

php training
column!

The above is the detailed content of Detailed explanation of Python implementation of mask processing of images. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.im. If there is any infringement, please contact admin@php.cn delete