This OpenCV tutorial is a very simple code example of GPU Cuda optical flow in OpenCV written in c++. The configuration of the project, code, and explanation are included for farneback Optical Flow method. Farneback algorithm is a dense method that is used to process all the pixels in the given image. The dense methods are slower but more accurate as all the pixels of the image are processed. In the following example, I am displaying just a few pixes based on a grid. I am not displaying all the pixes. In the opposite to dense method the sparse method like Lucas Kanade using just a selected subset of pixels. They are faster. Both methods have specific applications. Lucas-Kanade is widely used in tracking. The farneback can be used for the analysis of more complex movement in image scene and furder segmentation based on these changes. As dense methods are slightly slower, the GPU and Cuda implementation can lead to great performance improvements to calculate optical flow for all pixels o
Load all pictures inside the Folder into Opencv Mat
This is example how to load all images inside the folder on the Windows system.
I use this to collect vector<Mat> to train classificator for image recognition.
Collect positive and negative pictures
Basically, I load all positive samples from one Folder and negative samples from another Folder into one vector<Mat>. Code show only reading from one folder.Label positive and negative data in Opencv
Also It is easy to count files (images) in one folder and another folder and label the images in your vector in sense that the first part contains only positive samples and when Iterator is greater than Num of positive samples rest we can labeled as negative.
Mat of labels 1 for positive and - 1 for negative you can create like that
This is vector with POSITIVE | NEGATIVE image samples inside
The number of positive images and negative images is count in code at the end of this article.
vector<Mat> samples;
// Create empty Container with INT values 1 for positive and -1 for negative
Mat_<int> labels(1, pos_count + neg_count);
//Fill the first part by 1 for positive samples intil I < pos_count
for (int i = 0; i < pos_count; ++i)
labels(0, i) = 1;
//Fill the rest as negative labels with -1
for (int i = pos_count; i < pos_count + neg_count; ++i)
labels(0, i) = -1;
-----------------------------------------------------------------
#include "opencv2\highgui.hpp"
#include "opencv2\imgproc.hpp"
#include <vector>
#include <fstream>
#include <stdio.h>
#include <iostream>
#include <Windows.h>
#include <iomanip>
vector<string> getNamesOfFile(string folderPath)
{
// vector of names of file within a folder
vector<string> names_ofFiles;
char search[200];
// convert your string search path into char array
sprintf(search, "%s*.*", folderPath.c_str());
// WIN32_FIND_DATA is a structure
// Contains information about the file that is found by the FindFirstFile
WIN32_FIND_DATA Info;
// Find first file inside search path
HANDLE Find = ::FindFirstFile(search, &fdInfo);
// If FindFirstFile(search, &fd); fails return INVALID_HANDLE_VALUE,
// If Find is not equal to INVALID_HANDLE_VALUE do the loop inside the
// IF condition
if(Find != INVALID_HANDLE_VALUE) {
do {
// DWORD dwFileAttributes , FILE_ATTRIBUTE_DIRECTORY ident a directory
// If condition is fine lets push file mane into string vector
if(! (Info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ) {
// Fill vector of names
names_ofFiles.push_back(Info.cFileName);
}
// do - while there is next file
//::FindNextFile the function succeeds, the return value is nonzero and the Info contain //information about next file
}while(::FindNextFile(Find, &Info));
::FindClose(Find);
}
// Return result
return names_ofFiles;
}
int main(int argc, const char** argv)
{
// your path where you can load all images String Path = "C://NegativeSample/";
// Vector that contain all images file mames
vector<string> vectorOfFileNameWithinPath = getNamesOfFile(Path2);
// Load all images and display
// and store inside the another vector StoredImages
Mat_<int> labels(1, pos_count + neg_count);
//Fill the first part by 1 for positive samples intil I < pos_count
for (int i = 0; i < pos_count; ++i)
labels(0, i) = 1;
//Fill the rest as negative labels with -1
for (int i = pos_count; i < pos_count + neg_count; ++i)
labels(0, i) = -1;
-----------------------------------------------------------------
#include "opencv2\highgui.hpp"
#include "opencv2\imgproc.hpp"
#include <vector>
#include <fstream>
#include <stdio.h>
#include <iostream>
#include <Windows.h>
#include <iomanip>
vector<string> getNamesOfFile(string folderPath)
{
// vector of names of file within a folder
vector<string> names_ofFiles;
char search[200];
// convert your string search path into char array
sprintf(search, "%s*.*", folderPath.c_str());
// WIN32_FIND_DATA is a structure
// Contains information about the file that is found by the FindFirstFile
WIN32_FIND_DATA Info;
// Find first file inside search path
HANDLE Find = ::FindFirstFile(search, &fdInfo);
// If FindFirstFile(search, &fd); fails return INVALID_HANDLE_VALUE,
// If Find is not equal to INVALID_HANDLE_VALUE do the loop inside the
// IF condition
if(Find != INVALID_HANDLE_VALUE) {
do {
// DWORD dwFileAttributes , FILE_ATTRIBUTE_DIRECTORY ident a directory
// If condition is fine lets push file mane into string vector
if(! (Info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ) {
// Fill vector of names
names_ofFiles.push_back(Info.cFileName);
}
// do - while there is next file
//::FindNextFile the function succeeds, the return value is nonzero and the Info contain //information about next file
}while(::FindNextFile(Find, &Info));
::FindClose(Find);
}
// Return result
return names_ofFiles;
}
int main(int argc, const char** argv)
{
// your path where you can load all images String Path = "C://NegativeSample/";
// Vector that contain all images file mames
vector<string> vectorOfFileNameWithinPath = getNamesOfFile(Path2);
// Load all images and display
// and store inside the another vector StoredImages
int pos_count = 0;
vector<Mat> StoredImages
for (int i = 0; i < vectorOfFileNameWithinPath.size() -1 ; ++i)
{
// Path "C://NegativeSample/" + i th "Filename.jpg"
String cesta = Path + vectorOfFileNameWithinPath[i];
// Read image into Mat container
Mat img = imread(cesta, IMREAD_COLOR);
// Store images in vector you can use later for example in own ML training
StoredImages.push_back(img)
// Display single image
namedWindow("Display",WINDOW_AUTOSIZE);
imshow( "Display", img);
int key1 = waitKey(10);
// Increase number of positive samples in StoredImages
// Later we push into same vector negative samples
// and we need to know, where the boundary is
pos_count = pos_count + 1;
}
}
vector<Mat> StoredImages
for (int i = 0; i < vectorOfFileNameWithinPath.size() -1 ; ++i)
{
// Path "C://NegativeSample/" + i th "Filename.jpg"
String cesta = Path + vectorOfFileNameWithinPath[i];
// Read image into Mat container
Mat img = imread(cesta, IMREAD_COLOR);
// Store images in vector you can use later for example in own ML training
StoredImages.push_back(img)
// Display single image
namedWindow("Display",WINDOW_AUTOSIZE);
imshow( "Display", img);
int key1 = waitKey(10);
// Increase number of positive samples in StoredImages
// Later we push into same vector negative samples
// and we need to know, where the boundary is
pos_count = pos_count + 1;
}
}
This comment has been removed by the author.
ReplyDeletevisual basic code examples
ReplyDeletevbasic code | Display the Open With dialog