获取二值图像的值

getting a value in binary image

本文关键字:二值图像 获取      更新时间:2023-10-16

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

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:使用opencv的c++ api,您不需要分配输出/结果Mat's。

//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;