使用同态滤波+拉普拉斯算法处理图像(APMCM为例)

以APMCM2019年的A题,该题目要求我们从这样的图片中提取出二氧化硅晶体。
块状固体是提取目标
提取方法

同态滤波
该方法在博客中有详细说明。主要思想是:

  • 图像 f ( x , y ) f(x,y) 可由 f i ( x , y ) f r ( x , y ) f_i(x,y)f_r(x,y) 组成,然后两边取对数并做傅里叶变换后就有

  • D F T [ l n f ( x , y ) ] = D F T [ l n f i ( x , y ) ] + D F T [ l n f r ( x , y ) ] DFT[lnf(x,y)]=DFT[lnf_i(x,y)]+DFT[lnf_r(x,y)]

  • 之后使用高通滤波器对图像滤波,其形式为

  • H p ( μ , v ) = 1 e x p [ c ( D 2 ( μ , v ) / D 0 2 ) ] H_p(\mu,v)=1-exp[-c(D^2(\mu,v)/D^2_0)]
    具体代码

import cv2
from matplotlib import pyplot as plt import numpy as np
def homomorphic_filter(src, d0=10, r1=0.5, rh=2, c=4, h=2.0, l=0.5): gray = src.copy()
if len(src.shape) > 2:
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) gray = np.float64(gray)
rows, cols = gray.shape2))

gray_fft = np.fft.fft2(gray) gray_fftshift = np.fft.fftshift(gray_fft)
dst_fftshift = np.zeros_like(gray_fftshift)
M, N = np.meshgrid(np.arange(-cols // 2, cols // 2), np.arange(-rows // 2, rows   //

D = np.sqrt(M ** 2 + N ** 2)
Z = (rh - r1) * (1 - np.exp(-c * (D ** 2 / d0 ** 2))) + r1 dst_fftshift = Z * gray_fftshift
dst_fftshift = (h - l) * dst_fftshift + l dst_ifftshift = np.fft.ifftshift(dst_fftshift) dst_ifft = np.fft.ifft2(dst_ifftshift)
dst = np.real(dst_ifft)
dst = np.uint8(np.clip(dst, 0, 255)) return dst
for i in range(497, 611, 1): j = i
i = '.\\data\\0' + str(i) + '.bmp' img = cv2.imread(i, 0)
img2 = homomorphic_filter(img) z = j
i = '.\\results\\0' + str(j) + '.bmp' cv2.imwrite(i, img2)
i = z

效果为在这里插入图片描述
拉普拉斯锐化

具体内容在博客有,主要思想是利用拉普拉斯算子,即
在这里插入图片描述
锐化图像边缘,代码为

import cv2 as cv

for i in range(497, 611, 1): j = i
i = '.\\data\\0' + str(i) + '.bmp'
img = cv.imread(i, cv.COLOR_BGR2GRAY) img2 = cv.Laplacian(img, ddepth=-1, ksize=3) imgOut = img - img2
z = j
i = '.\\results\\0' + str(j) + '.bmp' cv.imwrite(i, imgOut)
i = z

然后使用cv包检测边缘,代码为

import cv2 as cv


for i in range(497, 611, 1): m = i
i = '.\\data\\0' + str(i) + '.png' img = cv.imread(i)

col = img.shape[0] row = img.shape[1] img2 = img.copy()
img2 = cv.cvtColor(img2, cv.COLOR_BGR2GRAY) for j in range(col):
for k in range(row):
if img.item(j, k, 2) > 150 and img.item(j, k, 0) < 100 and img.item(j, k,1) < 100:k, 1) < 130:
z = m
img2.itemset((j, k), 255)
elif img.item(j, k, 2) > 180 and img.item(j, k, 0) < 130 and   img.item(j,
img2.itemset((j, k), 255)
else:
img2.itemset((j, k), 0)

i = '.\\results\\0' + str(m) + '.bmp' cv.imwrite(i, img2)
i = z

结果为
在这里插入图片描述
效果添加了一些人工辅助,然后我们就可以进行后续的计算了。

发布了5 篇原创文章 · 获赞 19 · 访问量 801

猜你喜欢

转载自blog.csdn.net/weixin_43518179/article/details/104734990