C++ 在openCV中设置特定图像中像素的RGB值

C++ 在openCV中设置特定图像中像素的RGB值,c++,opencv,image-processing,C++,Opencv,Image Processing,我正在尝试设置二进制图像中某些像素的RGB值。但每当坐标大于(89,89)时,它就会给我一个断言错误!我的图像分辨率还可以,因为我正在从(150150)坐标访问RGB值。如果坐标为(89,89)或更小,则工作正常。我的代码: cv::Mat img_gray, img_bw; //read an image cv::Mat3b img_bgr = cv::imread("test.jpg"); cv::imshow("Original Image", img_bgr); //conversio

我正在尝试设置二进制图像中某些像素的RGB值。但每当坐标大于(89,89)时,它就会给我一个断言错误!我的图像分辨率还可以,因为我正在从(150150)坐标访问RGB值。如果坐标为(89,89)或更小,则工作正常。我的代码:

cv::Mat img_gray, img_bw;
//read an image
cv::Mat3b img_bgr = cv::imread("test.jpg");
cv::imshow("Original Image", img_bgr);

//conversion to binary from color
cv::cvtColor(img_bgr, img_gray,CV_RGB2GRAY);
cv::threshold(img_gray, img_bw, 75.0, 255.0, THRESH_BINARY);

//accessing BGR of position (150, 150) from a color image
std::cout<<"Pixel at position (x, y) : ("<<150<<", "<<150<<") ="<<img_bgr(150,150)<<std::endl;  

//Setting BGR of position (150, 150) in binary image
img_bw.at<Vec3b>(150, 150)[0] = 255;
img_bw.at<Vec3b>(150, 150)[1] = 255;
img_bw.at<Vec3b>(150, 150)[2] = 255;
std::cout<<"Pixel at position (x, y) : ("<<150<<", "<<150<<") ="<<img_bw.at<Vec3b>(150, 150)<<std::endl;
cv::Mat img_gray,img_bw;
//读图
cv::Mat3b img_bgr=cv::imread(“test.jpg”);
cv::imshow(“原始图像”,img_bgr);
//从彩色到二进制的转换
cv::cvt颜色(img_bgr、img_gray、cv_RGB2GRAY);
cv::阈值(img_灰度、img_bw、75.0、255.0、阈值二进制);
//从彩色图像访问位置(150150)的BGR

std::cout好的,多亏比克在评论中的提示,我现在有了答案:)我把解决方案交给别人帮助

我只需要将输出定义为一个整数。所以最后一场比赛会是这样的

cv::Mat img_gray, img_bw;
//read an image
cv::Mat3b img_bgr = cv::imread("test.jpg");
cv::imshow("Original Image", img_bgr);

//conversion to binary from color
cv::cvtColor(img_bgr, img_gray,CV_RGB2GRAY);
cv::threshold(img_gray, img_bw, 75.0, 255.0, THRESH_BINARY);

//accessing BGR of position (150, 150) from a color image
std::cout<<"Pixel at position (x, y) : ("<<150<<", "<<150<<") ="<<img_bgr(150,150)<<std::endl;  

//Setting BGR of position (150, 150) in binary image
img_bw.at<uchar>(150, 150) = 255;
std::cout<<"Pixel at position (x, y) : ("<<150<<", "<<150<<") ="<<img_bw.at<uchar>(150, 150)<<std::endl;

std::cout好的,多亏比克在评论中的提示,我现在有了答案:)我把这个解决方案交给别人帮助

我只需要将输出定义为一个整数。所以最后一场比赛会是这样的

cv::Mat img_gray, img_bw;
//read an image
cv::Mat3b img_bgr = cv::imread("test.jpg");
cv::imshow("Original Image", img_bgr);

//conversion to binary from color
cv::cvtColor(img_bgr, img_gray,CV_RGB2GRAY);
cv::threshold(img_gray, img_bw, 75.0, 255.0, THRESH_BINARY);

//accessing BGR of position (150, 150) from a color image
std::cout<<"Pixel at position (x, y) : ("<<150<<", "<<150<<") ="<<img_bgr(150,150)<<std::endl;  

//Setting BGR of position (150, 150) in binary image
img_bw.at<uchar>(150, 150) = 255;
std::cout<<"Pixel at position (x, y) : ("<<150<<", "<<150<<") ="<<img_bw.at<uchar>(150, 150)<<std::endl;

std::coutYour img_bw为灰色类型。因此,您无法通过键入img_bw.at访问任何像素。您应该使用img_bw.at(150150)=255@alex我已经试过了。在这种情况下,我的性病:cout@TousifZaman我们无法修复您未显示的代码。您知道使用
Vec3b
是不正确的。如果您使用
uchar
向我们显示您的代码以及错误和输入图像,也许我们可以帮助您。@Tusifzaman更新应该使用添加到问题本身,而不是添加到注释中。我们仍然没有您的输入图像,因此无法再现您的错误。您正在打印一个值为
0
255
。这两个字符(ascii或utf-8)都不是可打印字符。尝试将像素值指定给int变量,然后打印。您的img_bw是灰色类型。因此,您无法通过键入img_bw.at访问任何像素。您应该使用img_bw.at(150150)=255@alex我已经试过了。在这种情况下,我的性病:cout@TousifZaman我们无法修复您未显示的代码。您知道使用
Vec3b
是不正确的。如果您使用
uchar
向我们显示您的代码以及错误和输入图像,也许我们可以帮助您。@Tusifzaman更新应该使用添加到问题本身,而不是添加到注释中。我们仍然没有您的输入图像,因此无法再现您的错误。您正在打印一个值为
0
255
。这两个字符(ascii或utf-8)都不是可打印字符。尝试将像素值指定给int变量,然后打印。