返回

python-OpenCV在视频中运行otsu thresh提供了一个奇怪的窗口

发布时间:2022-05-13 00:42:42 249
# node.js

我正在尝试运行此 opencv 代码以在我的图像上运行 otsu tresh 并获取掩码

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()

这给了我一个奇怪的框架窗口,如下所示

大约 20 秒内什么都没有发生,直到它警告我程序将崩溃。我得到的唯一输出是 Killed

是什么原因造成的,我该如何解决?

 

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(1)
按点赞数排序
用户头像