简体   繁体   中英

OpenCv - create mask from edge - crop and save image

I have a video with opaque logo, my goal is to identify area of image that remain still and extract it.

this is my code:

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

The cv2.accumulateWeighted funtion, after time pass, permits to clearly identify parts that remain mostly still on the frames. orig frame part: 在此处输入图片说明

edged averaged frame part: 在此处输入图片说明

How can I create a mask for the entire averaged edged part, crop it and save it in a separate image?

You can use cv2.findContours() to do connected component analysis. Then use cv2.boundingRect() to get the bounding box. Then you can crop the image using img[r1:r2, c1:c2] using Numpy slicing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM