# Using Image processing toolbox on MATLAB.

• MATLAB
Hi,

I have a movie that contains a particle moving from one end of a channel to another end. The velocity of the particle is super-fast. I need to know how I can estimate the time and pictorially (like using a histogram--similar to plotting the heart pulses of a person) be able to tell when the particle is at each of the ends....I don't care too much about the intermediate path.

I have started using the image processing toolbox on matlab...I'm thinking that my steps will be-
1. to view the movie on movie player (on the image processing toolbox)
2. split the video into individual frames.
3.attach a histogram to each frame, to tell (due to difference in light intensity in the area where the particle is located) in which part of the frame the particle is.
4. now I can tell the time difference between the two frames in which I can see the particle is at each end.

So does anyone have a better idea on how I can achieve my goal?
What functions will I have to use?

Related MATLAB, Maple, Mathematica, LaTeX News on Phys.org
Someone told me that for each frame I need to calculate where the spot is,by picking wherever the brightest pixel is or by frame to frame subtracting, calculating means & standard deviations, thresholding, etc...which one would you recommend for me, especially since I'm a beginner in this...I can't code for something too difficult.

After that, I need to calculate the velocity of the particle... by seeing the number of pixels it moves divided by the amount of time between each frame or by using kalman filters.

I've heard about kalman filters....but how do I employ this? I need an easy code...nothing too complex..

Is the background of your movie constant (sounds like it would be). If so, the first thing you want to do is average all of the frames and subtract that average from each frame. This will leave you with just the particle in each frame.

From there it should be pretty easy to figure out which ones are blank and remove them. The first frame you are left with is when the particle entered the video and the last frame you are left with is when the particle left the video.

If you background isn't constant then identifying the particle from things moving in the background becomes much more difficult...

I've tried different videos, and all of my avi videos are working on movieplayer (MATLAB), except one...which is the one that I need.
Before I proceed to follow Floid's instructions and analyse the images, it is important that I be able to split this video into its frames...

I can't bring the video on the workspace, and aviread cannot read it...

I can, however play it using
implay('cell.avi')

But now, I need to separate out its frames (maybe using image function?)...please suggest a code for this..

Apparently, my video 'cell.avi' is a 8 character long string..

Hey, I managed to convert the file atlast!!

Floid, here's the frame...if u see carefully, you will be able to locate a time cell at the bottom left hand corner of the frame....

Please tell me what u would suggest for me to do..as u can see, the cell isn't easy to make out from its background..

How would I write the code to subtract the background from the image?

#### Attachments

• 6.8 KB Views: 446
Take a frame that you know does not have a cell in it as your "background" image. Now subtract the background image from every frame of the video. This will in effect remove the background from the video and all will you be left with is a video of the cell.

Actual implementation you will have to figure out as I have never tried it, but I know that is how you seperate foreground objects from background objects (as long as the background is fairly constant). You can do it pixel by pixel but if you have Matlab Image Processing Toolbox there probably is a built in function to do it.

But I just realized that I also need some way to measure the displacement of the cell...is it possible to select a point and measure the pixel displacement or something?...How about using control points?

I managed to subtract the frames and I'm getting my desired result....now can you suggest how I can-
1. measure how much the cell has moved from one point of time to another.
(it doesn't even show coordinate axes)

2. how can I make the image of the moving cell more clear...its a faint white blob moving from frame to frame....

Last edited:

Are you worried about clarity/accuracy of what you are looking at?

If all you want to do is make the cell location more visible but don't care about what the cell itself actually looks like the easiest way will be to change the image to black/white. The basic idea is that all colors below some level will be converted to white (your background) and all colors above some level (your cell) will be converted to black. What you will end up with is a white background with a black blob that is your cell. This will make step 1 easier if it is acceptable.

Look into the MATLAB image processing function dither. http://www.mathworks.com/help/techdoc/ref/dither.html

It didn't look like it had a threshold level you could set for white and black, so you may have to write a loop that goes through every pixel of the image and makes a decision to change it to black or white.

To question 1.)

You need to know a few things to measure distance travelled. The first is the frame rate of the video (needed to get time). This should be easy to determine.

The next part is an idea of distance and can be a little harder. One way is to have a scale, which may be what that rectangular thing in the back of your image is... even if it isn't you can use it as one. Ideally you can establish the size of one pixel. One way to do this is that if you know the actual size of that rectangular thing is to count the pixels between those 45 degreeish slanted boxes and then measure how far it is actually between them. You can use this to establish the size of a pixel.

Now, all you have to do is take two images that both feature the cell and measure the distance between them in pixels which will give you distance and take the difference in frame numbers divided by the frame rate (in Hz) to give you time.

How to find the cell itself? If you convert to black and white like in 2.) then search through all the pixels of the image and find the left most or right most black pixel and this should give you an edge of the cell and then do the same search and find the top most or bottom most black pixel and this will give you the top or bottom of the cell.

Do the same measurements between different frames and you can get the displacement in pixels of the cell.

Hi Floid, thanks for your help!!

I subtracted the first frame as u said, and it left me with only the cell moving...but it was very faint against the background..

Then I tried dither as u said...but unfortunately, there must be other point with the same intensity as the cell, since I get multiple indistinguishable white spots...

I also tried imclear, but again, due to the fact that there must be other spots indistinguishable from the cell, the result was never satisfactory..

Also, the frame, after subtraction of the first frame, consists of a black background with a faint white blob, which is the cell.....and even after converting it to grayscale (using rgbgray), doesn't make any difference in the output..

I just did it using imcontrast function....it worked!!....now I'll try working with the second part of your answer.

I've done a large part of Floid said in the second part of his answer...I'm calculating the displacement of the cell by putting all the frames into a matrix...the cell is the brightest pixel on each frame...I'm therefore calculating the difference of the x,y coordinates of the brightest spot on each frame...any idea what's wrong with my for loop?

It says ERROR IN USING - (MINUS SIGN)...MATRIX DIMENSIONS MUST MATCH.

This is the for loop:

Bii=B1{ii};
for ii= 1:N
Bii=B1{ii};
[x,y]=find(B1{ii}==max(max(B1{ii})));
[x,y]=find(B1{ii}==max(max(B1{ii})))
end

Note that this is supposed to be a modification of the following code that does work...

Bii=B1{ii};
for ii= 1:N
Bii=B1{ii};
[x,y]=find(B1{ii}==max(max(B1{ii})));
[x,y]=find(B1{ii}==max(max(B1{ii})))
end

I discovered that its because there are multiple points with the same (maximum) intensity...so I guess I can't track the cell on the basis of brightness... any idea how else/what other basis I could identify the cell's position?

All the preliminary points having been solved, I have a new problem!!

I have some new videos in which again, a cell moves along a channel...however, the background i.e. the channel is near the brightness of that of my cell, so when I convert the video into images of its frames, and turn them into grayscale images, I cannot isolate the cell from its background very clearly, though I can faintly see it move against the background.

I tried frame subtraction..after subtracting the first frame that does not contain the cell from the remaining, I still get a lot of background noise. *

Thus, when I try locating the cell by trying to identify the maximum brightness spot, *I get the locations of different other points with the greater brightness than the cell.

Can you suggest what I can do?