Ask Your Question
0

Numpy Arrays

asked 2016-02-10 10:15:58 -0600

Nain gravatar image

I am using python bindings to convert a project ,initially made in C++ , in python. The following is the snapshot of some part of the code :

cv::Mat debugImage;
CvCapture* capture;
cv::Mat frame;
capture = cvCaptureFromCAM( -1 );
  if( capture )
  {
      while( true ) 
      {
        frame = cvQueryFrame( capture );
        // mirror it
        cv::flip(frame, frame, 1);
        frame.copyTo(debugImage);
       }
   }

Here frame is defined as matrix which is then copied to the debugImage matrix inside the loop. But if I use numpy arrays declaration for frame and debugimage and then copy it using the copyto method of numpy, it throws error. Please help me to covert this code into python.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-02-10 10:39:27 -0600

berak gravatar image

updated 2016-02-10 10:46:06 -0600

aww, you're looking at outdated c-api code from pre 2010 (which is not using numpy arrays to store images or generic matrices)

take a close look at the python tutorials and, ofc, the sample codes

but here's the nutshell:

import cv2
import numpy as np

cam = cv2.VideoCapture(0)
while cam.isOpened():
   ok,img = cam.read()
   if (not ok):
       continue # maybe you got an oldish webcam, that needs some 'warmup'
    # if you had a video **file** instead, you want to break here instead (reached last frame)
    #
    # your img processing would go here ;)
    #
    cv2.imshow("lalala", img)
    k = cv2.waitKey(10) & 0xff
    if k==27:
        break
edit flag offensive delete link more

Comments

This I know very well. Please tell me how to conver the debugImage matrix and the frame matrix into numpy arrays

Nain gravatar imageNain ( 2016-02-10 11:07:27 -0600 )edit

not sure, if i understand you correctly here: conver the debugImage matrix and the frame matrix

berak gravatar imageberak ( 2016-02-10 11:27:43 -0600 )edit

In the original code above, debugImage and frame are declared as Matrix . The frame is being used to capture the frames from the camera and then it copies the contents of frame matrix to the debugImage matrix. Now when I try to do the same in python by declaring debugImage matrix and frame as numpy arrays , python throws an error. Please tell me how to code this then.

Nain gravatar imageNain ( 2016-02-13 01:46:53 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-02-10 10:15:58 -0600

Seen: 1,862 times

Last updated: Feb 10 '16