Home > Software design >  OpenCV running otsu thresh on video gives weird window
OpenCV running otsu thresh on video gives weird window

Time:05-11

I am trying to run this opencv code to run otsu tresh on my image and get mask

import cv2
import numpy as np


video = cv2.VideoCapture('video.mp4')
 
 
# loop runs if capturing has been initialized
while True:
 
    # reads frames from a camera
    ret, frame = video.read()
    
    original = frame.copy()
    mask = np.zeros(frame.shape, dtype=np.uint8)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV   cv2.THRESH_OTSU)[1]
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
    opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=3)

    cnts = cv2.findContours(opening, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
    for c in cnts:
        cv2.drawContours(mask, [c], -1, (255,255,255), -1)
        break

    close = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=4)
    close = cv2.cvtColor(close, cv2.COLOR_BGR2GRAY)
    result = cv2.bitwise_and(original, original, mask=close)
    result[close==0] = (255,255,255)


    cv2.imshow('result', result)
video.release()
 
# De-allocate any associated memory usage
cv2.destroyAllWindows()

This is giving me a weird frame window like below

Nothing happens for about 20 seconds until it warns me the program will crash. The only output I get is Killed

This is how the image looks like:

enter image description here

What is causing this and how do I fix it?

CodePudding user response:

You are missing cv2.waitKey(). Put this after cv2.imshow()

    cv2.imshow('result', result)
    cv2.waitKey(0)
video.release()

CodePudding user response:

Mostly, it crashes because it is too much for your device. You can check this by saving some frames from your video streaming, running the same code, and measuring the time it takes to finish. If the time it takes exceeds the period between capturing two frames, it means you are creating a queue of operations until your device is no longer able to handle it. For example, if you're taking 60 frames/minute and your operations take 2 seconds, you will have a delay of 1 min of operations per each 60 frames. It will increase until crashing.

Regarding the OTSU output, it totally depends on your input. You having to try it first on a few frames. Maybe your colors are close to while and everything becomes black because of the inversion.

  • Related