unsafe code i modified isn't working quite right.. (C#)

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

    #1

    unsafe code i modified isn't working quite right.. (C#)

    my best attempt to get the following code to store 2 ints representing the average location of x and y coordinates is throwing NullExeption errors. this is unsafe C# code by the way. i'm a novice so hopefully you will notice some obvious, glaring, simple solution for this. it's very important to me that i get this working. it would be a better motion detector than the one i currently use on my motion tracking turret. (although the old detector does the job well enough, it uses a blobcounting (from the Aforge library) algorithm that just doesn't act right)

    Code:
    else
                {
                    // calculate motion without suppressing noise
                    byte* motion = (byte*)currentFrame.ToPointer();
    
                    for (int i = 0; i < frameSize; i++, motion++)
                    {
                        pixelsChanged += (*motion & 1);//compares the last bit of the motion value if it was set to one.
                    }
                }
    
                // 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;
    
                    int coordinateCount = 0;
    
                    for (int y = 0; y < height; y++) // do this 480 times
                    {
                        for (int x = 0; x < width; x++, motion++, src += 3)//do this 640 times
                        {
                            *src |= *motion; //the address of source or motion = the address of source
    
                            //i added this. if this is a motion pixel, store it's coordinates
                            if (*motion > 0)
                            {
                                //x is guide to what pixel is being tested so store it as a coordinate
                                xCoord[coordinateCount] = x;
                                //the same with y
                                yCoord[coordinateCount] = y;
                                //increase the array number so the next values aren't written over the first
                                coordinateCount++;                           
                            }
    
                        }
                       src += srcOffset;//moves the pointer to the next adress where to get the next data.
                    }
    
                    //i added this to get the average value of the ints in the x and y arrays
                    int sumX = 0;
                    for (int i = 0; i < coordinateCount; i++)
                    {
                        sumX += xCoord[i];
                    }
                    int targetX = sumX / coordinateCount;
    
                    int sumY = 0;
                    for (int i = 0; i < coordinateCount; i++)
                    {
                        sumY += yCoord[i];
                    }
                    int targetY = sumY / coordinateCount;
                }
    
                // unlock source image
                image.UnlockBits(imageData);
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Would be easier if you paste the error and give the line number. Thanks.

    Comment

    Working...