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: - 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);
-
}
4 1897
I seem to have missed the question there...
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...
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?
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 - // 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
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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),...
|
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...
|
by: Alex |
last post by:
My table is laid out as such:
ID (int) What (varchar 20) TimeStamp (smalldatetime)
------- ------------- ---------------
73 Start ...
|
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...
|
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...
|
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.
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
| |