Image Processing : Acquisition with OpenCV Part I

I assume that you already configure your project in Visual Studio C++ with OpenCV from the previous tutorial here.

Before manipulating digital image or video, we need to read the input which obtained from optical device. OpenCV make this acquisition easier. But first we need to make an OpenFileDialog class to browse the file we want.


Create a File Browser in C++

First create OpenFileDialog.h and OpenFileDialog.cpp in your Visual Studio project.

open-file-browser

Write this code in OpenFileDialog.h

#pragma once
#include <Windows.h>
#include <Commdlg.h>
#include <tchar.h>
class OpenFileDialog
{
public:
TCHAR* DefaultExtension; ///< Default extension
TCHAR* FileName; ///< File name
TCHAR* Filter; ///< Filter file type
TCHAR* Title; ///< GUI title
TCHAR* InitialDir; ///< Initial directory
int FilterIndex; ///< Filter index
int Flags; ///< Flag, true if file is open
HWND Owner; ///< Window handle
/** Open file */
OpenFileDialog(void);
/** Show GUI */
bool ShowDialog();
};

Write this code in OpenFileDialog.cpp

#include “OpenFileDialog.h”
// Open file
OpenFileDialog::OpenFileDialog(void)
{
this->DefaultExtension = 0;
this->FileName = new TCHAR[MAX_PATH];
this->Filter = 0;
this->FilterIndex = 0;
this->Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
this->InitialDir = 0;
this->Owner = 0;
this->Title = 0;
}
// Show GUI
bool OpenFileDialog::ShowDialog()
{
OPENFILENAME ofn ;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = this->Owner;
ofn.lpstrDefExt = this->DefaultExtension;
ofn.lpstrFile = this->FileName;
ofn.lpstrFile[0] = ‘\0’;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter = this->Filter;
ofn.nFilterIndex = this->FilterIndex;
ofn.lpstrInitialDir = this->InitialDir;
ofn.lpstrTitle = this->Title;
ofn.Flags = this->Flags;
GetOpenFileName(&ofn);
if (_tcslen(this->FileName) == 0) return false;
return true;
}

Now in your main program write this code, for example in your MainProgram.cpp this are the headers that will be used:

#include <Windows.h>
#include <string>
#include <opencv\cv.h>
#include <opencv\highgui.h>
#include “OpenFileDialog.h”
using namespace std;
using namespace cv;

We will wrap up that class on simple Open() function that return path to any file we choose.

/** Open File */
string Open(string paths)
{
string path = “”;
TCHAR* initDir = new TCHAR[paths.size() + 1];
initDir[paths.size()] = 0;
copy(paths.begin(), paths.end(), initDir);
OpenFileDialog* openFileDialogs = new OpenFileDialog();
openFileDialogs->FilterIndex = 1;
openFileDialogs->InitialDir = initDir;
openFileDialogs->Title = _T(“Open image file”);
if (openFileDialogs->ShowDialog())
{
wstring arr_w(openFileDialogs->FileName);
string arr_s(arr_w.begin(), arr_w.end());
path = arr_s;
}
return path;
}

Read Image

OpenCV can read many types of image such as .jpg, .bmp, .png, etc. This code shows how you open file browser and read an image with OpenCV.

int main(int argc, char** argv)
{
Mat image; // create matrix container for image
string path = Open(“”); // get path image
if (path != “”) // check if path is empty
{
image = imread(path, 1); // read image from path
imshow(“Preview Image”, image); // show image to the screen
cvWaitKey(0); // wait any key pressed to continue
}
image.release(); // do not forget to release what you have created
return 0;
}

Read Video
OpenCV can also read file video format. This code shows how to open file video and play it using OpenCV.

int main(int argc, char** argv)
{
VideoCapture video; // memory container for video
Mat frame; // matrix container for frame
int nFrame; // number of frame
int frameRate; // frame rate
double fps; // frame per second
bool status; // status if capture is vaild
string path = Open(“”); // open file video
if (path != “”) // check if path is empty
{
video = VideoCapture(path); // load video from path to memory
if (video.isOpened()) // check if video is valid
{
fps = video.get(CV_CAP_PROP_FPS); // get frame per second from the video
nFrame = (int)video.get(CV_CAP_PROP_FRAME_COUNT); // get number of frame
frameRate = 1000 / fps; // set frame rate
status = video.set(CV_CAP_PROP_POS_FRAMES, 1); // go to frame 1
while (status == true) // check if status is true
{
video >> frame; // begin grab frame by frame along with the loop iteration
if (frame.empty() == false) // check if grab frame is succeed
{
imshow(“Preview Video”, frame); // show frame video on the screen
if (cvWaitKey(frameRate) == 32) // check if user press ‘esc’
status == false; // end the loop
}
else
status == false; // end the loop
}
}
}
// release after memory is not used
frame.release();
video.release();
return 0;
}

Read Webcam 

This code shows how to read webcam.

// read webcam
int main(int argc, char** argv)
{
VideoCapture webcam; // memory container for webcam stream
Mat frame; // matrix container for frame
bool status; // status looping
// capture webcam index 0
// if we have 2 webcam, then the index will be 0 or 1
webcam = VideoCapture(0);
if (webcam.isOpened()) // check if webcam is valid
{
int keyCode = 1; // key to end the loop
// set frame width from webcam stream
webcam.set(CV_CAP_PROP_FRAME_WIDTH, 640);
// set frame height from webcam stream
webcam.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
// begin looping until user press ‘esc’
while (keyCode != 32)
{
webcam >> frame; // grab frame from webcam stream
if (frame.empty() == false) // check if frame is valid
{
imshow(“Preview Webcam”, frame); // show frame on the screen
keyCode = cvWaitKey(1); // give a miliseconds gap between frame
}
}
}
// release every memory
frame.release();
webcam.release();
return 0;
}
 

Done! There was some basic acquisition in OpenCV.