I'm trying to write a code The helps me in my biology work. Concept of code is to analyze a video file of contracting cells in a tissue

Example 1

Example 2: youtube.com/watch?v=uG_WOdGw6Rk

And plot out the following:

  1. Count of beats per min.
  2. Strenght of Beat
  3. Regularity of beating

And so i wrote a Matlab code that would loop through a video and compare each frame vs the one that follow it, and see if there was any changes in frames and plot these changes on a curve.

Example of My code Results enter image description here

Core of Current code i wrote:

for i=2:totalframes
        ref=rgb2gray(compared);%% convert to gray
        level=graythresh(ref);%% calculate threshold
        compared=im2bw(compared,level);%% convert to binary        
        differ=sum(sum(imabsdiff(vid,compared))); %% get sum of difference between 2 frames
        if (differ ~=0) && (any(amp==differ)==0) %%0 is = no change happened so i dont wana record that !
            amp(end+1)=differ;  % save difference to array amp wi
            time(end+1)=i/framerate; %save to time array with sec's, used another array so i can filter both later.
            vid=compared; %% save current frame as refrence to compare the next frame against.


So thats my code, but is there a way i can improve it so i can get better results ?

because i get fealing that imabsdiff is not exactly what i should use because my video contain alot of noise and that affect my results alot, and i think all my amp data is actually faked !

Also i actually can only extract beating rate out of this, by counting peaks, but how can i improve my code to be able to get all required data out of it ??

thanks also really appreciate your help, this is a small portion of code, if u need more info please let me know. thanks

  • $\begingroup$ How would you visually assess the Strength of Beat? By the distance the tissue moves? $\endgroup$
    – endolith
    Jan 4, 2016 at 4:58
  • $\begingroup$ not exactly, although its a 2 years old work, i remember the idea was to convert frame to black and white, since tissue color become black and background is white, you sum all pixels 'black = 1, white = 0' and compare difference, more black means tissue is relaxed 'higher value' more white means tissue is contracted 'result in lower value'.i also remember before i come up with this i tried identifying borders of tissue to compare size; yet that was a waste of time and effort, was never accurate $\endgroup$ Jan 4, 2016 at 7:13
  • $\begingroup$ Yeah thresholding seems like a crude method. Tracking features in the image and measuring when and how far they move should work better $\endgroup$
    – endolith
    Jan 4, 2016 at 7:23

1 Answer 1


You could try a simple non-linear denoising filter (3x3 or 5x5 median filter) on your image sequence to make your detection more robust to noise ; but I get the feeling that the moving details on these images or quite thin and that too much filtering would harm your detection. And it looks like you are already thresholding the images before comparison, so the only noise that matters here are pixels moving above and below the threshold at each frame.

To get a beating rate you could also compute the auto-correlation of the sequence. It will show a secondary maximum at the beating rate. The autocorrelation is actually an interesting representation for your task because:

  • A very regular beat will mean a very sharp secondary peak on the AC sequence ; while a more irregular beat will show a flattened, spread out secondary peak in the AC sequence.
  • The ratio of the autocorrelation secondary peak to the first peak ($r_0$) can be used as a good measure of beat strength.

But back in time domain you could also consider:

  • The kurtosis of the sequence as a measure of "peakedness" (and thus beat strength). Other measures of peakedness include the ratio of geometric mean to arithmetic mean.
  • The standard deviation of the time intervals between peaks as a measure of beat regularity. The downside is that you will have to detect peaks, which will be a bit imprecise in your case especially given the low temporal resolution of your data (and this is why I thought about doing things from the AC function first!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.