Search code examples
c++opencvcclconnected-components

OpenCv see each pixel value in an image


I'm working on Connected Component Labeling (CCL) operation in OpenCv (in C++ language). To see whether CCL works reliably, I must check each pixel value in the image while debugging. I have tried saving the result of CCL as an image, however I could not reach digital values of the pixels. Is there any way of doing this during debugging operation?


Solution

  • Convert the CCL matrix into values in the range [0, 255] and save it as an image. For example:

    cv::Mat ccl = ...; // ccl operation returning CV_8U
    double min, max;
    cv::minMaxLoc(ccl, &min, &max);
    cv::Mat image = ccl * (255. / max);
    cv::imwrite("ccl.png", image);
    

    Or store all the values in a file:

    std::ofstream f("ccl.txt");
    f << "row col value" << std::endl;
    for (int r = 0; r < ccl.rows; ++r) {
      unsigned char* row = ccl.ptr<unsigned char>(r);
      for (int c = 0; c < ccl.cols; ++c) {
        f << r << " " << c << " " << static_cast<int>(row[c]) << std::endl;
      }
    }