1

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?

Dark Apostle
  • 189
  • 4
  • 18
  • 1
    cv2 needs `waitKey` to update data in window. – furas May 11 '22 at 01:41
  • 1
    some systems may kill program if it doesn't get key/mouse events - and `waitKey` is for checking if there are new key/mouse events - because system may think that program freezed and it has to be killed. – furas May 11 '22 at 01:44

2 Answers2

3

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

    cv2.imshow('result', result)
    cv2.waitKey(0)
video.release()
toyota Supra
  • 3,181
  • 4
  • 15
  • 19
-1

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.

Esraa Abdelmaksoud
  • 1,307
  • 12
  • 25