Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hello, I'd like to detect clouds movement filmed with a camera. I'm coding with Python. To do so, I choose two different pictures from this film. Next I detect the interesting points with the goodFeaturesToTrack() function. At last, I'd like to use the calcOpticalFlowPyLK() function. When I'm using the last function and I'm meeting the errror that's in title. You'll find the code underneath :

#-*-coding:utf-8-*-

import cv2 as cv
import sys
import numpy as np
import time as time

##### Paramètres variables à entrer #######

nom_video="IMG_0595.mp4"
path="/Users/oliviersamin/Documents/Projets_perso/Projet_FORSOLAR/Developement/Traitement image/Premier developpement/Photos_References"
nom=path+"/"+nom_video


duree=5
nombre_coins=10

### Fonctions ####
def sauvegarde(nom_fichier,image):
    cv.imwrite(nom_fichier,image)

def chargement(nom_fichier):
    data=np.loadtxt(nom_fichier)
    return(data)

def ouverture_image(nom):
    #image=cv.imread('IMG_book.png',1)
    image=cv.imread(nom,1)
    return (image)

def HSV_masque_bleu(image):
    hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
    low_b=np.array([107,50,50])
    high_b=np.array([125,255,255])
    maskb=cv.inRange(hsv,low_b,high_b)
    resb_and=cv.bitwise_and(image,image, mask= maskb)
    return(resb_and)

def gris(resb_and):
    bgr=cv.cvtColor(resb_and, cv.COLOR_HSV2BGR) # etape HSV -> BGR
    gray=cv.cvtColor(resb_and, cv.COLOR_BGR2GRAY) # etape BGR -> GRAY
    return(gray)

def seuillage(gray):
    ret,th_b = cv.threshold(gray,10,255,cv.THRESH_BINARY)# tout ce qui est au dessus de 10 passe en blanc
    th=255-th_b #j'inverse pour avoir les nuages blanc et le ciel noir
    return(th,th_b)

def detection_coins(th_b,image,nombre_coins):
    #feature_params=dict(maxCorners=100,qualityLevel=0.3,minDistance=7,blockSize=7)
    corners=cv.goodFeaturesToTrack(th_b,nombre_coins,0.00001,1)
    #corners=cv.goodFeaturesToTrack(th_b,mask=None,**feature_params)
    corners=np.int0(corners)
    for i in corners:
        x,y=i.ravel()
        cv.circle(image,(x,y),3,0,-1)
    return(image,corners)


def verifs(th,image,num):
    cv.imshow('threshold binaire'+str(num),th)
    cv.imshow('image avec coins detectes'+str(num),image)
##    cv.waitKey(0)
##    cv.destroyAllWindows()

def calcul_trajectoire(im1,im2,coins):
    test,st,err=cv.calcOpticalFlowPyrLK (im1,im2,coins)
    return (test)

def choix_images(nom,duree):
    cap = cv.VideoCapture(nom)
    cap.open(nom)
    test=[]
##    boucle=0
    t0=time.localtime()
    d=0
    while(d<duree):
##        print "boucle ",str(boucle+1)
        ret,frame = cap.read()
        test.append(frame)
##        cv.imshow('frame',frame)
##        boucle+=1
        t=time.localtime()
        d=t.tm_sec-t0.tm_sec
    return (test[0],test[-1])

##### prog principal #######

image1,image2=choix_images(nom,duree)
im1=HSV_masque_bleu(image1) 
im2=HSV_masque_bleu(image2) 
gris1=gris(im1)
gris2=gris(im2)
nb1,nb1_inv=seuillage(gris1)
nb2,nb2_inv=seuillage(gris2)
i1,c1=detection_coins(nb1_inv,image1,nombre_coins)
i2,c2=detection_coins(nb2_inv,image2,nombre_coins)
image_flow=calcul_trajectoire(image1,image2,c1)
##verifs(nb1,i1,1)
##verifs(nb2,i2,2)
cv.imshow("image avec flow",image_flow)
cv.waitKey(0)
cv.destroyAllWindows()

Thanks for your help