【问题标题】:How to get correct Laplacian sharpened .raw image?如何获得正确的拉普拉斯锐化 .raw 图像?
【发布时间】:2021-09-09 11:14:13
【问题描述】:

我正在尝试使用此算法对月球图像进行 拉普拉斯锐化

我正在转换这张图片:

但我不知道为什么我会得到这样的图像:

这是我的代码:

import numpy as np

def readRawFile(name,row_size,column_size):
    imgFile = open(name,'rb')
    img = np.fromfile(imgFile, dtype = np.uint8, count = row_size * column_size)
    img = np.reshape(img,(-1,row_size))
    imgFile.close()
    return img

img = readRawFile("ass-3/moon464x528.raw", 464, 528)

width = img.shape[0]
height = img.shape[1]

img_pad = np.pad(img, ((1, 1), (1, 1)), 'edge')

w = np.array([1,1.2,1])

t1 = np.array([[0,-1,0],[-1,4,-1],[0,-1,0]])

edge_img = np.zeros((width, height))
edge_pad = np.pad(edge_img, ((1, 1), (1, 1)), 'constant')

for i in range(1,width-1):
    for j in range(1,height-1):
                
        edge_pad[i, j]=abs(np.sum((img_pad[i:i + 3, j:j + 3] * t1)*w))
        
        if edge_pad[i, j] < 0:
            edge_pad[i, j] = 0

out_img = img-edge_pad[1:edge_pad.shape[0]-1,1:edge_pad.shape[1]-1]
out_img.astype('int8').tofile("ass-3/moon-1.raw")

谁能帮帮我?

【问题讨论】:

    标签: python numpy image-processing rawimage laplacian


    【解决方案1】:

    我能够识别的问题很少:

    • 允许边缘图像同时具有正值和负值。
      删除abs,并删除if edge_pad[i, j] &lt; 0...
    • “窗口”img_pad[i:i + 3, j:j + 3] 未以[i, j] 为中心,将其替换为:
      img_pad[i-1:i+2, j-1:j+2]
      (寻找二维离散卷积实现)。
    • 我认为公式中的w 应该是一个负标量。
      w = np.array([1, 1.2, 1]) 替换为w = -1.2
    • t1edge_pad的类型是np.float64img的类型是np.uint8
      img - edge_pad[1:edge_pad.shape[0] - 1, 1:edge_pad.shape[1] - 1] 的类型是 np.float64
      我们需要将值裁剪到 [0, 255] 范围内并转换为 np.uint8:
      out_img = np.clip(out_img, 0, 255).astype(np.uint8)

    我看不到有关 .raw 格式的任何问题。
    我将输入和输出替换为 PNG 图像格式,并使用 OpenCV 读取和写入图像。
    OpenCV 的使用只是示例,您不需要使用 OpenCV。


    这是一个“完整”的代码示例:

    import numpy as np
    import cv2
    
    #def readRawFile(name, row_size, column_size):
    #    imgFile = open(name, 'rb')
    #    img = np.fromfile(imgFile, dtype=np.uint8, count=row_size * column_size)
    #    img = np.reshape(img, (-1, row_size))
    #    imgFile.close()
    #    return img
    
    #img = readRawFile("ass-3/moon464x528.raw", 464, 528)
    img = cv2.imread('moon.png', cv2.IMREAD_GRAYSCALE)  # Read input image as grayscale.
    
    width = img.shape[0]  # The first index is the height (the names are swapped)
    height = img.shape[1]
    
    img_pad = np.pad(img, ((1, 1), (1, 1)), 'edge')
    
    #w = np.array([1, 1.2, 1])
    w = -1.2  # I think w in the formula is supposed to be a negative a scalar
    
    t1 = np.array([[0, -1, 0], [-1, 4, -1], [0, -1, 0]])
    
    edge_img = np.zeros((width, height))
    edge_pad = np.pad(edge_img, ((1, 1), (1, 1)), 'constant')
    
    for i in range(1, width - 1):
        for j in range(1, height - 1):
    
            #edge_pad[i, j] = abs(np.sum((img_pad[i:i + 3, j:j + 3] * t1) * w))
            # Edge is allowed to be negative.
            edge_pad[i, j] = np.sum(img_pad[i-1:i+2, j-1:j+2] * t1) * w
    
            #if edge_pad[i, j] < 0:
            #    edge_pad[i, j] = 0
    
    # img tyep is uint8 and edge_pad is float64, the result is float64
    out_img = img - edge_pad[1:edge_pad.shape[0] - 1, 1:edge_pad.shape[1] - 1]
    out_img = np.clip(out_img, 0, 255).astype(np.uint8)  # Clip range to [0, 255] and cast to uint8
    
    #out_img.astype('int8').tofile("ass-3/moon-1.raw")
    
    cv2.imwrite('out_img.png', out_img)  # Save out_img as PNG image file
    
    # Show the input and the output images for testing
    cv2.imshow('img', img)
    cv2.imshow('edge_pad', (edge_pad-edge_pad.min())/(edge_pad.max() - edge_pad.min()))
    cv2.imshow('out_img', out_img)
    cv2.waitKey()
    cv2.destroyAllWindows()
    

    结果:

    img:

    out_img:

    edge_pad(线性对比拉伸后):

    【讨论】:

      猜你喜欢
      • 2017-01-29
      • 2016-08-09
      • 2012-12-03
      • 1970-01-01
      • 1970-01-01
      • 2020-05-10
      • 1970-01-01
      • 2021-04-01
      相关资源
      最近更新 更多