OpenCV Error: Assertion failed in function Mat

asked 2014-04-18 03:42:43 -0500

gialang gravatar image

hi everyone, i'm making solution sercurity for raspberry and i use this code:


// Created by Cedric Verstraeten on 18/02/14. // Copyright (c) 2014 Cedric Verstraeten. All rights reserved. //

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="">

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 if ( pzPath == NULL || (pDir = opendir (pzPath)) == NULL) mkdir(pzPath, 0777); // if directory exists we opened it and we // have to close the directory again. else if(pDir != NULL) (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 << "/cropped";

// Create name for the image
strftime (TIME,80,FILE_FORMAT,timeinfo);
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>(<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="" &gt;="" 0)="" min_x="" -="10;" if(min_y-10="" &gt;="" 0)="" min_y="" -="10;" if(max_x+10="" &lt;="" result.cols-1)="" max_x="" +="10;" if(max_y+10="" &lt;="" 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 ... (more)

edit retag flag offensive close merge delete