gpt4 book ai didi

c++ - 在openCV中设置某个图像中像素的RGB值

转载 作者:太空宇宙 更新时间:2023-11-03 23:14:59 24 4
gpt4 key购买 nike

我正在尝试设置二进制图像中某些像素的 RGB 值。但是只要坐标大于 (89, 89) 它就会给我一个断言错误!我的图像分辨率还可以,因为我正在从 (150, 150) 坐标访问 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;

在这里,如果我在“设置 BGR”部分中输入 89 而不是 150,那么它就可以工作。否则完整的错误是:

OpenCV 错误:断言失败 (dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] && (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)( size.p 1 *channels()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3) - 1) )*4) & 15) == elemSize1()) 在 cv::Mat::at 中,文件 e:\opencv\opencv\build\include\opencv2\core\mat.hpp,第 538 行

那么这是任何类型的内存空间错误吗?在此先感谢您的帮助! :)

更新:我试过这种方法!但是现在输出是空白的。

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;

我的测试图在这里

This is my test image here

输出在这里

Output image here

最佳答案

好的,感谢烧杯在评论中的提示,我现在有了答案:) 我正在为其他人提供帮助的解决方案!

我只需要将输出定义为一个整数。所以最后一次会像

std::cout<<"Pixel at position (x, y) : ("<<150<<", "<<150<<") ="<<(int)img_bw.at<uchar>(150, 150)<<std::endl;

关于c++ - 在openCV中设置某个图像中像素的RGB值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41807386/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com