Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the blur region mask? #15

Closed
guangpintao opened this issue Mar 12, 2021 · 3 comments
Closed

How to get the blur region mask? #15

guangpintao opened this issue Mar 12, 2021 · 3 comments

Comments

@guangpintao
Copy link

Hello, i wonder how to get the demo result as shown below using the brightness to show the blur degree:
image
It will be helpful if you can give me some instrs.

@WillBrennan
Copy link
Owner

You can see in the process code how it displays the pretty blur map; you can display it from process or save it off.

https://github.com/WillBrennan/BlurDetection2/blob/master/process.py#L84

@guangpintao
Copy link
Author

guangpintao commented Mar 15, 2021 via email

@kimlia545
Copy link

I saved the blur map, but it is a black image.

import cv2
import numpy


def fix_image_size(image: numpy.array, expected_pixels: float = 2E6):
    ratio = numpy.sqrt(expected_pixels / (image.shape[0] * image.shape[1]))
    return cv2.resize(image, (0, 0), fx=ratio, fy=ratio)


def estimate_blur(image: numpy.array, threshold: int = 100):
    if image.ndim == 3:
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    blur_map = cv2.Laplacian(image, cv2.CV_64F)
    score = numpy.var(blur_map)
    return blur_map, score, bool(score < threshold)


def pretty_blur_map(blur_map: numpy.array, sigma: int = 5, min_abs: float = 0.5):
    abs_image = numpy.abs(blur_map).astype(numpy.float32)
    abs_image[abs_image < min_abs] = min_abs

    abs_image = numpy.log(abs_image)
    cv2.blur(abs_image, (sigma, sigma))
    return cv2.medianBlur(abs_image, sigma)

image = cv2.imread('data/test.png', 0)
image = fix_image_size(image)
blur_map, score, blurry = estimate_blur(image)
cv2.imwrite('result.png', pretty_blur_map(blur_map))

Input
test

Output
result

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants