读取图像并在打开的状态下进行处理

2024-03-29 13:05:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我对opencv和python还不熟悉。我需要一个建议。例如,我有两个视频:第一个是2分钟的视频:在城市里有一个广告牌,上面有商业广告,背景是汽车和人,第二个是10秒的商业广告,就在这个广告牌上。我想知道2分钟的视频里有没有10秒的广告。为了达到这个目标,我将比较不同视频中的帧。 首先,我采取第一个视频和分裂的帧,然后我需要选择这个广告牌从帧(要做到这一点,我减去背景)。我的问题是这个过程不起作用。我不能加载帧和垃圾从那里这个对象。你知道吗

import cv2
import numpy as np
import os
from glob import glob

os.chdir('C:/Python27/Projects/VidComp/asd') 
cap = cv2.VideoCapture('C:/Python27/Projects/VidComp/asd/test_video.mov')
cap.isOpened()

count = 0
success = True
while success:
  success,image = cap.read()
if count % 91 == 0:
    cv2.imwrite("image%d.jpg" % count, image) 
count += 1

cv2.namedWindow('image', cv2.WINDOW_NORMAL)

#Load the Image
 for fn in glob('*.jpg'):
 img0 = cv2.imread(fn)

 height, width = imgo.shape[:2]

 #Create a mask holder
 mask = np.zeros(imgo.shape[:2],np.uint8)

 #Grab Cut the object
 bgdModel = np.zeros((1,65),np.float64)
 fgdModel = np.zeros((1,65),np.float64)

  #Hard Coding the Rect The object must lie within this rect.
  rect = (750,500,450,950)
  cv2.grabCut(imgo,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)
  mask = np.where((mask==2)|(mask==0),0,1).astype('uint8')
  img1 = imgo*mask[:,:,np.newaxis]

  #Get the background
  background = imgo - img1

  #Change all pixels in the background that are not black to white
  background[np.where((background > [0,0,0]).all(axis = 2))] = [255,255,255]

   #Add the background and the image
   final = background + img1

     #To be done - Smoothening the edges

    #cv2.imshow('image', img1 )
     cv2.imwrite('*.jpg',img1 )

    cv2.waitKey(0)

    k = cv.waitKey(0)

    if k==27:
         cv2.destroyAllWindows()

如果你能帮我解决这个问题,我将非常高兴。你知道吗


Tags: theimageimport视频countnpmaskcv2