473,385 Members | 1,925 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.

"Static" (VB6) equalivent in C++?

I have a function which will return a shape from an image everytime it's
called.
Since it's time consuming I would like it to run itself only once, and
at every subsequent calls it should return the shape it calculated the
first time.
In VB6 I would have used the key word "static" to preserve the results
from the first call. Is there an equivalent in C++?

---------------
In VB6 I would have simply said

function Foo(byref uSomething, byref uOutput) as boolean

static bWasRunBefore as boolean //This is a variable will be never get
out of scope
static nFirstTimeResult as something //This is a variable will be never
get out of scope

if not bWasRunBefore
//If it's the first time that this function is called
//do some heavy math stuff
uOutPut = nResult
nFirstTimeResult = nResult //Rember the first result for subsequent calls
bWasRunBefore = True //Remember that this function was called before
else
//Function was already called before, and we will return the old result
//return the return from the first time around
uOutput = nFirstTimeResult
end if

End function

-----------------
C++ code:

bool ViolaJonesFindFace(SHAPE &Shape, const IplImage* image,const char
sDataDir[])
{
if (!pgCascade) // first time?
OpenViolaJones(sDataDir);
Shape.dimClear(NBR_VIOLA_JONES_POINTS, 2);

IplImage* pWork = cvCreateImage
(cvSize(image->width/2, image->height/2), image->depth, image->nChannels);
cvPyrDown(image, pWork, CV_GAUSSIAN_5x5 );
CvSeq* pFaces = cvHaarDetectObjects(pWork, pgCascade, pgStorage,
SCALE_FACTOR, MIN_NEIGHBORS,CV_HAAR_DO_CANNY_PRUNING);

cvReleaseImage(&pWork);

if(0 == pFaces->total)//can't find a face
return false;

int iSelectedFace = 0;
// get most central face
double MaxOffset = 1e10;
// max abs dist from center of face to center of image
for (int iFace = 0; iFace < pFaces->total; iFace++)
{
CvRect* r = (CvRect*)cvGetSeqElem(pFaces, iFace);
double Offset = ABS(r->x*2.0 + r->width - image->width/2.0);
if (Offset < MaxOffset)
{
MaxOffset = Offset;
iSelectedFace = iFace;
}
}

// Write the global detector shape into Shape.
// We must convert the Viola Jones shape coords to our internal shape
coords.
CvRect* r = (CvRect*)cvGetSeqElem(pFaces, iSelectedFace);

int scale = 2;
CvPoint pt1, pt2;
pt1.x = r->x*scale;
pt2.x = (r->x+r->width)*scale;
pt1.y = r->y*scale;
pt2.y = (r->y+r->height)*scale;

Shape(DETECTOR_TopLeft, VX) = r->x*2 - image->width/2.0;
Shape(DETECTOR_TopLeft, VY) = image->height/2.0 - r->y*2;
Shape(DETECTOR_BotRight, VX) = Shape(DETECTOR_TopLeft, VX) + 2*r->width;
Shape(DETECTOR_BotRight, VY) = Shape(DETECTOR_TopLeft, VY) - 2*r->height;

return true;
}
Oct 29 '08 #1
7 1732
Anna Smidt wrote:
I have a function which will return a shape from an image everytime it's
called.
Since it's time consuming I would like it to run itself only once, and
at every subsequent calls it should return the shape it calculated the
first time.
In VB6 I would have used the key word "static" to preserve the results
from the first call. Is there an equivalent in C++?

---------------
In VB6 I would have simply said

function Foo(byref uSomething, byref uOutput) as boolean

static bWasRunBefore as boolean //This is a variable will be never
get out of scope
static nFirstTimeResult as something //This is a variable will be
never get out of scope

if not bWasRunBefore
//If it's the first time that this function is called
//do some heavy math stuff
uOutPut = nResult
nFirstTimeResult = nResult //Rember the first result for
subsequent calls
bWasRunBefore = True //Remember that this function was called
before
else
//Function was already called before, and we will return the old
result
//return the return from the first time around
uOutput = nFirstTimeResult
end if

End function

-----------------
C++ code:

