DenseOpticalFlow - Shape problems

asked 2020-02-24 08:44:25 -0600

Hi all,

I am working on a project where I have to implement the DOF algorithm: calcOpticalFlowFarneback().

I've followed the opencv tutorial and it works very well with .avi files.

The "problem" is that I have to use .tif image sequences. As you can see in the tutorial he's using gray image and my .tif are already gray-scale but the algorithm doesn't like the shape of the calculated magnitude and phase:

-Tiff Extraction:

VideoToSOF = [0 for _ in range(len(images))]
print("Extracting frames..")
VideoFrameCounter = 0
for image in images:
    VideoToSOF[VideoFrameCounter] = image
    VideoFrameCounter += 1
    print("Video Frame n°%d/%d" % (VideoFrameCounter,len(images)),end='\r')

-DOF Code:

frame2 = VideoToSOF[counter]
next = frame2
flow = cv2.calcOpticalFlowFarneback(prvs,next, flow, pyr_scale, levels, winsize, iteration, poly_n, poly_sigma, flags)

mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1]) 

hsv[...,0] = ang*180/np.pi/2
hsv[...,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)

As you can see my code is totally equal to the tutorial's one. Anyway i get this error:

Traceback (most recent call last): File ".\varioustest_script.py", line 73, in <module> hsv[...,0] = ang*180/np.pi/2 ValueError: could not broadcast input array from shape (300,300) into shape (300)

Do you know Why? I mean, the image should have the same shape as the .avi one..I don't get it!

edit retag flag offensive close merge delete

Comments

where/how do you construct hsv ?

what do you want to do with the optflow ? (the hsv is only for visualization)

berak gravatar imageberak ( 2020-02-24 09:03:49 -0600 )edit
1

I've followed the tutorial. Here's how i construct the HSV.

hsv = np.zeros_like(frame1) 
hsv[...,1] = 255

HSV is only for visual: yes, but why it gives me this error? The size of frames, 'mag' and 'ang' are all the same if I use .avi or .tif.

I can avoid real-time image show but I would like to see it!

I want to use OptFlow to analyze phase and mag of the .tif I give to it

DoubleR gravatar imageDoubleR ( 2020-02-24 09:28:15 -0600 )edit
1

if frame1 is a grayscale tiff, your hsv only has a single channel, too

berak gravatar imageberak ( 2020-02-24 09:55:47 -0600 )edit