0

My project is recognizing faces from images. The images have the same name and change occasionally
So i want to load and process the images every time they change
how can i modify this:

    while (image==NULL) {
    image = cvLoadImage("ayman.jpg", 1);
}
cout << endl << "My image was finally loaded!";    

sorry for my english

Borgleader
  • 15,826
  • 5
  • 46
  • 62

1 Answers1

0

Ok, let's assume than your images are different at the moment they change (random). How do we detect that it's another image, different from the previous one?

We are going to extract 3 features of your image and it is: the mean of the RED CHANNEL, GREEN CHANNEL and BLUE CHANNEL (it's a vector). So, if the image is the same, the 3 means are the same, but if it's different, the image has been changed.

So think we are in a infinite loop (your while).

while(true) {
    // we are going to say here if the image has changed or not
}

Are we ok ? So this is the code:

    image = cvLoadImage("ayman.jpg", 1);
    Scalar meanOfImage = mean(image);

    while (true) {
        Scalar meanAtThisMoment = mean(image);
        // This are the three features (it's in real one, but one vector of 3).
        // We are going to compare the three to be more clear.
        if (meanAtThisMoment[0] == meanOfImage[0]
            &&
            meanAtThisMoment[1] == meanOfImage[1]
            &&
            meanAtThisMoment[2] == meanOfImage[2]) {

            cout << endl << "The image hasn't been changed yet.";
            image = cvLoadImage("ayman.jpg", 1); // added!! forgot this, sorry
        } else {

            cout << endl << "The image has been changed!!!";
            // and now we set the meanOfImage (the main mean) of the new image to compare with the future images.

            meanOfImage = meanAtThisMoment;



        }

    }
Rafael Ruiz Muñoz
  • 5,333
  • 6
  • 46
  • 92
  • sorry mate, I forgot to add one line!!! I commented as " // added!! forgot this, sorry "... please try and tell me if it worked ;) – Rafael Ruiz Muñoz May 14 '15 at 17:56
  • thanks Rafa i changed the image but the Xterm keep telling me that "The image hasn't been changed yet." – badri ayman May 14 '15 at 18:21
  • consider to remove the first cout (the sentence: `cout << endl << "The image hasn't been changed yet."` and so you will get notified ONLY when it's been changed ;). If it was ok you can accept it as correct answer :) – Rafael Ruiz Muñoz May 14 '15 at 18:30
  • the image change only if i close the image window but thats ok!! thank you :) – badri ayman May 14 '15 at 18:39
  • I guess the image changes in the memory when you close it, that's why. Try to save it with the same name, it will change as well :). – Rafael Ruiz Muñoz May 14 '15 at 18:48
  • sorry for disturbing Rafa but this is functional only with Mat images . my input images are iplimage* . any idea for this ? (i'm a electrical engineer student not a programmer :/ ) – badri ayman May 15 '15 at 11:44
  • I would highly reccommend you to use Mat since `IplImage` is used in older versions. If still it doesn't work, you can convert from IplImage to Mat, for example by this link http://stackoverflow.com/questions/15925084/conversion-from-iplimage-to-cvmat – Rafael Ruiz Muñoz May 15 '15 at 11:49
  • i found another solution : AFTER THIS: while (image==NULL) { image = cvLoadImage("ayman.jpg", 1); } cout << endl << "My image was finally loaded!"; I ADDED while(!image.empty()){ image= cvLoadImage("css.jpg", 1); thats worked perfectly :D – badri ayman May 15 '15 at 13:53