Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
3.6 kB
1
Indexable
Never
import cv2
import numpy as np

# calculate the mse
def getMSE(inputImage, imageWithNoise):
    mse = np.sum((inputImage.astype("float") - imageWithNoise.astype("float")) ** 2)
    mse /= float(inputImage.shape[0] * inputImage.shape[1])
    return mse

def applyFilters(type, inputImage, imageWithNoise):
    # create reference var
    org = (0, 330)
    fontScale = 0.6
    thickness = 2
    color_Original = (255, 0, 0)
    color_Noise = (5, 33, 251)
    color_Blur = (90, 255, 87)
    font = cv2.FONT_HERSHEY_SIMPLEX

    imageWithFilter1 = ''
    imageWithFilter2 = ''

    # create blurred copies depends on blur type
    if type == 'medianBlur':
        imageWithFilter1 = cv2.medianBlur(imageWithNoise, 3)
        mse = getMSE(inputImage, imageWithFilter1)
        imageWithFilter1 = cv2.putText(
            imageWithFilter1,
            'Blur 3x3, mse: ' + str(round(mse, 2)),
            org,
            font,
            fontScale,
            color_Blur,
            int(thickness),
            cv2.LINE_AA
        )

        imageWithFilter2 = cv2.medianBlur(imageWithNoise, 5)
        mse = getMSE(inputImage, imageWithFilter2)
        imageWithFilter2 = cv2.putText(
            imageWithFilter2,
            'Blur 5x5, mse: ' + str(round(mse, 2)),
            org,
            font,
            fontScale,
            color_Blur,
            int(thickness),
            cv2.LINE_AA
        )

    elif type == 'gaussianBlur':
        imageWithFilter1 = cv2.GaussianBlur(imageWithNoise, (3, 3), 0)
        mse = getMSE(inputImage, imageWithFilter1)
        imageWithFilter1 = cv2.putText(
            imageWithFilter1,
            'Blur 3x3, mse: ' + str(round(mse, 2)),
            org,
            font,
            fontScale,
            color_Blur,
            int(thickness),
            cv2.LINE_AA
        )

        imageWithFilter2 = cv2.GaussianBlur(imageWithNoise, (5, 5), 0)
        mse = getMSE(inputImage, imageWithFilter2)
        imageWithFilter2 = cv2.putText(
            imageWithFilter2,
            'Blur 5x5, mse: ' + str(round(mse, 2)),
            org,
            font,
            fontScale,
            color_Blur,
            int(thickness),
            cv2.LINE_AA
        )

    # describe original input img and img with noise
    inputImage = cv2.putText(
        inputImage,
        'Original ',
        org,
        font,
        fontScale,
        color_Original,
        int(thickness),
        cv2.LINE_AA
    )

    imageWithNoise = cv2.putText(
        imageWithNoise,
        'With Noise',
        org,
        font,
        fontScale,
        color_Noise,
        int(thickness),
        cv2.LINE_AA
    )

    # concatenate all images to be able to compare results with source images
    bunchOfImages = np.concatenate((inputImage, imageWithNoise, imageWithFilter1, imageWithFilter2), 1, None)
    return bunchOfImages


cv2.imshow(
    "a.medianBlur", applyFilters(
        'medianBlur', cv2.imread('images\\a.bmp'), cv2.imread('images\\a_sz.bmp')
    )
)
cv2.imshow(
    "b.medianBlur", applyFilters(
        'medianBlur', cv2.imread('images\\b.bmp'), cv2.imread('images\\b_sz.bmp')
    )
)

cv2.imshow(
    "a.gaussianBlur", applyFilters(
        'gaussianBlur', cv2.imread('images\\a.bmp'), cv2.imread('images\\a_sz.bmp')
    )
)
cv2.imshow(
    "b.gaussianBlur", applyFilters(
        'gaussianBlur', cv2.imread('images\\b.bmp'), cv2.imread('images\\b_sz.bmp')
    )
)

cv2.waitKey(0)