获取二进制图像中的值

时间:2014-06-08 03:44:09

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

我试图在二进制图像中获取一组值来反转它..但是我有麻烦索引矩阵,我的代码的第一行是。

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中的值,但我不知道如何得到它......

1 个答案:

答案 0 :(得分:3)

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;