Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Using Image processing toolbox on MATLAB.

  1. Oct 14, 2011 #1

    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?
  2. jcsd
  3. Oct 14, 2011 #2
    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..

    please suggest...I'm in soup!!
  4. Oct 17, 2011 #3
    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...
  5. Oct 17, 2011 #4
    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

    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..
  6. Oct 17, 2011 #5
    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?

    Attached Files:

  7. Oct 17, 2011 #6
    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.
  8. Oct 19, 2011 #7
    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'm completely new to this...please help.
  9. Oct 19, 2011 #8
    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: Oct 19, 2011
  10. Oct 20, 2011 #9
    To answer 2.) first:

    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.
  11. Oct 21, 2011 #10
    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..

    So I'm still stuck with the first part of your answer....
  12. Oct 21, 2011 #11
    I just did it using imcontrast function....it worked!!....now I'll try working with the second part of your answer.
  13. Oct 22, 2011 #12
    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?


    This is the for loop:

    for ii= 1:N
  14. Oct 22, 2011 #13
    Note that this is supposed to be a modification of the following code that does work...

    for ii= 1:N
  15. Oct 22, 2011 #14
    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?
  16. Nov 1, 2011 #15
    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?
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook