当前位置:网站首页>【OpenCV 例程200篇】220.对图像进行马赛克处理
【OpenCV 例程200篇】220.对图像进行马赛克处理
2022-07-06 12:39:00 【小白YouCans】
文章目录:『youcans 的 OpenCV 例程200篇 - 总目录』
【youcans 的 OpenCV 例程200篇】220.对图像进行马赛克处理
9. 图像的马赛克处理
马赛克效果是广泛使用的图像和视频处理方法。将图像中指定区域的色阶细节劣化,造成色块模糊的效果,看上去像是一个个小格子组成的色块,称为马赛克。马赛克效果的主要目的是使特定区域的细节无法辨认,经常用于遮挡人物脸部、隐私信息。
马赛克的方法很简单,将处理区域划分为一个个小方块,每个小方块内所有像素置为相同的或相似的像素值。例程 A4.13 给出了一个简单的实施案例。
马赛克方块的尺寸越大,图像越模糊,马赛克区域图像丢失的细节越多。
这与图像多尺度像素采样是相似的:图像向下采样,分辨率逐级降低。对整幅图像进行马赛克处理,与图像下采样是等效的;对图像局部进行马赛克处理,相当于原始图像与局部图像下采样的融合。
上采样和下采样是不可逆的,将下采样的图像还原回原来尺寸时会丢失高频信息,使图片变模糊。因此,消除图像马赛克在原理上是不可能的。但是,通过图片像素临近点插值的填充算法,可以增强马赛克区域的视觉效果。近年来,随着 AI 技术的发展,基于对大量类似清晰图像的学习,使用 AI 算法可以较好地还原图像,识别被马赛克遮挡的人脸或文本,取得了很好的效果。
例程 A4.13:对图像指定区域进行马赛克处理
对于选定的 ROI 区域进行马赛克处理。马赛克方块的尺寸越大,图像越模糊,马赛克区域图像丢失的细节越多。
# A4.13 对图像指定区域进行马赛克处理
img = cv.imread("../images/imgLena.tif", 1) # 加载原始图片,单通道
roi = cv.selectROI(img, showCrosshair=True, fromCenter=False)
x, y, wRoi, hRoi = roi # 矩形裁剪区域的位置参数
# x, y, wRoi, hRoi = 208, 176, 155, 215 # 矩形裁剪区域
imgROI = img[y:y+hRoi, x:x+wRoi].copy() # 切片获得矩形裁剪区域
print(x, y, wRoi, hRoi)
plt.figure(figsize=(9, 6))
plt.subplot(231), plt.title("Original image"), plt.axis('off')
plt.imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB))
plt.subplot(232), plt.title("Region of interest"), plt.axis('off')
plt.imshow(cv.cvtColor(imgROI, cv.COLOR_BGR2RGB))
mosaic = np.zeros(imgROI.shape, np.uint8) # ROI 区域
ksize = [5, 10, 20] # 马赛克块的宽度
for i in range(3):
k = ksize[i]
for h in range(0, hRoi, k):
for w in range(0, wRoi, k):
color = imgROI[h,w]
mosaic[h:h+k,w:w+k,:] = color # 用顶点颜色覆盖马赛克块
imgMosaic = img.copy()
imgMosaic[y:y + hRoi, x:x + wRoi] = mosaic
plt.subplot(2,3,i+4), plt.title("Coding image (size={})".format(k)), plt.axis('off')
plt.imshow(cv.cvtColor(imgMosaic, cv.COLOR_BGR2RGB))
plt.subplot(233), plt.title("Mosaic"), plt.axis('off')
plt.imshow(cv.cvtColor(mosaic, cv.COLOR_BGR2RGB))
plt.show()
【本节完】
版权声明:
[email protected] 原创作品,转载必须标注原文链接:(https://blog.csdn.net/youcans/article/details/125522759)
Copyright 2022 youcans, XUPT
Crated:2022-6-30
边栏推荐
- Rhcsa Road
- OLED屏幕的使用
- BUUCTF---Reverse---easyre
- 永磁同步电机转子位置估算专题 —— 基波模型类位置估算概要
- PHP online examination system version 4.0 source code computer + mobile terminal
- Spiral square PTA
- Function optimization and arrow function of ES6
- 2022 refrigeration and air conditioning equipment installation and repair examination contents and new version of refrigeration and air conditioning equipment installation and repair examination quest
- 22-07-05 upload of qiniu cloud storage pictures and user avatars
- Notes on beagleboneblack
猜你喜欢
Leetcode question 283 Move zero
Implementation of packaging video into MP4 format and storing it in TF Card
[DSP] [Part 2] understand c6678 and create project
Core principles of video games
Pytest (3) - Test naming rules
永磁同步电机转子位置估算专题 —— 基波模型与转子位置角
OAI 5G NR+USRP B210安装搭建
数字三角形模型 AcWing 1018. 最低通行费
B-杰哥的树(状压树形dp)
use. Net analysis Net talent challenge participation
随机推荐
02 basic introduction - data package expansion
Can novices speculate in stocks for 200 yuan? Is the securities account given by qiniu safe?
Rhcsa Road
2022 nurse (primary) examination questions and new nurse (primary) examination questions
JVM_ Common [interview questions]
【每周一坑】计算100以内质数之和 +【解答】输出三角形
[asp.net core] set the format of Web API response data -- formatfilter feature
Gui Gui programming (XIII) - event handling
2022 Guangdong Provincial Safety Officer C certificate third batch (full-time safety production management personnel) simulation examination and Guangdong Provincial Safety Officer C certificate third
过程化sql在定义变量上与c语言中的变量定义有什么区别
性能测试过程和计划
Implementation of packaging video into MP4 format and storing it in TF Card
02 基础入门-数据包拓展
Build your own application based on Google's open source tensorflow object detection API video object recognition system (IV)
Is it difficult for small and micro enterprises to make accounts? Smart accounting gadget quick to use
Distributed ID
SSH connection denied
JMeter server resource indicator monitoring (CPU, memory, etc.)
I've seen many tutorials, but I still can't write a program well. How can I break it?
2022 refrigeration and air conditioning equipment installation and repair examination contents and new version of refrigeration and air conditioning equipment installation and repair examination quest