This question already has answers here:
Segmenting License Plate Characters

(2个答案)


25天前关闭。




因此,我需要构建一个同态过滤器,但是我的代码似乎是错误的。我不知道它是执行的还是我不了解的一些细节,但是我确实知道这是错误的。我很乐意就我可以做些什么来改善它。
我正在使用
python - 频域上的同态滤波(Python和OpenCV)-LMLPHP
图像作为输入参考,因为它在DIP的Ricardo C. Gonzales书中,我知道输出的外观。我什至在使用与过滤器中使用的书相同的参数,但无法正常工作。
冈萨雷斯的输入和输出分别为:
python - 频域上的同态滤波(Python和OpenCV)-LMLPHP
我的输出:
python - 频域上的同态滤波(Python和OpenCV)-LMLPHP
我的代码如下:
# coding: utf-8

import cv2
import numpy as np
from matplotlib import pyplot as plt

tss = cv2.imread("The_Seventh_Seal_1.jpg", 0)
mc = cv2.imread("mussels_cave_050.JPG", 0)
sh = cv2.imread("shelter_homomorphic.bmp", 0)
pet = cv2.imread("pet.png", 0)


def filtro_gaussiano_livro(img, gl, gh, inc, Dz):
    im = np.copy(img)
    P = im.shape[0] / 2
    Q = im.shape[1] / 2
    h = np.zeros(im.shape)
    U, V = np.meshgrid(range(im.shape[0]), range(im.shape[1]), sparse=False, indexing='ij')
    d = ((U - P) ** 2 + (V - Q) ** 2).astype(float)
    d0 = Dz
    c = inc
    h = (gh - gl) * (1 - (np.exp(-c * (d / (d0 ** 2))))) + gl
    return h


def filtro_gaussiano(img, Dz):
    im = np.copy(img)
    P = im.shape[0] / 2
    Q = im.shape[1] / 2
    h = np.zeros(im.shape)
    U, V = np.meshgrid(range(im.shape[0]), range(im.shape[1]), sparse=False, indexing='ij')
    d = (((U - P) ** 2) + ((V - Q) ** 2)).astype(float)
    h = 1 - np.exp(-(d / (2 * (Dz ** 2))))
    return h


def uint8_conv(img):
    mat = np.copy(img)
    for i in range(mat.shape[0]):
        for j in range(mat.shape[1]):
            if mat[i, j] < 0:
                mat[i, j] = 0
            elif mat[i, j] > 255:
                mat[i, j] = 255
            else:
                mat[i, j] = mat[i, j]
    return np.uint8(mat)


def reescalona(img, min, max):
    mat = np.copy(img)
    ph = cv2.add(min, (
        cv2.divide((cv2.multiply((cv2.subtract(mat, np.min(mat))), (max - min))), (np.max(mat) - np.min(mat)))))
    rtn = np.uint8(ph)
    return rtn


def homomorfica(img, l, s):
    im = np.float64(np.copy(img))
    cv2.imshow("BORDER", im)
    if s == 0:
        f = filtro_gaussiano(im, l)
    elif s == 1:
        f = filtro_gaussiano_livro(im, 0.05, 3.5, 1, l)
    cv2.imshow("gauss " + str(s), f)
    im_log = np.log1p(im)
    Im_shift = np.fft.fftshift(np.fft.fft2(im_log))
    Im_fft_filt = np.multiply(f, Im_shift)
    cv2.imshow("FFT Shift", uint8_conv(np.real(Im_shift)))
    Im_filt = np.real(np.fft.ifft2(np.fft.ifftshift(Im_fft_filt)))
    Im = np.exp(Im_filt) - 1
    Im = reescalona(Im, 0, 255)
    return uint8_conv(Im)


# def notch(img):



raio = 2500
i = pet
a = homomorfica(i, raio, 0)
b = homomorfica(i, raio, 1)
cv2.imshow("Imagem original", i)
cv2.imshow("Filtro homofobico comum", a)
cv2.imshow("Filtro homofobico do livro", b)

k = 0
while k != 27:
    k = cv2.waitKey(0)
cv2.destroyAllWindows()

最佳答案

这是使用Python / Numpy / OpenCV在频域中进行同态滤波的一种方法。
我相信您的问题只是您的过滤。我将在下面显示两个不同的滤镜,它们的圆半径和高斯滤镜各不相同。

  • 将输入读取为灰度
  • 取输入
  • 的自然对数
  • 对实部/虚部进行FFT
  • 移位FFT以使DC点位于中心
  • 在小半径
  • 的白色背景上创建黑色圆形蒙版
  • 将高斯模糊应用于蒙版
  • 移位FFT,使DC点位于左上角
  • 执行IFFT并将其转换为简单的真实图像
  • 取IFFT的指数
  • 将其拉伸(stretch)到0到25​​5之间的范围
  • 保存结果

  • python - 频域上的同态滤波(Python和OpenCV)-LMLPHP
    import numpy as np
    import cv2
    
    # read input and convert to grayscale
    img = cv2.imread('person.png', cv2.IMREAD_GRAYSCALE)
    hh, ww = img.shape[:2]
    
    # take ln of image
    img_log = np.log(np.float64(img), dtype=np.float64)
    
    # do dft saving as complex output
    dft = np.fft.fft2(img_log, axes=(0,1))
    
    # apply shift of origin to center of image
    dft_shift = np.fft.fftshift(dft)
    
    # create black circle on white background for high pass filter
    #radius = 3
    radius = 13
    mask = np.zeros_like(img, dtype=np.float64)
    cy = mask.shape[0] // 2
    cx = mask.shape[1] // 2
    cv2.circle(mask, (cx,cy), radius, 1, -1)
    mask = 1 - mask
    
    # antialias mask via blurring
    #mask = cv2.GaussianBlur(mask, (7,7), 0)
    mask = cv2.GaussianBlur(mask, (47,47), 0)
    
    # apply mask to dft_shift
    dft_shift_filtered = np.multiply(dft_shift,mask)
    
    # shift origin from center to upper left corner
    back_ishift = np.fft.ifftshift(dft_shift_filtered)
    
    # do idft saving as complex
    img_back = np.fft.ifft2(back_ishift, axes=(0,1))
    
    # combine complex real and imaginary components to form (the magnitude for) the original image again
    img_back = np.abs(img_back)
    
    # apply exp to reverse the earlier log
    img_homomorphic = np.exp(img_back, dtype=np.float64)
    
    # scale result
    img_homomorphic = cv2.normalize(img_homomorphic, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)
    
    # write result to disk
    cv2.imwrite("person_dft_numpy_mask.png", (255*mask).astype(np.uint8))
    cv2.imwrite("person_dft_numpy_homomorphic.png", img_homomorphic)
    
    cv2.imshow("ORIGINAL", img)
    cv2.imshow("MASK", mask)
    cv2.imshow("FILTERED DFT/IFT ROUND TRIP", img_back)
    cv2.imshow("HOMOMORPHIC", img_homomorphic)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    半径= 3和模糊= 7的高通滤波器蒙版和同态结果:
    python - 频域上的同态滤波(Python和OpenCV)-LMLPHP
    python - 频域上的同态滤波(Python和OpenCV)-LMLPHP
    半径= 13且模糊= 47的高通滤波器蒙版和同态结果:
    python - 频域上的同态滤波(Python和OpenCV)-LMLPHP
    python - 频域上的同态滤波(Python和OpenCV)-LMLPHP

    关于python - 频域上的同态滤波(Python和OpenCV),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/64284739/

    10-09 05:31