C++ Opencv写入和读取像素
写入像素
if(img.channels() == 3) // 3通道图像 { img.at<cv::Vec3b>(0, 0)[0] = 255; img.at<cv::Vec3b>(0, 0)[1] = 255; img.at<cv::Vec3b>(0, 0)[2] = 255; } else img.at<uchar>(0, 0) = 255;
读取像素
if(img.channels() == 3) { cout<< static_case<int>(img.at<cv::Vec3b>(0, 0)[0])<<endl;
cout<< static_case<int>(img.at<cv::Vec3b>(0, 0)[1])<<endl;
cout<< static_case<int>(img.at<cv::Vec3b>(0, 0)[2])<<endl;
}
else
{
cout<< static_case<int>(img.at<uchar>(0, 0))<<endl;
}
还有另一种方法读写像素
cv::Mat img(fileName); cv::Mat_<uchar> imgTemp(img); // 假如图片是单通道,告诉模板 T 为 uchar cout<<static_cast<int>(imgTemp(0, 0))<<endl; imgTemp(0, 0) = 255; // 写入像素