2
$\begingroup$

enter image description hereI'm using kernel convolution in python to blur an image, but in addition to blurring it also turns the image darker. Could someone explain why it happens?

def convolve(pixel, kernel, i, j):
R = 0
G = 0
B = 0
if i<2 or j<2 or i>=height-2 or j>=width-2:
    R = pixel[i][j][0]
    G = pixel[i][j][1]
    B = pixel[i][j][2]
else:
    # pixel[][][R/G/B]
    R = (
    pixel[i-2][j-2][0] * kernel[0][0] + pixel[i-2][j-1][0] * kernel[0][1] + pixel[i-2][j][0] * kernel[0][2] + pixel[i-2][j+1][0] * kernel[0][3] + pixel[i-2][j+2][0] * kernel[0][4] + 
    pixel[i-1  ][j-2][0] * kernel[1][0] + pixel[i-1  ][j-1  ][0] * kernel[1][1] +pixel[i-1  ][j][0] * kernel[1][2] + pixel[i-1  ][j+1][0] * kernel[1][3] + pixel[i-1  ][j+2][0] * kernel[1][4] +
    pixel[i][j-2][0] * kernel[2][0] + pixel[i][j-1][0] * kernel[2][1] + pixel[i][j][0] * kernel[2][2] + pixel[i][j+1][0] * kernel[2][3] + pixel[i][j+2][0] * kernel[2][4]
    )

    G = (
    pixel[i-2][j-2][1] * kernel[0][0] + pixel[i-2][j-1][1] * kernel[0][1] + pixel[i-2][j][1] * kernel[0][2] + pixel[i-2][j+1][1] * kernel[0][3] + pixel[i-2][j+2][1] * kernel[0][4] + 
    pixel[i-1  ][j-2][1] * kernel[1][0] + pixel[i-1  ][j-1  ][1] * kernel[1][1] +pixel[i-1  ][j][1] * kernel[1][2] + pixel[i-1][j+1][1] * kernel[1][3] + pixel[i-1  ][j+2][1] * kernel[1][4] +
    pixel[i][j-2][1] * kernel[2][0] + pixel[i][j-1][1] * kernel[2][1] + pixel[i][j][1] * kernel[2][2] + pixel[i][j+1][1] * kernel[2][3] + pixel[i][j+2][1] * kernel[2][4]
    )

    B = (
    pixel[i-2][j-2][2] * kernel[0][0] + pixel[i-2][j-1][2] * kernel[0][1] + pixel[i-2][j][2] * kernel[0][2] + pixel[i-2][j+1][2] * kernel[0][3] + pixel[i-2][j+2][2] * kernel[0][4] + 
    pixel[i-1  ][j-2][2] * kernel[1][0] + pixel[i-1  ][j-1][2] * kernel[1][1] +pixel[i-1][j][2] * kernel[1][2] + pixel[i-1][j+1][2] * kernel[1][3] + pixel[i-1  ][j+2][2] * kernel[1][4] +
    pixel[i][j-2][2] * kernel[2][0] + pixel[i][j-1][2] * kernel[2][1] + pixel[i][j][2] * kernel[2][2] + pixel[i][j+1][2] * kernel[2][3] + pixel[i][j+2][2] * kernel[2][4]
    )

# if <0, then 0
R = R*(R>0)
G = G*(G>0)
B = B*(B>0)

R = R if R<256 else 255
G = G if G<256 else 255
B = B if B<256 else 255


# if >255, then 255
if R<0 or R>255 or G<0 or G>255 or B<0 or B>255:
    print(R, G, B)
return (round(R),round(G),round(B))

The kernel I'm using is a 5x5 gaussian blur from wikipedia

$\endgroup$
0

2 Answers 2

2
$\begingroup$

Your image appears darker because standard RGB is used in transformation, with problem inherent to color - grayscale conversion as standard RGB transformation does not preserve luminance.

Simple modification is to switch RGB channels to color space using perceived luminance: $Y = 0.299*R + 0.587*G + 0.114*B$. That way, luminance and chrominance blurring will not result in darkening or artificial color bleeding.

Color spaces like Y'UV or RGB are non-linear, so make sure to use linear one, say sRGB or any perception-aware/ linear space.

If you can trade speed with accuracy, floating-point box filters are visually better. Given 5x5 blur is tempting, but does not approximate Gaussian Blur accurately enough.

Something is wrong with your code.
Images appears darker by bad color space, but not that much. I took python code found here as it is, and took your image, the result is darker in my opinion, but not visible to the point of shadow dark. Gaussian blur 5x5 I haven't seen your picture while writing answer but, it seems to me that divisor used is bigger than the sum of cell values.

$\endgroup$
0
-1
$\begingroup$

You must Normalise the values. If the values divide by (sum of them), that will not be happend.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.