Ask Your Question
0

Static video

asked 2018-09-20 17:28:55 -0500

zulugula gravatar image

updated 2018-09-21 01:06:14 -0500

Hi, I want to check if recieved image is freezed, eg. ip camera is sending the same frame all the time. I decided to simulate freeze with test avi video like this: https://www.youtube.com/watch?v=woXNR... , theoretically difference between two cv::mat frames (with 4 sec. interval) should be zero. But it isn't. Does anybody can help me?

Here is code:

cv::Mat frame1, frame2;
cv::VideoCapture cap;
cap.open("myTestVid.avi");
cap.read(frame1);
sleep(3);
cap.read(frame2);
compareFrames(frame1, frame2);

And here is compareFrames method:

void CompareWorker::compareFrames(Mat frame1, Mat frame2)
{
//    std::cout << "compare frames";
    QImage firstImage((const unsigned char *)(frame1.data), frame1.cols, frame1.rows, QImage::Format_RGB888);
    QImage secondImage((const unsigned char*)(frame2.data), frame2.cols, frame2.rows, QImage::Format_RGB888);


    double totaldiff = 0.0;
    double diffLevelVal = 0.0 ;
    if((frame1.rows == frame2.rows) && (frame1.cols == frame2.cols)){
        for ( int y = 0 ; y < firstImage.height() ; y++ ) {
            //odczytywanie linii obrazków
            uint *firstLine = ( uint* )firstImage.scanLine( y ) ;
            uint *secondLine = ( uint* )secondImage.scanLine( y ) ;
            for ( int x = 0 ; x < firstImage.width() ; x++ ) {
                uint pixelFirst = firstLine[ x ] ;
                int rFirst = qRed( pixelFir ) ;
                int gFirst = qGreen( pixelFir ) ;
                int bFirst = qBlue( pixelFir ) ;

                uint pixelSecond = secondLine[ x ] ;
                int rSecond = qRed( pixelSec ) ;
                int gSecond = qGreen( pixelSec ) ;
                int bSecond = qBlue( pixelSec ) ;
                double rDiff = std::abs(rFirst - rSec)/255.0;
                totaldiff+=rDiff;
                double gDiff = std::abs(gFirst - gSec)/255.0;
                totaldiff+=gDiff;
                double bDiff = std::abs(bFirst - bSec)/255.0;
                totaldiff+=bDiff;
            }
        }

    }
    diffLevelVal = ((totaldiff * 100)  / (firstImage.width() * firstImage.height() * 3));

    emit returnDiffVal(diffLevelVal);
}
edit retag flag offensive close merge delete

Comments

1

How are you comparing them? Are you using absdiff? We need your code.

sjhalayka gravatar imagesjhalayka ( 2018-09-20 20:19:23 -0500 )edit

Hello everyone, I'm new, I have the same doubt or problem. Greetings.

ucb38 gravatar imageucb38 ( 2018-10-03 07:41:48 -0500 )edit

@ucb38, please do not post answers here, if you have a question or comment, thank you.

berak gravatar imageberak ( 2018-10-03 07:48:55 -0500 )edit

2 answers

Sort by » oldest newest most voted
2

answered 2018-09-21 01:10:12 -0500

berak gravatar image

updated 2018-10-02 08:08:11 -0500

theoretically difference between two cv::mat frames (with 4 sec. interval) should be zero

no, that will almost NEVER happen.

there will always be noise, and ipcams usually send (lossy) jpg images, so you also have compression artefacts. you can't bet on it being exactly zero. (you'll be amazed, how large it is in reality)

then, -- please do NOT write for loops here (or even use Qt images), use existing opencv functions like absdiff() and sum() (and find a nice threshold value for the decision)

also: https://docs.opencv.org/master/d5/dc4...

so in short:

double CompareWorker::compareFrames(Mat frame1, Mat frame2) {
    Mat diff;
    cv::absdiff(frame1, frame2, diff);
    Scalar s = cv::sum(diff);
    return s[0] + s[1] + s[2];
}
edit flag offensive delete link more
0

answered 2018-10-02 07:32:59 -0500

zulugula gravatar image

Thanks for reply. That's beacuse my teacher says diff must be zero, so when i got value other than zero I got confused. I decided to try opencv functions with binary treshold, and now works fine.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-09-20 17:28:55 -0500

Seen: 192 times

Last updated: Oct 02 '18