Maybe my question is strange something but I need to make an adaptive Threshold on part of the image that the user selects with his mouse and that's my code
import cv2
img = cv2.imread("test.png")
# img2 = cv2.imread("flower.jpg")
# variables
ix = -1
iy = -1
drawing = False
def draw_reactangle_with_drag(event, x, y, flags, param):
global ix, iy, drawing, img
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
ix = x
iy = y
elif event == cv2.EVENT_MOUSEMOVE:
if drawing == True:
img2 = cv2.imread("test.png")
cv2.rectangle(img2, pt1=(ix, iy), pt2=(x, y),
color=(0, 255, 255), thickness=1)
img = img2
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
img2 = cv2.imread("test.png")
cv2.rectangle(img2, pt1=(ix, iy), pt2=(x, y),
color=(0, 255, 255), thickness=1)
img = img2
gray = cv2.cvtColor(img2[y: iy, x: ix], cv2.COLOR_BGR2GRAY)
th = cv2.adaptiveThreshold(gray,
255, # maximum value assigned to pixel values exceeding the threshold
cv2.ADAPTIVE_THRESH_GAUSSIAN_C, # gaussian weighted sum of neighborhood
cv2.THRESH_BINARY, # thresholding type
5, # block size (5x5 window)
3) # constant
img = th
cv2.namedWindow(winname="Title of Popup Window")
cv2.setMouseCallback("Title of Popup Window", draw_reactangle_with_drag)
while True:
cv2.imshow("Title of Popup Window", img)
if cv2.waitKey(10) == 27:
break
cv2.destroyAllWindows()







np.where(mask==255, adaptive_threshold_image, input_image)