Ask Your Question
0

opencv video analysing

asked 2015-02-24 10:57:25 -0600

i grab video from webcam and i need some alert when frames in my video become completely black . can anyone halpe me ?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2015-02-24 16:43:04 -0600

matman gravatar image

updated 2015-02-24 16:54:50 -0600

You can threshold your image with a relative low threshold and than count the non zero pixels, which should be under a threshold because of bad pixels and camera noise. Example:

threshold(imgIn, imgOut, 5, 255, THRESH_BINARY);
if(countNonZero(imgOut) < maxNonZeroPixelsInBlackImage)
    cout << "Image is a black one!" << endl;

Or you make an inverse threshold, than findContours and if the area of your found contour is nearly as big as the image area it is a black one. Remember noise and bad pixels so it must be a bit smaller

vector<Mat> Contours;
threshold(imgIn, imgOut, 5, 255, THRESH_BINARY_INV);
findContours(imgOut, Contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));
for(int i = 0; i < Contours.size(); i++) {
    if(contourArea(Contours[i]) > imgIn.total()*0.99) {
        cout << "Image is a black one!" << endl;
        break;
    }
}

The first one should be the easier method, the second one should be more flexible, if just a part of the image has to be black or if you search a special area it can be adjusted. Check out: contours in OpenCV

edit flag offensive delete link more

Comments

i guess you are using C++ , how can i transform it in Python ?

mad_head gravatar imagemad_head ( 2015-02-25 05:43:39 -0600 )edit

I'm not familiar with Python so I think its something like

ret, imgOut = cv2.threshold(imgIn, 5, 255, cv2.THRESH_BINARY) 
if cv2.countNonZero(imgOut) < maxNonZeroPixelsInBlackImage
    print "Image is a black one"

and this

ret, imgOut = cv2.threshold(imgIn, 5, 255, cv.THRESH_BINARY_INV)
contours, hierarchy = cv2.findContours(imgOut, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for i in range(0, contours.size)
    if cv2.contourArea(contours[i]) > imgIn.size*0.99
        print "Image is a black one"

but I can not give any guaranty that this code is right, I didn't tested it. Check out: contours

matman gravatar imagematman ( 2015-02-26 13:13:41 -0600 )edit
0

answered 2015-02-24 20:03:01 -0600

unxnut gravatar image

You could use minMaxLoc on the frame and then check if the max value is 0. If so, you could issue the alert.

edit flag offensive delete link more

Comments

can you tell me a little bit more , i'm nooby about it

mad_head gravatar imagemad_head ( 2015-02-25 04:37:55 -0600 )edit

I can not recommend to check with minMaxLoc(), because only on pixel with a value greater than 0 makes your test absurd even if the other of pixels are all zero an because of camera noise it is probable that you have at least one pixel not equal to zero. So if you have synthetic images it will work, but not in real scenarios.

matman gravatar imagematman ( 2015-02-26 12:48:20 -0600 )edit

Question Tools

Stats

Asked: 2015-02-24 10:57:25 -0600

Seen: 86 times

Last updated: Feb 24 '15