0

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
fd.png
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;
  }
3
Contributors
22
Replies
79
Views
3 Months
Discussion Span
Last Post by rproffitt
Featured Replies
  • That's one way to narrow it done. Let me share I was on a team behind a motion detection system used for doors. I see it all over the US today even 17 years later. It works but we didn't use OpenCV. It's a custom single board design without an … Read More

  • > my thesis will be on july I have no advice to offer regarding OpenCV, but I'll offer some generic advice. If you're writing a graduate/professional level thesis, that's usually a huge project, generally far too big for an online forum where the posters are anonymous and the advice is … Read More

0

Visual Studio let's me single step code here. I think you need to use this (single step) to find which line blew up. Few will duplicate your project and can't since they don't have your system, so let's have you point out which line blew up.

Note: Edited for grammar.

Edited by rproffitt: Grammar correction.

0

dr reprofitt again :) thank youuuuu :D
dr i dont know how to line out which line blew up with VS but when i commented this three lines it debbugged without exception but without noo results
124.png

1

That's one way to narrow it done.

Let me share I was on a team behind a motion detection system used for doors. I see it all over the US today even 17 years later. It works but we didn't use OpenCV. It's a custom single board design without an OS. Many call them embedded computing.

Anyhow, the entire system works with 2 cameras (one for each side of the door) and in short we capture an image then run it through a Sobel filter to get edges. No pixel method here. Just count edges. When the edge count changes in a zone (group of pixels) then we flag up as movement in that zone.

Got it?

0

Unsure what your question was. The system was done from scratch with the usual bit of assembler to setup the stack and then jump to the code which was in C. As you know it matters little if it was C or C++ for this discussion. We are talking basics here. How to troubleshoot, how to detect motion.

0

you are right dr, i search for systemC code for motion detection that compare two frame stored in directory but due lake of ressource for his language, i decide to use c++ if i implement it using systemC it would be better but no problem. how can i get this code dr ?

0

As it was a company project I will only share high level ideas.

For example only your newest to motion detection would compare pixels. A change in light intensity would not be motion so comparing pixels is going to give you motion events without any motion at all.

Remember, language choice does not matter. Algorithm matters.

0

yes you are right , you mean to compare macropixel ? i read an article in a scientific journal who developp this technology, if you want i will join to you the article, also comparing macro pixel will gain computational feature, dr my intention is not to developp a robust motion detector system rather than valdating my project by an example of a system i choose motion detector.

0

Here's the thing. I want you to be successful plus I've worked in this area. So go ahead and try it your way. I might be around once you figure out comparing pixels fails for the reason given and other reasons. Once you get past your method we can chat again.

I don't want to upset you here. Just someone that worked on a product that is working and deployed. Not that I can share code but I can share what we learned. Yes, I know folk that need to try the pixel compare method. Let them do that so they are ready for the next method.

0

dr if you told me for a robust method for motion detector it will be better, as you see for the method that i used found on openCv website i dont use it so i cant judge it but as you say a change in luminosity will cause problem also this method runs exception and ididnt come to solve it right now, can you share me the code for the method you use dr really i spent a lot of time in this job. thank you for your help dr really i wold be grateful

0

I did reveal the method. You count edges in areas. We used a grid with a Sobel filter. When the edge counts change across a threshold (your choice but here we used a configuration file to set this) then it's movement. By comparing pixels you will get nothing but false alarms.

But as I wrote, many new to this area have to try pixel comparing then they learn it's a dead end. Some have burned down over that and insist it's the way. My view? Let them burn as it is a way to get better programmers.

0

dr really im not specialist in image processing can you share me the code via email :). thank you

0

dr sincerly i have no idea about image processing, filters... , we will use openCv or what ?

0

there a work done with C#, i search a lot dr before asking, and i found the code i told you above but unfortunately itruns an exception. i can not return from the begining and there are a lot of things to do yet, can you help me to solve this memory exeption dr

0

Sorry but I was under the impression you were going to create a motion detection system or app. Maybe there are forums that write code for folk but for me, I will share concepts from real world systems we sold. That's gold for most but maybe you need to just bite the bullet and just find a working app and use it?

That is, it's beginning to sound like you need complete working apps rather than learn to code and create something new.

0

hi dr,
at this step i need just a sort of application that validate my project, may be later i should developp robust motion detection app. i wish that you understood me, im in a step if i succeed it it , i would enter the research field and i will have to and i will be able to reseach for new strategies and methodologies.

Edited by yosri_1

0

the problem is on the time assigned for the project, my thesis will be on july

2

my thesis will be on july

I have no advice to offer regarding OpenCV, but I'll offer some generic advice. If you're writing a graduate/professional level thesis, that's usually a huge project, generally far too big for an online forum where the posters are anonymous and the advice is free. Your only hope is to get much more specific much earlier in your threads, describing precisely what you are doing and what you need, as well as your experience level. If you are writing a thesis, one assumes you are a graduate student or a researcher or a professional.

Right now you are not coming across as a graduate student or a researcher or a professional IMO from a communication standpoint, and I'm not referring to you not being a native English speaker here. I'm referring to your lack of code formatting/indentation, lack of capitalization and punctuation, not using a spellchecker, and your basic approach. Approach your interactions on this forum as you would approach the people reviewing your thesis. Programming, particularly embedded programming at the "thesis" level, requires attention to detail and I think you'll be more successful getting answers if you write a succinct, proofwritten initial post stating your objective, experience level, and where you are stuck in complete sentences. Right now the writing is overly casual and the details trickle out in follow-up posts and we have to guess what you want.

0

Since it is a thesis I don't mind sharing work we did before but you have to do all the work. Anyhow, given it's a thesis, besides what others have written here I wonder what your thesis is? Share that so all will know.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.