hi all, im realising motion detection that compare two successive frame saved in directory , using OpenCv but, the code is successfully debugged, but when i run the app it displays an exception, any help please
my source code
#include <iostream>
#include <fstream>
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <time.h>
#include <dirent.h>
#include <sstream>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string>
#include <iomanip>
#include <direct.h>
using namespace std;
using namespace cv;
// Check if the directory exists, if not create it
// This function will create a new directory if the image is the first
// image taken for a specific day
inline void directoryExistsOrCreate(const char* pzPath)
{
DIR *pDir;
// directory doesn't exists -> create it
// mkdir("C:\mydir");
// if directory exists we opened it and we
// have to close the directory again.
(pDir = opendir(pzPath));
(void) closedir(pDir);
}
// When motion is detected we write the image to disk
// - Check if the directory exists where the image will be stored.
// - Build the directory and image names.
int incr = 0;
inline bool saveImg(Mat image, const string DIRECTORY, const string EXTENSION, const char * DIR_FORMAT, const char * FILE_FORMAT)
{
stringstream ss;
time_t seconds;
struct tm * timeinfo;
char TIME[80];
time(&seconds);
// Get the current time
timeinfo = localtime(&seconds);
// Create name for the date directory
strftime(TIME, 80, DIR_FORMAT, timeinfo);
ss.str("");
ss << DIRECTORY << TIME;
directoryExistsOrCreate(ss.str().c_str());
ss << "/cropped";
directoryExistsOrCreate(ss.str().c_str());
// Create name for the image
strftime(TIME, 80, FILE_FORMAT, timeinfo);
ss.str("");
if (incr < 100) incr++; // quick fix for when delay < 1s && > 10ms, (when delay <= 10ms, images are overwritten)
else incr = 0;
ss << DIRECTORY << TIME << static_cast<int>(incr) << EXTENSION;
return imwrite(ss.str().c_str(), image);
}
// Check if there is motion in the result matrix
// count the number of changes and return.
inline int detectMotion(const Mat & motion, Mat & result, Mat & result_cropped,
int x_start, int x_stop, int y_start, int y_stop,
int max_deviation,
Scalar & color)
{
// calculate the standard deviation
Scalar mean, stddev;
meanStdDev(motion, mean, stddev);
// if not to much changes then the motion is real (neglect agressive snow, temporary sunlight)
if (stddev[0] < max_deviation)
{
int number_of_changes = 0;
int min_x = motion.cols, max_x = 0;
int min_y = motion.rows, max_y = 0;
// loop over image and detect changes
for (int j = y_start; j < y_stop; j += 2) { // height
for (int i = x_start; i < x_stop; i += 2) { // width
// check if at pixel (j,i) intensity is equal to 255
// this means that the pixel is different in the sequence
// of images (prev_frame, current_frame, next_frame)
if (static_cast<int>(motion.at<uchar>(j, i)) == 255)
{
number_of_changes++;
if (min_x>i) min_x = i;
if (max_x<i) max_x = i;
if (min_y>j) min_y = j;
if (max_y<j) max_y = j;
}
}
}
if (number_of_changes) {
//check if not out of bounds
if (min_x - 10 > 0) min_x -= 10;
if (min_y - 10 > 0) min_y -= 10;
if (max_x + 10 < result.cols - 1) max_x += 10;
if (max_y + 10 < result.rows - 1) max_y += 10;
// draw rectangle round the changed pixel
Point x(min_x, min_y);
Point y(max_x, max_y);
Rect rect(x, y);
Mat cropped = result(rect);
cropped.copyTo(result_cropped);
rectangle(result, rect, color, 1);
}
return number_of_changes;
}
return 0;
}
int main(int argc, char * const argv[1])
{
const string DIR = "C:\yosri1"; // directory where the images will be stored
const string EXT = ".jpg"; // extension of the images
const int DELAY = 5000; // in mseconds, take a picture every 5 seconds
const string LOGFILE = "/home/pi/motion_src/log";
// Format of directory
string DIR_FORMAT = "%d%h%Y"; // 1Jan1970
string FILE_FORMAT = DIR_FORMAT + "/" + "%d%h%Y_%H%M%S"; // 1Jan1970/1Jan1970_12153
string CROPPED_FILE_FORMAT = DIR_FORMAT + "/cropped/" + "%d%h%Y_%H%M%S"; // 1Jan1970/cropped/1Jan1970_121539
//directory where images be loaded
std::string folder = "C:\yosri";
std::string suffix = ".jpg";
int counter = 0;
cv::Mat myImage;
Mat prev_frame, current_frame, next_frame;
Mat result, result_cropped;
std::string name;
while (1)
{
std::stringstream ss;
ss << std::setw(1) << std::setfill('0') << counter; //00, 01, 02, etc...
std::string number = ss.str();
name = folder + number + suffix;
myImage = cv::imread(name);
prev_frame = result = myImage;
counter++;
current_frame = cv::imread(name);
counter++;
next_frame = cv::imread(name);
cvtColor(current_frame, current_frame, CV_RGB2GRAY);
cvtColor(prev_frame, prev_frame, CV_RGB2GRAY);
cvtColor(next_frame, next_frame, CV_RGB2GRAY);
}
// d1 and d2 for calculating the differences
// result, the result of and operation, calculated on d1 and d2
// number_of_changes, the amount of changes in the result matrix.
// color, the color for drawing the rectangle when something has changed.
Mat d1, d2, motion;
int number_of_changes, number_of_sequence = 0;
Scalar mean_, color(0, 255, 255); // yellow
// Detect motion in window
int x_start = 10, x_stop = current_frame.cols - 11;
int y_start = 350, y_stop = 530;
// If more than 'there_is_motion' pixels are changed, we say there is motion
// and store an image on disk
int there_is_motion = 5;
// Maximum deviation of the image, the higher the value, the more motion is allowed
int max_deviation = 20;
// Erode kernel
Mat kernel_ero = getStructuringElement(MORPH_RECT, Size(2, 2));
// All settings have been set, now go in endless loop and
// take as many pictures you want..
while (true) {
// Take a new image
prev_frame = current_frame;
current_frame = next_frame;
next_frame = cv::imread(name);
result = next_frame;
cvtColor(next_frame, next_frame, CV_RGB2GRAY);
// Calc differences between the images and do AND-operation
// threshold image, low differences are ignored (ex. contrast change due to sunlight)
absdiff(prev_frame, next_frame, d1);
absdiff(next_frame, current_frame, d2);
bitwise_and(d1, d2, motion);
threshold(motion, motion, 35, 255, CV_THRESH_BINARY);
erode(motion, motion, kernel_ero);
number_of_changes = detectMotion(motion, result, result_cropped, x_start, x_stop, y_start, y_stop, max_deviation, color);
// If a lot of changes happened, we assume something changed.
if (number_of_changes >= there_is_motion)
{
if (number_of_sequence>0) {
saveImg(result, DIR, EXT, DIR_FORMAT.c_str(), FILE_FORMAT.c_str());
saveImg(result_cropped, DIR, EXT, DIR_FORMAT.c_str(), CROPPED_FILE_FORMAT.c_str());
}
number_of_sequence++;
}
else
{
number_of_sequence = 0;
// Delay, wait a 1/2 .
cvWaitKey(DELAY);
}
}
return 0;
}