OpenCv -从边缘创建蒙版-裁剪并保存图像

j5fpnvbx  于 7个月前  发布在  其他
关注(0)|答案(1)|浏览(102)

我有一个不透明的标志视频,我的目标是确定图像区域保持静止并提取它。
这是我的代码:

import cv2
import numpy as np
import imutils
c = cv2.VideoCapture('sky.mp4')
_,f = c.read()
avg2 = np.float32(f)
while(1):
    _,f = c.read()
    cv2.accumulateWeighted(f,avg2,0.005)
    #cv2.accumulateWeighted(f,avg2,0.01)
    res2 = cv2.convertScaleAbs(avg2)

    # load the query image, compute the ratio of the old height
    # to the new height, clone it, and resize it
    ratio = res2.shape[0] / 300.0
    orig = res2.copy()
    res2 = imutils.resize(res2, height = 600)   

    # convert the image to grayscale, blur it, and find edges
    # in the image
    gray = cv2.cvtColor(res2, cv2.COLOR_BGR2GRAY)
    gray = cv2.bilateralFilter(gray, 11, 17, 17)
    edged = cv2.Canny(gray, 30, 200)    

    cv2.imshow('img',f)
    cv2.imshow('avg2',edged)
    k = cv2.waitKey(20)

    if k == 27:
        break
cv2.destroyAllWindows()
c.release()

字符串
cv2.accumulateWeighted函数允许在经过时间后清楚地识别大部分仍保留在帧上的部分。orig帧部分:

边缘平均帧部分:

如何为整个平均边缘部分创建蒙版,裁剪并保存到单独的图像中?

68de4m5k

68de4m5k1#

您可以使用cv2.findContours()进行连通分量分析。然后使用cv2.boundingRect()获取边界框。然后您可以使用Numpy切片使用img[r1:r2, c1:c2]裁剪图像。

相关问题