Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

finding centroid of a mask

hi, i have a mask obtained by threshold function. i wonder if i can find its centroid with builtin function, now i'm doing manual:

    float sumx=0, sumy=0;
    float num_pixel = 0;
    for(int x=0; x<difference.cols; x++) {
        for(int y=0; y<difference.rows; y++) {
            int val = difference.at<uchar>(y,x);
            if( val >= 50) {
                sumx += x;
                sumy += y;
                num_pixel++;
            }
        }
    }
    Point p(sumx/num_pixel, sumy/num_pixel);

but probabilly is there a better way..

finding centroid of a mask

hi, i have a mask obtained by threshold function. i wonder if i can find its centroid with builtin function, now i'm doing manual:

    float sumx=0, sumy=0;
    float num_pixel = 0;
    for(int x=0; x<difference.cols; x++) {
        for(int y=0; y<difference.rows; y++) {
            int val = difference.at<uchar>(y,x);
            if( val >= 50) {
                sumx += x;
                sumy += y;
                num_pixel++;
            }
        }
    }
    Point p(sumx/num_pixel, sumy/num_pixel);

but probabilly is there a better way..

finding centroid of a mask

hi, i have a mask obtained by threshold function. i wonder if i can find its centroid with builtin function, now i'm doing manual:

    float sumx=0, sumy=0;
    float num_pixel = 0;
    for(int x=0; x<difference.cols; x++) {
        for(int y=0; y<difference.rows; y++) {
            int val = difference.at<uchar>(y,x);
            if( val >= 50) {
                sumx += x;
                sumy += y;
                num_pixel++;
            }
        }
    }
    Point p(sumx/num_pixel, sumy/num_pixel);

but probabilly is there a better way..