bool ViolaJonesFindFace(SHAPE &Shape, const IplImage* image,const char
sDataDir[])
{
if (!pgCascade) // first time?
OpenViolaJones(sDataDir);
Shape.dimClear(NBR_VIOLA_JONES_POINTS, 2);

IplImage* pWork = cvCreateImage
(cvSize(image->width/2, image->height/2), image->depth,
image->nChannels);
cvPyrDown(image, pWork, CV_GAUSSIAN_5x5 );
CvSeq* pFaces = cvHaarDetectObjects(pWork, pgCascade, pgStorage,
SCALE_FACTOR, MIN_NEIGHBORS,CV_HAAR_DO_CANNY_PRUNING);

cvReleaseImage(&pWork);

if(0 == pFaces->total)//can't find a face
return false;

int iSelectedFace = 0;
// get most central face
double MaxOffset = 1e10;
// max abs dist from center of face to center of image
for (int iFace = 0; iFace < pFaces->total; iFace++)
{
CvRect* r = (CvRect*)cvGetSeqElem(pFaces, iFace);
double Offset = ABS(r->x*2.0 + r->width - image->width/2.0);
if (Offset < MaxOffset)
{
MaxOffset = Offset;
iSelectedFace = iFace;
}
}

// Write the global detector shape into Shape.
// We must convert the Viola Jones shape coords to our internal
shape coords.
CvRect* r = (CvRect*)cvGetSeqElem(pFaces, iSelectedFace);

int scale = 2;
CvPoint pt1, pt2;
pt1.x = r->x*scale;
pt2.x = (r->x+r->width)*scale;
pt1.y = r->y*scale;
pt2.y = (r->y+r->height)*scale;

Shape(DETECTOR_TopLeft, VX) = r->x*2 - image->width/2.0;
Shape(DETECTOR_TopLeft, VY) = image->height/2.0 - r->y*2;
Shape(DETECTOR_BotRight, VX) = Shape(DETECTOR_TopLeft, VX) +
2*r->width;
Shape(DETECTOR_BotRight, VY) = Shape(DETECTOR_TopLeft, VY) -
2*r->height;

return true;
}
Anna:

You can use the static keyword in just this way in C or C++. But you must be
sure the function cannot be called from multiple threads at the same time.

--
David Wilkinson
Visual C++ MVP
Oct 29 '08 #2
Thank you!
Anna
Oct 29 '08 #3
I seem to be a bit stupid... Why is "iDone" always "1"?

static int iDone=0;

iDone++; //Increment each time the void is called

if (iDone>5)
{
iDone=1; // If iDone exceeds 5, set it to 1 again
}

if (iDone=1) //
{
printf("iDone is 1!");
}
else
{
printf("Yieha!! Not 1!");
}

....
Oct 29 '08 #4
Anna Smidt wrote:
I seem to be a bit stupid... Why is "iDone" always "1"?

static int iDone=0;

iDone++; //Increment each time the void is called

if (iDone>5)
{
iDone=1; // If iDone exceeds 5, set it to 1 again
}

if (iDone=1) //
Right here, you have used the assignment operator (one =), when you wanted
the equality comparison operator (==, that's two =). Turn up your warning
level and pay attention to the warnings.
{
printf("iDone is 1!");
}
else
{
printf("Yieha!! Not 1!");
}

...

Oct 29 '08 #5
Doh!! How embarassing!
Thanks.

Anna
Oct 29 '08 #6
"Anna Smidt" <a.*****@nospamgmail.comwrote in message
news:#I**************@TK2MSFTNGP02.phx.gbl...
Doh!! How embarassing!
Thanks.
The ease with which I and other experts recognize this mistake should
suggest that we've seen it many times before -- most often in our own code.

There are some tricks to help prevent it though.

For example,

if (1 == x)

is legal, but

if (1 = x)

is not. If you put anything on the left side that can't be assigned,
whether a literal number or an expression, the compiler won't let you make
the mistake.
>
Anna
Oct 30 '08 #7
Hi Anna,
Doh!! How embarassing!
In addition to Ben's suggestion.

Turn on warning level 4 in your project's C++ settings.
That will warn on assignments in conditional expressions.

Besides that it will warn you about a lot of dubious code.

So try to get your code warning free in level 4 as good as
you can and you have at least taken all the help the
compiler can give you.

--
SvenC
Oct 30 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member...
3
by: Ajax Chelsea | last post by:
can not the "static const int" be replaced by "static enum" anywhere? is it necessary that define special initialization syntax for "static const int"?
12
by: cppaddict | last post by:
Hi, I know that it is illegal in C++ to have a static pure virtual method, but it seems something like this would be useful when the following 2 conditions hold: 1. You know that every one...
3
by: Zeljko | last post by:
Hi, Is there a way to define a local variable that retains it's value between the function calls ? Zeljko
9
by: Neil Kiser | last post by:
I'm trying to understand what defining a class as 'static' does for me. Here's an example, because maybe I am thinking about this all wrong: My app will allows the user to control the fonts...
11
by: comp.lang.php | last post by:
function blah($item) { if (!isset($baseDir)) { static $baseDir = ''; $baseDir = $item; print_r("baseDir = $baseDir\n"); } $dirID = opendir($item); while (($fyl = readdir($dirID)) !== false)...
16
by: Chris | last post by:
Looking at some code I see a declaration inside a function like static const string s("some string"); Does the static serve any purpose here?
5
by: none | last post by:
I'd like to create a new static property in a class "hiding" the property present in a base class. Since this needs to happen at runtime I tried doing this via DynamicMethod. But obviously the...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.