teaching machines

Installing OpenCV on Linux

October 13, 2011 by . Filed under fish vision, public.

I was able to get OpenCV installed without too much trouble on my Ubuntu Linux machine. A simple

sudo apt-get install libcv-dev

did the trick. I tested the installation by writing a short C program to pop up an image:

#include <opencv/highgui.h>
#include <stdio.h>

int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Usage: %s path/to/image", argv[0]);
    exit(1);
  }

  char *path = argv[1];

  IplImage *img = cvLoadImage(path);
  cvNamedWindow(path, CV_WINDOW_AUTOSIZE);
  cvShowImage(path, img);
  cvWaitKey(0);
  cvReleaseImage(&img);
  cvDestroyWindow(path);

  return 0;
}

To test video playback and webcam capturing, I wrote another little program to play a video frame-by-frame:

#include <iostream>
#include <opencv/highgui.h>

int main(int argc, char **argv) {
  CvCapture *movie;
  char *path;

  // Get frames from movie if one provided. Webcam otherwise.
  if (argc > 1) {
    movie = cvCreateFileCapture(argv[1]);
    path = argv[1];
  } else {
    movie = cvCreateCameraCapture(0);
    path = strdup("camera");
  }

  cvNamedWindow(path, CV_WINDOW_AUTOSIZE);
  IplImage *frame;

  // Play movie one frame at a time, advancing only when a non-Escape
  // key is pressed. Hit Escape to escape.
  char c = '\0';
  frame = cvQueryFrame(movie);
  while (frame && c != 27) {
    cvShowImage(path, frame);
    c = cvWaitKey(0);
    frame = cvQueryFrame(movie);
  }

  cvReleaseCapture(&movie);
  cvDestroyWindow(path);

  return 0;
}

I compiled these at the shell with:

gcc -o image image.cpp -lcv -lhighgui
gcc -o framer framer.cpp -lcv -lhighgui

Installation on Linux was pleasant.