Opencv imshow() freezes when updating

后端 未结 9 2331
Happy的楠姐
Happy的楠姐 2020-12-09 05:18

For my image processing algorithm I\'m using python / OpenCV. The output of my algorithm shall be updated im the same window over and over again.

However sometimes

相关标签:
9条回答
  • 2020-12-09 05:56

    Add the following two lines of code after cv2.imshow() function,

    cv2.waitKey()

    cv2.destroyAllWindows()

    0 讨论(0)
  • 2020-12-09 05:58

    You can use while loop to take burst images without freezing. Here is an example for taking 10 images. You can also try to increase waitkey number and sleep time in while loop. This work for me.

    key = cv2.waitKey(1)
    webcam = cv2.VideoCapture(0)
    sleep(1)
    
    while True:
    
        try:
            check, frame = webcam.read()
            cv2.imshow("Capturing", frame)
            key = cv2.waitKey(1)
    
            img_counter = 0
    
            if key & 0xFF == ord('s'): #press s to take images
                while img_counter < 10:
                    check, frame = webcam.read()
                    cv2.imshow("Capturing", frame)
                    key = cv2.waitKey(1)
                    path = 'F:/Projects/' #folder path to save burst images
                    img_name = "burst_{}.png".format(img_counter)
                    cv2.imwrite(os.path.join(path, img_name), img=frame)
                    print("Processing image...")
                    img_ = cv2.imread(img_name, cv2.IMREAD_ANYCOLOR) #save as RGB color format
                    print("{} written!".format(img_name))
                    img_counter += 1
                    sleep(0.2)
                webcam.release()
                cv2.destroyAllWindows()
                break
    
            elif key == ord('q'): #press q to quit without taking images
                webcam.release()
                cv2.destroyAllWindows()
                break
    
        except(KeyboardInterrupt):
            print("Turning off camera.")
            webcam.release()
            print("Camera off.")
            print("Program ended.")
            cv2.destroyAllWindows()
            break
    
    0 讨论(0)
  • 2020-12-09 06:00

    If your window is going grey then it might be take more processing power. So try to resize image into smaller size image and execute. Sometimes times it freezes while running in ipython notebooks due to pressing any key while performing operation. I had personally executed your problem but I didn't get grey screen while doing it. I did executing directly using terminal. Code and steps are shown below.

    import argparse
    import cv2
    import numpy as np 
    
    # construct the argument parser and parse the arguments
    ap = argparse.ArgumentParser()
    ap.add_argument("-i", "--image", required=True, help="Path to the image")
    args = vars(ap.parse_args())
    
    # load the image, grab its dimensions, and show it
    image = cv2.imread(args["image"])
    (h, w) = image.shape[:2]
    cv2.imshow("Original", image)
    cv2.waitKey(0)
    
    for i in range(0,1000):
        image = cv2.imread(args["image"])
        cv2.imshow("The result",image);
        cv2.waitKey(0)
    

    Run it in terminal:

    1. source activate env_name
    2. python Filename.py --image Imagename.png

    This will get to your result in one window only(updating each time) without freezing and if you want seperate image in every new window then add .format(i) as given below. But Remember to run in terminal only not in jupyter notebooks.

    You can check using terminal commands in this video link https://www.youtube.com/watch?v=8O-FW4Wm10s

    for i in range(0,1000):
        image = cv2.imread(args["image"])
        cv2.imshow("The result{}".format(i),image);
        cv2.waitKey(0)
    

    This may help to get you 1000 images separately.

    0 讨论(0)
提交回复
热议问题