Recursive method setting values to negative values

  c++, image-processing, mat, recursion

I am trying to work around a problem where I need to separate foreground from background of an image, for my kernel to iterate through. I’ve bumped into a problem where I need to assign all values of 255 to -1 and let values of 0 stay 0.

As I’m a noob at C++ I’m not even sure if it’s possible to assign negative values to an Mat object. Anyone who can help me understand this matter?

Kind regards.

int main ()
{
    Mat src, dst; /// source-file, destination, filter
    
    src = imread("/Users//Developer/OpenCV-test/checkers.jpeg", IMREAD_GRAYSCALE);

    int imageWidth = src.size().width;
    int imageHeight = src.size().height;

    
    dst = src.clone();

    if (!src.data) {
        printf("No image!n");
        return -1;
    } else {
        cout << imageWidth << "x" << imageHeight << endl;
    }
    

        for (int i = 0; i < src.rows; i ++)
        {
            for(int j = 1; j < src.cols; j++)
            {
                if(src.at<uint8_t>(i,j) >= 254)
                {
                    src.at<uint8_t>(i,j) = 1;
                }
            }
        }
    
    cout << src << endl;

    namedWindow("Output", WINDOW_AUTOSIZE);
    imshow("Output", src);

    waitKey();

}

Source: Windows Questions C++

LEAVE A COMMENT