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
Release: 2020-09-14 13:13:15
forward
4737 people have browsed it

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. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">import cv2 import numpy as np复制代码</pre><div class="contentsignin">Copy after login</div></div>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复制代码
Copy after login

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)复制代码
Copy after login

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)复制代码
Copy after login

Display image

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

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!

Related labels:
source:juejin.im
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template