读取图像并在opencv中对其进行处理

时间:2018-07-20 06:51:31

标签: python opencv

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

如果您能帮助我解决问题,将非常高兴。

0 个答案:

没有答案