Get the average of points

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • David24
    New Member
    • Jan 2008
    • 13

    Get the average of points

    i am modifying a simple motion detector from the Aforge Library. It simply compares 2 frames and draws the difference as semitransparent red pixels. I need to store the (constantly changing) average location of these red pixels (representing motion) as int tartgetX and int targetY. Here is the code:

    Code:
     public unsafe void ProcessFrame(Bitmap image)
            {
                BitmapData imageData = null;
    
                // check previous frame
                if (previousFrame == IntPtr.Zero)
                {
                    // save image dimension
                    width = image.Width;
                    height = image.Height;
                    frameSize = width * height;
    
                    // alocate memory for previous and current frames
                    previousFrame = Marshal.AllocHGlobal(frameSize);
                    currentFrame = Marshal.AllocHGlobal(frameSize);
                    // temporary buffer
                    if (suppressNoise)
                    {
                        tempFrame = Marshal.AllocHGlobal(frameSize);
                    }
    
                    // lock source image
                    imageData = image.LockBits(
                        new Rectangle(0, 0, width, height),
                        ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
    
                    // convert source frame to grayscale
                    ImageProcessingTools.GrayscaleImage(imageData, previousFrame);
    
                    // unlock source image
                    image.UnlockBits(imageData);
    
                    return;
                }
    
                // check image dimension
                if ((image.Width != width) || (image.Height != height))
                    return;
    
                // lock source image
                imageData = image.LockBits(
                    new Rectangle(0, 0, width, height),
                    (highlightMotionRegions) ? ImageLockMode.ReadWrite : ImageLockMode.ReadOnly,
                    PixelFormat.Format24bppRgb);
    
                // convert current image to grayscale
                ImageProcessingTools.GrayscaleImage(imageData, currentFrame);
    
                // pointers to previous and current frames
                byte* prevFrame = (byte*)previousFrame.ToPointer();
                byte* currFrame = (byte*)currentFrame.ToPointer();
                // difference value
                int diff;
    
                // 1 - get difference between frames
                // 2 - threshold the difference
                // 3 - copy current frame to previous frame
                for (int i = 0; i < frameSize; i++, prevFrame++, currFrame++)
                {
                    // difference
                    diff = (int)*currFrame - (int)*prevFrame;
                    // copy current frame to previous
                    *prevFrame = *currFrame;
                    // threshold
                    *currFrame = ((diff >= differenceThreshold) || (diff <= differenceThresholdNeg)) ? (byte)255 : (byte)0;
                }
    
                // calculate amount of motion pixels
                pixelsChanged = 0;
    
                if (suppressNoise)
                {
                    // suppress noise and calculate motion amount
                    AForge.Win32.memcpy(tempFrame, currentFrame, frameSize);
    
                    byte* motion = (byte*)currentFrame.ToPointer() + width + 1;
                    byte* temp = (byte*)tempFrame.ToPointer() + width + 1;
    
                    int widthM1 = width - 1;
                    int heightM1 = height - 1;
    
                    // erosion is used to suppress noise
                    for (int y = 1; y < heightM1; y++)
                    {
                        for (int x = 1; x < widthM1; x++, motion++, temp++)
                        {
                            // check if it is motion pixel
                            if (*motion != 0)
                            {
                                *motion = (byte)(temp[-width - 1] & temp[-width] & temp[-width + 1] &
                                    temp[width - 1] & temp[width] & temp[width + 1] &
                                    temp[1] & temp[-1]);
    
                                pixelsChanged += (*motion & 1);
                            }
                        }
                        motion += 2;
                        temp += 2;
                    }
                }
                else
                {
                    // calculate motion without suppressing noise
                    byte* motion = (byte*)currentFrame.ToPointer();
    
                    for (int i = 0; i < frameSize; i++, motion++)
                    {
                        pixelsChanged += (*motion & 1);
                    }
                }
    
                // highlight motion regions
                if (highlightMotionRegions)
                {
                    byte* src = (byte*)imageData.Scan0.ToPointer();
                    byte* motion = (byte*)currentFrame.ToPointer();
                    int srcOffset = imageData.Stride - width * 3;
    
                    // shift to the red channel
                    src += 2;
    
                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++, motion++, src += 3)
                        {
                            *src |= *motion;
                        }
                        src += srcOffset;
                    }
                }
    
                // unlock source image
                image.UnlockBits(imageData);
            }
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    I seem to have missed the question there...

    Comment

    • David24
      New Member
      • Jan 2008
      • 13

      #3
      Originally posted by sicarie
      I seem to have missed the question there...
      here, i'll repeat it for you; i need to get the average of points and save them as integers targetx and targety. when i say points i'm talking about the red pixels this code draws as a representation of the difference between 2 frames it compares. if, after reading this and the first post a few times you still don't understand, you are likely not qualified to solve the problem anyway...

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Haha, I can understand, and read, but you didn't ask a question (again). Did you want me to just do the whole thing for you? When I consulted, I billed at $150/hr, so we can start there, eh?

        Or did you have a question about your implementation, something you weren't understanding so I can help point you in the right direction?

        Comment

        • David24
          New Member
          • Jan 2008
          • 13

          #5
          Originally posted by sicarie
          Haha, I can understand, and read, but you didn't ask a question (again). Did you want me to just do the whole thing for you? When I consulted, I billed at $150/hr, so we can start there, eh?

          Or did you have a question about your implementation, something you weren't understanding so I can help point you in the right direction?
          ok, here's my question; how would i store the red pixels this motion detector draws as a list of points so i can then average their location? this code runs as unsafe so that it can run very fast and uses address and indirection operators which i vaguely understand..

          this bit of code is hard to comprehend. i've never seen an address operator used like this (*motion & 1). i don't understand what the code is doing here

          Code:
          // calculate motion without suppressing noise
                          byte* motion = (byte*)currentFrame.ToPointer();
          
                          for (int i = 0; i < frameSize; i++, motion++)
                          {
                              pixelsChanged += (*motion & 1);
                          }
          i don't understand this either
          Code:
          *src |= *motion;
          i can't find any good examples of what The OR assignment operator does except that it performs a bitwise logical OR operation on integral operands which doesn't help

          Comment

          Working...