Python Forum
Image convoluion - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Image convoluion (/thread-22918.html)



Image convoluion - HuntahSVK - Dec-03-2019

Hi, im a student of software engineering and i've been tasked to apply convolutions to images.
I have to use the basic convolution kernels that are on wikipedia, im mainly focusing on sharpen which is giving me strange artifacts but i do now know why since i think my code is good. Do you have any tips?

[Image: fFs5Hp5]


RE: Image convoluion - Webchefz - Dec-03-2019

Convolution is simply an element-wise multiplication of two matrices followed by a sum.

- Take two matrices (which both have the same dimensions).
- Multiply them, element-by-element (i.e., not the dot-product, just a simple multiplication).
- Sum the elements together

Enjoy
Webchefz Infotech


RE: Image convoluion - HuntahSVK - Dec-03-2019

Yeah, im doing that as you can see in the Image posted bellow and im trying to clip the values to 0, 255 so it wont mess things up, but they still od.


RE: Image convoluion - jefsummers - Dec-03-2019

Maybe I'm missing something. Once you have the outputImage array, why not use np.max() to find the maximum value, then scale all pixels by multiplying by 255/max?


RE: Image convoluion - HuntahSVK - Dec-04-2019

That sounds like a good idea, will try that, Thanks!

But yeah, that did not work out... Still some strange errors.
This is my code, as you can see i have some other functions but they work just fine, only sharpen is giving me some errors.
def sharpen(image):
    kernel =  np.array([
    [0, -1, 0],
    [-1, 5, -1],
    [0, -1, 0],
])

    padding = kernel.shape[0] // 2

    outputImage = np.zeros_like(image)
    
    if (image.ndim == 2):
        paddedImage = np.zeros((image.shape[0] + (padding * 2), image.shape[1] + (padding * 2)))
        paddedImage[padding:-padding, padding:-padding] = image

        for i in range(image.shape[0]):
            for j in range(image.shape[1]):
                outputImage[i,j]=(kernel*paddedImage[i:i+kernel.shape[0], j:j+kernel.shape[0]]).sum()

    elif(image.ndim == 3):
        paddedImageR = np.zeros((image.shape[0] + (padding * 2), image.shape[1] + (padding * 2)))
        paddedImageG = np.zeros((image.shape[0] + (padding * 2), image.shape[1] + (padding * 2)))
        paddedImageB = np.zeros((image.shape[0] + (padding * 2), image.shape[1] + (padding * 2)))

        paddedImageR[padding:-padding, padding:-padding] = image[:,:,0]
        paddedImageG[padding:-padding, padding:-padding] = image[:,:,1]
        paddedImageB[padding:-padding, padding:-padding] = image[:,:,2]

        for i in range(image.shape[0]):
            for j in range(image.shape[1]):
                outputImage[i,j,0]=(kernel*paddedImageR[i:i+kernel.shape[0], j:j+kernel.shape[0]]).sum()   
                outputImage[i,j,1]=(kernel*paddedImageG[i:i+kernel.shape[0], j:j+kernel.shape[0]]).sum()
                outputImage[i,j,2]=(kernel*paddedImageB[i:i+kernel.shape[0], j:j+kernel.shape[0]]).sum()

    return outputImage

args = sys.argv[1:]
args = ["--sharpen", "LennaBnW.png", "LennaSharpen.png"]
inputFilePath = args[-2]
outputFilePath = args[-1]

image1 = Image.open(inputFilePath)
imgArray = np.asarray(image1, dtype = np.uint8);

for arg in args:
    if (arg == "--rotate"):
        imgArray = rotate(imgArray)
    if (arg == "--mirror"):
        imgArray = mirror(imgArray)
    if (arg == "--inverse"):
        imgArray = inverse(imgArray)
    if (arg == "--bw"):
        imgArray = bw(imgArray)
    if (arg == "--lighten"):
        index = args.index("--lighten")
        imgArray = lighten(imgArray, int(args[index + 1]))
    if (arg == "--darken"):
        index = args.index("--darken")
        imgArray = darken(imgArray, int(args[index + 1]))
    if (arg == "--sharpen"):
        imgArray = sharpen(imgArray)

imgArray = (255/np.max(imgArray)) * imgArray
np.clip(imgArray, 0, 255)

if (imgArray.ndim == 3):
    image = Image.fromarray(imgArray.astype(np.uint8), 'RGB')
elif (imgArray.ndim == 2):
    image = Image.fromarray(imgArray.astype(np.uint8), 'L')

image.save(outputFilePath)
So if you can find error in that i would be really glad.


RE: Image convoluion - HuntahSVK - Dec-04-2019

I found the solution, which increased the time from 7 seconds to 30, but the results are correct.
All that needs to be done is to add clipping, but not after the output image is calculated, the clipping needs to be added to the lines 18, 31, 32 and 33 after the sum() function.
I repeat, it increases the computational time like hell, but it works.
Thanks to everybody for trying to help me! Heart