opencv video analysing
i grab video from webcam and i need some alert when frames in my video become completely black . can anyone halpe me ?
i grab video from webcam and i need some alert when frames in my video become completely black . can anyone halpe me ?
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
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
You could use minMaxLoc
on the frame and then check if the max value is 0. If so, you could issue the alert.
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.
Asked: 2015-02-24 10:57:25 -0600
Seen: 86 times
Last updated: Feb 24 '15
Area of a single pixel object in OpenCV
build problems for android_binary_package - Eclipse Indigo, Ubuntu 12.04
OpenCV DescriptorMatcher matches
Can't compile .cu file when including opencv.hpp
Using OpenCV's stitching module, strange error when compositing images
compile error in opencv2/flann/lsh_table.h when compiling bgslibrary