How to compare frames of a video in MATLAB by histogram level?

I have written code that stores a video as an array of frames, however now I want to compare the individual frames via their histogram levels and return a message when a significant difference is detected between frames, how would I go about doing this?

Réponses (1)

Sudden changes in the histogram can indicate things in a video, like abrupt scene changes. However you may not need to get the whole histogram, you might be able to just use mean2 on the whole frame
grayImage = rgb2gray(thisFrame);
meanGrayLevel = mean2(grayImage);
However mean gray level, and even the complete histogram is not a measure of how alike the images are. For example, take any image. Now cut the intensity in half. It still "looks" a lot like the original image, even though its histogram is drastically shifted. Now take the original image and sort the gray levels so that the image looks like a ramp. That image would look nothing like the original image even though their histograms are 100% identical (because the same pixels are there, they're just moved around to new locations).
It might help if you said what you were really after so we could help with the proper algorithm.

9 commentaires

Essentially I'm trying to detect whether a person has blinked in a video sequence. I know how to record a video with a selected region of interest that would narrow down the search however I then need to apply the same principle to live video.
People have already done this. Check out http://www.visionbib.com/bibliography/motion-f725.html
16.7.2.6.1 Driver Monitoring, Eyes, Gaze
for papers showing how they do it.
Thanks but I have looked at this before and while they make good background reading they're not relevant to MATLAB...
They're relevant to the extend that you can take the algorithm and code it up in MATLAB. This is because it's such a niche application that there is nothing built into MATLAB for doing exactly that so you must write your own. So which paper did you code up, and do you need help with your code?
I didn't code up any of the papers - I have code which I've written that detects and encircles the eyes in an image and now I'm trying to transfer this (and the histogram comparison) into code for pre-recorded and live video. I know which commands to use -
imfindcircles, imhist
etc. I just don't know how to loop them round to work continually. Any help would be greatly appreciated.
You call getsnapshot() to pull a frame from teh live video. Then after you do that you call your functions. getsnapshot will be in a loop for as long as you want to run it. For pre-recorded video it will most likely be a loop for every frame in the video. For a live video you will loop until something happens, like a predetermined amount of time has elapsed or the user clicks something on the GUI or however you want to stop it.
This is what I have at the moment for the pre-recorded video; the frames are stored in a 3D array:
vid=VideoReader('EyeVid2.avi');
j=1;
while hasFrame(vid)
A=readFrame(vid);
B(:,:,j)=A(:,:,1);
j=j+1;
end
However I haven't been able to implement the code you sent above to be able to retrieve the histogram level of each frame.
No need to create an image stack with B as far as I can tell, simply do this
while hasFrame(vid)
thisFrame = readFrame(vid); % Get one RGB frame of the color video.
redChannel = thisFrame(:,:,1); % Extract red channel only.
[pixelCounts, grayLevels] = imhist(redChannel); % Get histogram of red channel.
% Now do something with pixelCounts, grayLevels, and redChannel....
end
I've added this line
stem(grayLevels, pixelCounts);
after getting the histogram of the red channel however I'm not sure what to do with 'redChannel'... The above produced a live stream histogram but I'm not sure how to do a frame-by-frame comparison. Why have you just extracted the redChannel?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Image Processing and Computer Vision dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by