473,385 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Get the average of points

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

Expand|Select|Wrap|Line Numbers
  1.  public unsafe void ProcessFrame(Bitmap image)
  2.         {
  3.             BitmapData imageData = null;
  4.  
  5.             // check previous frame
  6.             if (previousFrame == IntPtr.Zero)
  7.             {
  8.                 // save image dimension
  9.                 width = image.Width;
  10.                 height = image.Height;
  11.                 frameSize = width * height;
  12.  
  13.                 // alocate memory for previous and current frames
  14.                 previousFrame = Marshal.AllocHGlobal(frameSize);
  15.                 currentFrame = Marshal.AllocHGlobal(frameSize);
  16.                 // temporary buffer
  17.                 if (suppressNoise)
  18.                 {
  19.                     tempFrame = Marshal.AllocHGlobal(frameSize);
  20.                 }
  21.  
  22.                 // lock source image
  23.                 imageData = image.LockBits(
  24.                     new Rectangle(0, 0, width, height),
  25.                     ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
  26.  
  27.                 // convert source frame to grayscale
  28.                 ImageProcessingTools.GrayscaleImage(imageData, previousFrame);
  29.  
  30.                 // unlock source image
  31.                 image.UnlockBits(imageData);
  32.  
  33.                 return;
  34.             }
  35.  
  36.             // check image dimension
  37.             if ((image.Width != width) || (image.Height != height))
  38.                 return;
  39.  
  40.             // lock source image
  41.             imageData = image.LockBits(
  42.                 new Rectangle(0, 0, width, height),
  43.                 (highlightMotionRegions) ? ImageLockMode.ReadWrite : ImageLockMode.ReadOnly,
  44.                 PixelFormat.Format24bppRgb);
  45.  
  46.             // convert current image to grayscale
  47.             ImageProcessingTools.GrayscaleImage(imageData, currentFrame);
  48.  
  49.             // pointers to previous and current frames
  50.             byte* prevFrame = (byte*)previousFrame.ToPointer();
  51.             byte* currFrame = (byte*)currentFrame.ToPointer();
  52.             // difference value
  53.             int diff;
  54.  
  55.             // 1 - get difference between frames
  56.             // 2 - threshold the difference
  57.             // 3 - copy current frame to previous frame
  58.             for (int i = 0; i < frameSize; i++, prevFrame++, currFrame++)
  59.             {
  60.                 // difference
  61.                 diff = (int)*currFrame - (int)*prevFrame;
  62.                 // copy current frame to previous
  63.                 *prevFrame = *currFrame;
  64.                 // threshold
  65.                 *currFrame = ((diff >= differenceThreshold) || (diff <= differenceThresholdNeg)) ? (byte)255 : (byte)0;
  66.             }
  67.  
  68.             // calculate amount of motion pixels
  69.             pixelsChanged = 0;
  70.  
  71.             if (suppressNoise)
  72.             {
  73.                 // suppress noise and calculate motion amount
  74.                 AForge.Win32.memcpy(tempFrame, currentFrame, frameSize);
  75.  
  76.                 byte* motion = (byte*)currentFrame.ToPointer() + width + 1;
  77.                 byte* temp = (byte*)tempFrame.ToPointer() + width + 1;
  78.  
  79.                 int widthM1 = width - 1;
  80.                 int heightM1 = height - 1;
  81.  
  82.                 // erosion is used to suppress noise
  83.                 for (int y = 1; y < heightM1; y++)
  84.                 {
  85.                     for (int x = 1; x < widthM1; x++, motion++, temp++)
  86.                     {
  87.                         // check if it is motion pixel
  88.                         if (*motion != 0)
  89.                         {
  90.                             *motion = (byte)(temp[-width - 1] & temp[-width] & temp[-width + 1] &
  91.                                 temp[width - 1] & temp[width] & temp[width + 1] &
  92.                                 temp[1] & temp[-1]);
  93.  
  94.                             pixelsChanged += (*motion & 1);
  95.                         }
  96.                     }
  97.                     motion += 2;
  98.                     temp += 2;
  99.                 }
  100.             }
  101.             else
  102.             {
  103.                 // calculate motion without suppressing noise
  104.                 byte* motion = (byte*)currentFrame.ToPointer();
  105.  
  106.                 for (int i = 0; i < frameSize; i++, motion++)
  107.                 {
  108.                     pixelsChanged += (*motion & 1);
  109.                 }
  110.             }
  111.  
  112.             // highlight motion regions
  113.             if (highlightMotionRegions)
  114.             {
  115.                 byte* src = (byte*)imageData.Scan0.ToPointer();
  116.                 byte* motion = (byte*)currentFrame.ToPointer();
  117.                 int srcOffset = imageData.Stride - width * 3;
  118.  
  119.                 // shift to the red channel
  120.                 src += 2;
  121.  
  122.                 for (int y = 0; y < height; y++)
  123.                 {
  124.                     for (int x = 0; x < width; x++, motion++, src += 3)
  125.                     {
  126.                         *src |= *motion;
  127.                     }
  128.                     src += srcOffset;
  129.                 }
  130.             }
  131.  
  132.             // unlock source image
  133.             image.UnlockBits(imageData);
  134.         }
Feb 10 '08 #1
4 1897
sicarie
4,677 Expert Mod 4TB
I seem to have missed the question there...
Feb 10 '08 #2
David24
13
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...
Feb 10 '08 #3
sicarie
4,677 Expert Mod 4TB
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?
Feb 10 '08 #4
David24
13
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

Expand|Select|Wrap|Line Numbers
  1. // calculate motion without suppressing noise
  2.                 byte* motion = (byte*)currentFrame.ToPointer();
  3.  
  4.                 for (int i = 0; i < frameSize; i++, motion++)
  5.                 {
  6.                     pixelsChanged += (*motion & 1);
  7.                 }
i don't understand this either
Expand|Select|Wrap|Line Numbers
  1. *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
Feb 13 '08 #5

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

Similar topics

5
by: Stephen Miller | last post by:
Hi, I am trying to add a staggered running total and average to a query returning quarterly CPI data. I need to add 4 quarterly data points together to calculate a moving 12-month sum (YrCPI),...
6
by: Stephen Miller | last post by:
Firstly, sorry for the long post, but I've included a fair bit of sample data. Im doing a comparision of 10yr Bond prices and CPI adjustments, with an 18 week moving average of the CPI. I'm...
8
by: Alex | last post by:
My table is laid out as such: ID (int) What (varchar 20) TimeStamp (smalldatetime) ------- ------------- --------------- 73 Start ...
6
by: J | last post by:
Kind of new at programming/vb.net. I'm doing this junky die roller program. Heres's what is supposed to happen: Roll 2 6-sided dies. Add rolls together put total in rolls(d6total). Display...
4
by: Gary | last post by:
Hi, I have a temperature conversion program down pat, but I was told to add an average, meaning, i need to get the average temperature for as many times as it was entered. i do not know where to...
3
by: C++Geek | last post by:
I need to get this program to average the salaries. What am I doing wrong? //Program to read in employee data and calculate the average salaries of the emplyees.
4
by: gaga | last post by:
hi guys, a part of my program requires me to calculate an average of items that are sold. the easiest way to do that would be writing a function, but im having trouble making up the parameters. if...
3
by: hanie | last post by:
a student wants to know his grade average for the semester. the grades are give in letter grades with numeric equivalents. develop a solution to calculate a grade average given the letter grades(the...
12
by: denveromlp | last post by:
Hello, I'm new to Access and trying to calculate a rolling 12 month average from some time data. Each data point is a date and a measurement taken at that date. As far as I can tell, the only...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.