开发者

Getting the centriod of a circle Turbo C++

开发者 https://www.devze.com 2023-03-17 15:57 出处:网络
It\'s me again. Turbo C++ is real ancient. I got a problem in getting my centriod of a circle, I have written a program to show me the co-ordinates of the centroid but the display shows me -NAN, -NAN

It's me again. Turbo C++ is real ancient. I got a problem in getting my centriod of a circle, I have written a program to show me the co-ordinates of the centroid but the display shows me -NAN, -NAN instead of numbers. Please advice, thnx.

unsigned char *p = rgbImage; //rbgImage = new unsigned char [ 160 * 120 * 4 ]
unsigned char *q = image; //image = new unsigned char [ 160 * 120 * 1 ]
int n = 0;
float LaserX = 0, LaserY = 0;
char* LaserMID = new char[255];

for( int j = 0; j < 120; j++ ) {
    for( int i = 0; i < 160; i++ ) {
        *q++ = *p++;
        if ( *q >= Thrshld ) {
            LaserX += j;
            LaserY += i;
            n = n + 1;
        }
    }
}
LaserX = LaserX/n;
LaserY =开发者_C百科 LaserY/n;
sprintf(LaserMID, "%.1f, %.1f", LaserX, LaserY);
ShowCo->Text = LaserMID;


You need to check that n is non-zero prior to your division.


You need to handle the case where no value pointed by q is greater than your threshold : in that case, n stays at 0 and you end up with a division by 0.

Moreover, I don't understand why you use *q++ = *p++; It's hard to read (ok, I never know if ++ happens before or after assignement in both sides)

  • adding parenthesis might easy readability
  • doing it in one big memcpy will be much more time efficient

Concerning the original data, name of the variable and size seems to point that your p pointer should be incremented more than that (like 3 or 4 instead of 1) to always check the same color and not use all colors and cover 1/3 of the picture (assuming memory representation is pixel_0_r,pixel_0_g,pixel_0_b, pixel_1_r, pixel_1_g, pixel_1_b...

0

精彩评论

暂无评论...
验证码 换一张
取 消