【问题标题】:getting a value in binary image获取二进制图像中的值
【发布时间】:2014-06-08 03:44:09
【问题描述】:

我正在尝试在二进制图像中获取一组值以将其反转。但我无法索引矩阵,我的代码的第一行是。

std::string path = "img/lena.jpg";

//Our color image
cv::Mat imageMat = cv::imread(path, CV_LOAD_IMAGE_GRAYSCALE);

if(imageMat.empty())
{
    std::cerr << "ERROR: Could not read image " << argv[1] << std::endl;
    return 1;
}

//Grayscale matrix
cv::Mat grayscaleMat (imageMat.size(), CV_8U);

//Convert BGR to Gray
cv::cvtColor( imageMat, grayscaleMat, CV_BGR2GRAY );

//Binary image
cv::Mat binaryMat(grayscaleMat.size(), grayscaleMat.type());

//Apply thresholding
cv::threshold(grayscaleMat, binaryMat, 100, 255, cv::THRESH_BINARY);

现在我需要处理 binaryMat 中的值,但我不知道如何获取它...

【问题讨论】:

    标签: c++ opencv visual-c++


    【解决方案1】:

    1:使用 opencv 的 c++ api,您不需要分配输出/结果 Mat 的。把它们留空。

    //Convert BGR to Gray
    cv::Mat grayscaleMat;   
    cv::cvtColor( imageMat, grayscaleMat, CV_BGR2GRAY );
    
    //Apply thresholding
    cv::Mat binaryMat;    
    cv::threshold(grayscaleMat, binaryMat, 100, 255, cv::THRESH_BINARY);
    

    2:现在访问像素:

    uchar p = binaryMat.at<uchar>(y,x); // row,col world !
    binaryMat.at<uchar>(5,5) = 17;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-31
      • 2010-11-20
      • 2012-08-22
      • 1970-01-01
      相关资源
      最近更新 更多