当前位置:网站首页>【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
边栏推荐
- Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache
- Gui Gui programming (XIII) - event handling
- Kubernetes learning summary (20) -- what is the relationship between kubernetes and microservices and containers?
- Learn to punch in Web
- [cloud lesson] EI lesson 47 Mrs offline data analysis - processing OBS data through Flink
- 小孩子学什么编程?
- 01 基础入门-概念名词
- How does kubernetes support stateful applications through statefulset? (07)
- Rhcsa Road
- Error analysis ~csdn rebound shell error
猜你喜欢
[weekly pit] positive integer factorization prime factor + [solution] calculate the sum of prime numbers within 100
How to upgrade high value-added links in the textile and clothing industry? APS to help
OAI 5G NR+USRP B210安装搭建
强化学习-学习笔记5 | AlphaGo
Tencent byte and other big companies interview real questions summary, Netease architects in-depth explanation of Android Development
[wechat applet] operation mechanism and update mechanism
[weekly pit] calculate the sum of primes within 100 + [answer] output triangle
[DSP] [Part 2] understand c6678 and create project
How does kubernetes support stateful applications through statefulset? (07)
【DSP】【第二篇】了解C6678和创建工程
随机推荐
Application layer of tcp/ip protocol cluster
Force deduction brush question - 98 Validate binary search tree
【每周一坑】正整数分解质因数 +【解答】计算100以内质数之和
Activiti global process monitors activitieventlistener to monitor different types of events, which is very convenient without configuring task monitoring in acitivit
看过很多教程,却依然写不好一个程序,怎么破?
Deep learning classification network -- zfnet
强化学习-学习笔记5 | AlphaGo
Dynamically switch data sources
Recyclerview GridLayout bisects the middle blank area
JMeter server resource indicator monitoring (CPU, memory, etc.)
Summary of different configurations of PHP Xdebug 3 and xdebug2
SQL injection 2
【DSP】【第一篇】开始DSP学习
"Penalty kick" games
Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache
Spiral square PTA
华为设备命令
01 basic introduction - concept nouns
2022 construction electrician (special type of construction work) free test questions and construction electrician (special type of construction work) certificate examination
APS taps home appliance industry into new growth points