-2

I use python to record the writing process (from 0 to the end), and use opencv to convert the writing video into many frames. But when observing the results, I found that sometimes 2-3 frames of pictures are equal. I want to keep only the first picture that is equal when writing the file, and add the timeline. Can any experts give me some advice? The following is my code:

#cv2

import numpy as np
import cv2
from PIL import Image


def get_images_from_video(video_name, time_F):
    video_images = []
    vc = cv2.VideoCapture(video_name)
    c = 1
    
    #判斷是否開啟影片
    if vc.isOpened(): 
        rval, video_frame = vc.read()
    else:
        rval = False

    #擷取視頻至結束
    while rval:  
        rval, video_frame = vc.read()
        
        #每隔幾幀進行擷取
        if(c % time_F == 0): 
            video_images.append(video_frame)     
        c = c + 1
    vc.release()
    
    return video_images 

#time_F越小,取樣張數越多
time_F = 24
#影片名稱
video_name = 'writingvideo2/11_0330吳恒基.mp4' 
#numpy
#讀取影片並轉成圖片
video_images = get_images_from_video(video_name, time_F) 
    
    
crop_img=[]
    
   
#顯示出所有擷取之圖片
for i in range(0,len(video_images)): 
    
    #i_str=str(i)    
    #cv2.imshow('windows',video_images[i])
    
    x=50
    y=150
    w=1800
    h=650
    crop_img.append(video_images[i][y:y+h,x:x+w])
    cv2.imshow("cropped", crop_img[i])
    
    #correct write path
    
    i_str=str(i)
    filename='output2/image/11/'+i_str+'.png'
    cv2.imwrite(filename, crop_img[i], [cv2.IMWRITE_JPEG_QUALITY, 90])
    
    
    cv2.waitKey(100)
    
cv2.destroyAllWindows

So I want is, if they are equal, keep the previous frame and discard the current photo

Aadi
  • 152
  • 15
陳昱廷
  • 25
  • 1
  • 5

1 Answers1

1

You need to employ some kind of a metric which calculates the similarity between two consecutive frames. This is well studied problem and you can gain a lot of insight by reading this question. Image comparison - fast algorithm

But be aware that dropping frames from your video will change the speed and thus the length of the video.

Paloha
  • 558
  • 6
  • 14