如何使用Python在Opencv中检测黑框

时间:2019-03-14 21:17:43

标签: python-3.x opencv

我有一个关于广播数据的项目。我为python编写了从视频到帧的拆分。但我只是想知道,如果我只能在blackFrames之间拍摄视频帧会更好。我分享我的代码;

import cv2
import time
import os

def video_to_frames(input_loc, output_loc):
"""Function to extract frames from input video file
and save them as separate frames in an output directory.
Args:
    input_loc: Input video file.
    output_loc: Output directory to save the frames.
Returns:
    None
"""
try:
    os.mkdir(output_loc)
except OSError:
    pass
# Log the time
time_start = time.time()
# Start capturing the feed
cap = cv2.VideoCapture(input_loc)
# Find the number of frames
video_length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) - 1
print ("Number of frames: ", video_length)
count = 0
print ("Converting video..\n")
# Start converting the video
while cap.isOpened():
    # Extract the frame
    ret, frame = cap.read()
    # Write the results back to output location.
    cv2.imwrite(output_loc + "/%#05d.jpg" % (count+1), frame)
    count = count + 1
    # If there are no more frames left
    if (count > (video_length-1)):
        # Log the time again
        time_end = time.time()
        # Release the feed
        cap.release()
        # Print stats
        print ("Done extracting frames.\n%d frames extracted" % count)
        print ("It took %d seconds forconversion." % (time_end-time_start))
        break

input_loc = 'try.mp4'
output_loc = 'try/'
video_to_frames(input_loc, output_loc)

2 个答案:

答案 0 :(得分:1)

由于openCV中的图像(或在您的情况下为帧)以numpy数组表示,因此可以将其取低值(表示黑框)进行平均。

import numpy as np

# converts the frame to gray scale for easier computation
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

if np.average(gray) < 20:
    # skips an iteration, so the frame isn't saved
    continue

在这种情况下,20是用于“黑色图像”的阈值。如果有些更轻,则可以增加此常数。因此,您的总代码应如下所示:

import cv2
import numpy as np
import time
import os


def video_to_frames(input_loc, output_loc):
"""Function to extract frames from input video file
and save them as separate frames in an output directory.
Args:
    input_loc: Input video file.
    output_loc: Output directory to save the frames.
Returns:
    None
"""
try:
    os.mkdir(output_loc)
except OSError:
    pass


# Log the time
time_start = time.time()
# Start capturing the feed
cap = cv2.VideoCapture(input_loc)

# Find the number of frames
video_length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) - 1
print("Number of frames: ", video_length)
count = 0

print("Converting video..\n")
# Start converting the video
while cap.isOpened():
    # Extract the frame
    ret, frame = cap.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    if np.average(gray) < 20:
        # skips an iteration, so the frame isn't saved
        continue

    # Write the results back to output location.
    cv2.imwrite(output_loc + "/%#05d.jpg" % (count+1), frame)
    count = count + 1

    # If there are no more frames left
    if count > video_length-1:
        # Log the time again
        time_end = time.time()
        # Release the feed
        cap.release()
        # Print stats
        print("Done extracting frames.\n%d frames extracted" % count)
        print("It took %d seconds forconversion." % (time_end-time_start))
        break

input_loc = 'try.mp4'
output_loc = 'try/'
video_to_frames(input_loc, output_loc)

答案 1 :(得分:0)

就我而言,我有一些镜框多数为黑色,但并非全黑。 np.average(image) < 20仍将它们标记为黑框。取而代之的是,我使用np.max(image) < 20在我的情况下效果很好。正如Komron所说,可能需要针对您的用例调整20。