跟我学Python图像处理丨基于灰度三维图的图像顶帽运算和黑帽运算

摘要:本篇文章结合灰度三维图像讲解图像顶帽运算和图像黑猫运算,通过Python调用OpenCV函数实现。

本文分享自华为云社区《​​[Python图像处理] 十三.基于灰度三维图的图像顶帽运算和黑帽运算​​》,作者: eastmount。

本篇文章继续深入,结合灰度三维图像讲解图像顶帽运算和图像黑猫运算,通过Python调用OpenCV函数实现。

一.图像顶帽运算

图像顶帽运算(top-hat transformation)又称为图像礼帽运算,它是用原始图像减去图像开运算后的结果,常用于解决由于光照不均匀图像分割出错的问题。其公式定义如下:

跟我学Python图像处理丨基于灰度三维图的图像顶帽运算和黑帽运算

图像封顶操作使用结构元素,通过打开操作从图像中移除对象,以纠正光照不均匀的影响,效果如下图所示。

[En]

The image cap operation uses a structural element to remove an object from an image through an open operation to correct the influence of uneven illumination, and the effect is shown in the following image.

跟我学Python图像处理丨基于灰度三维图的图像顶帽运算和黑帽运算

在Python中,图像顶帽运算主要调用morphologyEx()实现,其中参数cv2.MORPH_TOPHAT表示顶帽处理,函数原型如下:

dst = cv2.morphologyEx(src, cv2.MORPH_TOPHAT, kernel)

  • src表示原始图像
  • cv2.MORPH_TOPHAT表示图像顶帽运算
  • kernel表示卷积核,可以用numpy.ones()函数构建

假设存在一张光照不均匀的米粒图像,如图所示,我们需要调用图像顶帽运算解决光照不均匀的问题。其Python代码如下所示:

跟我学Python图像处理丨基于灰度三维图的图像顶帽运算和黑帽运算
#encoding:utf-8import cv2  import numpy as np  #读取图片src = cv2.imread('test06.png', cv2.IMREAD_UNCHANGED)#设置卷积核kernel = np.ones((10,10), np.uint8)#图像顶帽运算result = cv2.morphologyEx(src, cv2.MORPH_TOPHAT, kernel)#显示图像cv2.imshow("src", src)cv2.imshow("result", result)#等待显示cv2.waitKey(0)cv2.destroyAllWindows()

运行结果如下,有效地将米粒从背景中分离出来。

[En]

The operation results are as follows, and it effectively separates the rice grain from the background.

跟我学Python图像处理丨基于灰度三维图的图像顶帽运算和黑帽运算

二.图像黑帽运算

图像底帽运算(bottom-hat transformation)又称为图像黑帽运算,它是用图像闭运算操作减去原始图像后的结果,从而获取图像内部的小孔或前景色中黑点,也常用于解决由于光照不均匀图像分割出错的问题。其公式定义如下:

跟我学Python图像处理丨基于灰度三维图的图像顶帽运算和黑帽运算

图像底盖操作使用结构元素通过闭合操作从图像中删除对象,这通常用于校正光照不均匀的影响。效果图如下图所示。

[En]

The image bottom cap operation uses a structural element to delete an object from an image through a closed operation, which is often used to correct the influence of uneven illumination. The effect picture is shown in the following figure.

跟我学Python图像处理丨基于灰度三维图的图像顶帽运算和黑帽运算

在Python中,图像底帽运算主要调用morphologyEx()实现,其中参数cv2.MORPH_BLACKHAT表示底帽或黑帽处理,函数原型如下:

dst = cv2.morphologyEx(src, cv2.MORPH_BLACKHAT, kernel)

  • src表示原始图像
  • cv2.MORPH_BLACKHAT表示图像底帽或黑帽运算
  • kernel表示卷积核,可以用numpy.ones()函数构建

Python实现图像底帽运算的代码如下所示:

#encoding:utf-8import cv2  import numpy as np  #读取图片src = cv2.imread('test06.png', cv2.IMREAD_UNCHANGED)#设置卷积核kernel = np.ones((10, 10), np.uint8)#图像黑帽运算result = cv2.morphologyEx(src, cv2.MORPH_BLACKHAT, kernel)#显示图像cv2.imshow("src", src)cv2.imshow("result", result)#等待显示cv2.waitKey(0)cv2.destroyAllWindows()

其运行结果如图所示:

跟我学Python图像处理丨基于灰度三维图的图像顶帽运算和黑帽运算

