Can someone help me fix the error in this Python script?
I wrote a Python script that should work like a camera and then add a depth of field effect to the photo that is saved.
This is my code:
import cv2 from PIL import Image as Img, ImageTk from tkinter import * import numpy as np from monodepth2 import monodepth2 cap = cv2.VideoCapture(0) md = monodepth2() win = Tk() win.bind('<Escape>', lambda e: win.quit()) label_widget = Label(win) label_widget.pack() def capture(): ret, frame = cap.read() depth = md.eval(frame) depth_gray = cv2.cvtColor(depth, cv2.COLOR_BGR2GRAY) depth_map = cv2.normalize(depth_gray, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F) # Erstelle ein leeres Bild für das Ergebnis bokeh_image = np.zeros_like(frame) # Wende einen Gaußschen Weichzeichner auf das gesamte Bild an blurred = cv2.GaussianBlur(frame, (0, 0), 10) # Mische das Originalbild und das verschwommene Bild basierend auf der Tiefenkarte bokeh_image = cv2.addWeighted(frame, 1.0 - depth_map, blurred, depth_map, 0) # Speichere das Ergebnis cv2.imwrite('bokeh.jpg', bokeh_image) cv2.imwrite("depthmap.jpg", depth_gray) cv2.imwrite("photo.jpg", frame) capture_button = Button(win, text="capture", command=capture) capture_button.pack() def update_image(): ret, frame = cap.read() opencv_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA) captured_image = Img.fromarray(opencv_image) photo_image = ImageTk.PhotoImage(image=captured_image) label_widget.photo_image = photo_image label_widget.configure(image=photo_image) win.after(10, update_image) # update every 10 ms update_image() win.mainloop()
But now, for some unknown reason, I get this error:
cv2.error: OpenCV(4.8.1) :-1: error: (-5:Bad argument) in function 'addWeighted' > Overload resolution failed: > - Argument 'alpha' can not be treated as a double > - Argument 'alpha' can not be treated as a double
Does anyone know what the problem is? Thanks for your help.
When I see it right in the Docs, normalize() is a void function. So without return value. And then 1.0 – zero will have problems. Try using a fixed value instead of alpha.
what a fixed value?
Any valid double value between 0 and 1.0