Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Convert binary image values to '0' '1' integers

I'm trying to convert a binary mask (for an image) to an integer array, and so far I'm having trouble understanding how to regard the type and value of the pixels in this image.

I used a binary image file containing imgMask:

threshold(bgIsolation, imgMask, 10, 255, THRESH_BINARY);

image description

Then tried to convert it to an integer vector for later classification, using the following function:

void color_imgreshape_2_1(Mat &img, int *class_labels){
    int i = 0;
    cout << "rows: " << img.rows << " cols: " << img.cols << endl;
    for (int x = 0; x < img.cols; ++x){
        for (int y = 0; y < img.rows; ++y){
            class_labels[i++] = (int)img.at<uchar>(x,y);
        }
    }
    ofstream out("out.txt");
    cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt!
    for (int i = 0; i < img.rows * img.cols; i++)
        cout << class_labels[i] << endl;
    return;
}

Redirecting the labels vector output to a file, it appears that it contains different values, and no 1's or 0's.

So can anyone tell me how to convert those pixel values to integers?

Convert binary image values to '0' '1' integers

I'm trying to convert a binary mask (for an image) to an integer array, and so far I'm having trouble understanding how to regard the type and value of the pixels in this image.

I used a binary image file containing imgMask:

threshold(bgIsolation, imgMask, 10, 255, THRESH_BINARY);

image description

Then tried to convert it to an integer vector for later classification, using the following function:

void color_imgreshape_2_1(Mat &img, int *class_labels){
    int i = 0;
    cout << "rows: " << img.rows << " cols: " << img.cols << endl;
    for (int x = 0; x < img.cols; ++x){
        for (int y = 0; y < img.rows; ++y){
            class_labels[i++] = (int)img.at<uchar>(x,y);
(int)img.at<uchar>(y,x);
        }
    }
    ofstream out("out.txt");
    cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt!
    for (int i = 0; i < img.rows * img.cols; i++)
        cout << class_labels[i] << endl;
    return;
}

Redirecting the labels vector output to a file, it appears that it contains different values, and no 1's or 0's.

So can anyone tell me how to convert those pixel values to integers?