I am trying to make a program in python where there is a picture and on top, there are two sliders. One slider effects how much blur there is in the photo and the other adds back in fine detail. The image should update as a slider is moved. We learned that to do an unsharp mask the equation is I+c(I-B) where I is the image, c is a variable of change and B is the blurred image. I am having issues with this because I have two functions one for each slider since you need a function as a parameter for the slider, but I need to reference the blurred image to be able to make the fine detail add back in.
This is what I have currently:
import cv2
import numpy as np
# Load the image from file
img = cv2.imread("cameraman.tif", cv2.IMREAD_GRAYSCALE)
# Code to be run when the slider is moved
# Performs a square box blur of size x+1
def update_blur(x):
global display_image
display_image = cv2.GaussianBlur(img, (2 * x + 1, 2 * x + 1), 0, 0)
cv2.imshow("Blur and Sharpen", display_image)
def update_sharpen(x):
display_image2 = img+x*(display_image)
cv2.imshow("Blur and Sharpen", display_image2)
# Create a window
cv2.namedWindow("Blur and Sharpen")
# Add in trackbar to control blur size
cv2.createTrackbar("Blur Size", "Blur and Sharpen", 0, 10, update_blur)
cv2.createTrackbar("Sharpen", "Blur and Sharpen", 0, 10, update_sharpen)
# Display the image initially
cv2.imshow("Blur and Sharpen", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
The error I get is:
Traceback (most recent call last):
File "SharpenGUI.py", line 16, in update_sharpen
display_image2 = img+x*(display_image)
NameError: name 'display_image' is not defined
I have tried putting the global in and outside of the function. I have not used Python much before image processing, so I think it has something to do with the "global display_image" line, but not certian. I have tried condensing the two functions; however, then I only have one trackbar/slider when I need two. I hope this has enough information. Thanks in advance.