I am trying to do Laplacian sharpening on the moon image with using this algorithm :
I am converting this image:
But I don't know why I am getting image like this:
Here is my code:
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")
Can anyone help me please?
There are few issues I was able to identify:
Remove the
abs
, and remove theif edge_pad[i, j] < 0
...img_pad[i:i + 3, j:j + 3]
is not centered around[i, j]
, replace it with:img_pad[i-1:i+2, j-1:j+2]
.(Look for 2D discrete convolution implementations).
w
in the formula is supposed to be a negative scalar.Replace
w = np.array([1, 1.2, 1])
withw = -1.2
.t1
andedge_pad
isnp.float64
, and the type ofimg
isnp.uint8
.The type of
img - edge_pad[1:edge_pad.shape[0] - 1, 1:edge_pad.shape[1] - 1]
isnp.float64
.We need to clip the values to range [0, 255] and cast to
np.uint8
:out_img = np.clip(out_img, 0, 255).astype(np.uint8)
.I can't see any issues regarding
.raw
format.I replaced the input and output with PNG image format, and used OpenCV for reading and writing the images.
The usage of OpenCV is just for the example - you don't need to use OpenCV.
Here is a "complete" code sample:
Results:
img
:out_img
:edge_pad
(after linear contrast stretching):