By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,526 Members | 2,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,526 IT Pros & Developers. It's quick & easy.

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

P: 13
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)

Expand|Select|Wrap|Line Numbers
  1. else
  2.             {
  3.                 // calculate motion without suppressing noise
  4.                 byte* motion = (byte*)currentFrame.ToPointer();
  5.  
  6.                 for (int i = 0; i < frameSize; i++, motion++)
  7.                 {
  8.                     pixelsChanged += (*motion & 1);//compares the last bit of the motion value if it was set to one.
  9.                 }
  10.             }
  11.  
  12.             // highlight motion regions
  13.             if (highlightMotionRegions)
  14.             {
  15.                 byte* src = (byte*)imageData.Scan0.ToPointer();
  16.                 byte* motion = (byte*)currentFrame.ToPointer();
  17.                 int srcOffset = imageData.Stride - width * 3;
  18.  
  19.                 // shift to the red channel
  20.                 src += 2;
  21.  
  22.                 int coordinateCount = 0;
  23.  
  24.                 for (int y = 0; y < height; y++) // do this 480 times
  25.                 {
  26.                     for (int x = 0; x < width; x++, motion++, src += 3)//do this 640 times
  27.                     {
  28.                         *src |= *motion; //the address of source or motion = the address of source
  29.  
  30.                         //i added this. if this is a motion pixel, store it's coordinates
  31.                         if (*motion > 0)
  32.                         {
  33.                             //x is guide to what pixel is being tested so store it as a coordinate
  34.                             xCoord[coordinateCount] = x;
  35.                             //the same with y
  36.                             yCoord[coordinateCount] = y;
  37.                             //increase the array number so the next values aren't written over the first
  38.                             coordinateCount++;                           
  39.                         }
  40.  
  41.                     }
  42.                    src += srcOffset;//moves the pointer to the next adress where to get the next data.
  43.                 }
  44.  
  45.                 //i added this to get the average value of the ints in the x and y arrays
  46.                 int sumX = 0;
  47.                 for (int i = 0; i < coordinateCount; i++)
  48.                 {
  49.                     sumX += xCoord[i];
  50.                 }
  51.                 int targetX = sumX / coordinateCount;
  52.  
  53.                 int sumY = 0;
  54.                 for (int i = 0; i < coordinateCount; i++)
  55.                 {
  56.                     sumY += yCoord[i];
  57.                 }
  58.                 int targetY = sumY / coordinateCount;
  59.             }
  60.  
  61.             // unlock source image
  62.             image.UnlockBits(imageData);
Feb 23 '08 #1
Share this Question
Share on Google+
1 Reply


kenobewan
Expert 2.5K+
P: 4,871
Would be easier if you paste the error and give the line number. Thanks.
Feb 24 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.