Search code examples
pythonmatplotlibphotoshop-script

Kernel Photo Processing : Image Can't Appear


I currently working on photo this one, it use 3D matrices using python processing filtering (kernel). Currently my photo can't appear on the plot, it only appeared some cyan dots on the plot. What should I do to the script so my photo can appear on the plot. Thank you for answering.

import numpy as np
import matplotlib.pyplot as plt

im=plt.imread('NSX.jpg')
(dim_x,dim_y,dim_z)=im.shape
im1=np.pad(array=im, pad_width=[1, 1], mode='constant', constant_values=0)
im2=np.empty((652,1026,3))
ker1 = np.array([[1/9, 1/9, 1/9],
                [1/9, 1/9, 1/9],
                [1/9, 1/9, 1/9]])

def filtergambar():
    for m in range(0,dim_z):
        for i in range(1,dim_x-1):
            for j in range(1, dim_y-1):
                im_entry=im[i-1:i+2, j-1:j+2]
                div=np.sum(im_entry*ker1)
                im2[i,j,m]=div
    return im2
filtergambar()
plt.imshow(im2.astyoe('uint8'))
plt.gray
plt.show

Solution

  • There are already some existing modules which can help you work with images, like ndimage and scikit-image to name but a few.

    Here's your example using ndimage:

    import numpy as np
    import matplotlib.pyplot as plt
    import scipy.ndimage as nd
    
    im=plt.imread('NSX.jpg')
    
    ker = 1/9 * np.ones((3, 3, 1))
    im2 = nd.convolve(im, ker, mode='constant')
    plt.imshow(im2.astype('uint8'))