三.基于灰度三维图的顶帽黑帽运算

跟我学Python图像处理丨基于灰度三维图的图像顶帽运算和黑帽运算

为什么图像顶帽运算会消除光照不均匀的效果呢?通常可以利用灰度三维图来进行解释该算法。灰度三维图主要调用Axes3D包实现,对原图绘制灰度三维图的代码如下:

-*- coding: utf-8 -*-import numpy as npimport cv2 as cvimport matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3Dfrom matplotlib import cmfrom matplotlib.ticker import LinearLocator, FormatStrFormatter#读取图像img = cv.imread("test06.png")img = cv.cvtColor(img,cv.COLOR_BGR2GRAY)imgd = np.array(img) #image类转numpy#准备数据sp = img.shapeh = int(sp[0]) #图像高度(rows)w = int(sp[1]) #图像宽度(colums) of image#绘图初始处理fig = plt.figure(figsize=(16,12))ax = fig.gca(projection="3d")x = np.arange(0, w, 1)y = np.arange(0, h, 1)x, y = np.meshgrid(x,y)z = imgdsurf = ax.plot_surface(x, y, z, cmap=cm.coolwarm) #自定义z轴ax.set_zlim(-10, 255)ax.zaxis.set_major_locator(LinearLocator(10)) #设置z轴网格线的疏密#将z的value字符串转为float并保留2位小数ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) # 设置坐标轴的label和标题ax.set_xlabel('x', size=15)ax.set_ylabel('y', size=15)ax.set_zlabel('z', size=15)ax.set_title("surface plot", weight='bold', size=20)#添加右侧的色卡条fig.colorbar(surf, shrink=0.6, aspect=8) plt.show()

运行结果如下图所示:

跟我学Python图像处理丨基于灰度三维图的图像顶帽运算和黑帽运算

图像中像素的变化趋势表明,图像受到各部分光照不均匀的影响,导致背景灰度不均匀,其中凹陷对应于图像中灰度值相对较小的区域。图像白帽操作后的灰度3D图像编码如下:

[En]

The trend of the pixels in the image shows that the image is affected by the uneven illumination of each part, which results in the uneven grayscale of the background, in which the depression corresponds to the region with relatively small gray value in the image. The code of the grayscale 3D image after the image white hat operation is as follows:

-*- coding: utf-8 -*-import numpy as npimport cv2 as cvimport matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3Dfrom matplotlib import cmfrom matplotlib.ticker import LinearLocator, FormatStrFormatter#读取图像img = cv.imread("test06.png")img = cv.cvtColor(img,cv.COLOR_BGR2GRAY)#图像黑帽运算kernel = np.ones((10,10), np.uint8)result = cv.morphologyEx(img, cv.MORPH_BLACKHAT, kernel)#image类转numpyimgd = np.array(result) #准备数据sp = result.shapeh = int(sp[0]) #图像高度(rows)w = int(sp[1]) #图像宽度(colums) of image#绘图初始处理fig = plt.figure(figsize=(8,6))ax = fig.gca(projection="3d")x = np.arange(0, w, 1)y = np.arange(0, h, 1)x, y = np.meshgrid(x,y)z = imgdsurf = ax.plot_surface(x, y, z, cmap=cm.coolwarm) #自定义z轴ax.set_zlim(-10, 255)ax.zaxis.set_major_locator(LinearLocator(10)) #设置z轴网格线的疏密#将z的value字符串转为float并保留2位小数ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) # 设置坐标轴的label和标题ax.set_xlabel('x', size=15)ax.set_ylabel('y', size=15)ax.set_zlabel('z', size=15)ax.set_title("surface plot", weight='bold', size=20)#添加右侧的色卡条fig.colorbar(surf, shrink=0.6, aspect=8) plt.show()

效果图像如下图所示,对应的灰度更集中在10%到100%的范围内,这证明不均匀的背景被大致消除了,这有利于后续的阈值分割或图像分割。

[En]

The effect image is shown below, and the corresponding grayscale is more concentrated in the range of 10 to 100, which proves that the uneven background is roughly eliminated, which is beneficial to the subsequent threshold segmentation or image segmentation.

跟我学Python图像处理丨基于灰度三维图的图像顶帽运算和黑帽运算

点击关注,第一时间了解华为云新鲜技术~

Original: https://blog.51cto.com/u_15214399/5586000
Author: 华为云开发者联盟
Title: 跟我学Python图像处理丨基于灰度三维图的图像顶帽运算和黑帽运算

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/500687/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球