在openCV中设置特定图像中像素的RGB值 [英] Setting RGB values of pixels in a certain image in openCV

查看:184
本文介绍了在openCV中设置特定图像中像素的RGB值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to set RGB values of some pixels in a binary image. But whenever the coordinate is more than (89, 89) its giving me an assertion error! My image resolution is okay because I am accessing RGB values from (150, 150) coordinate. If the coordinate is (89, 89) or less it works fine. My Code:

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,那么它将起作用.否则,完整错误为:

Here if I put 89 instead of 150 in the "Setting BGR" section then it works. Otherwise the full error is:

OpenCV错误:断言失败(dims< = 2&&&&(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行

OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] && (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p1*channels()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3) - 1))*4) & 15) == elemSize1()) in cv::Mat::at, file e:\opencv\opencv\build\include\opencv2\core\mat.hpp, line 538

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

So is this any type of memory space error? Thanks in advance for the helps! :)

更新:我已经尝试过这种方式!但是现在输出为空.

UPDATE: I've tried it this way! But the output is blank now.

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;

我的测试图像在这里

输出在这里

推荐答案

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

Okay I have the answer now thanks to beaker's hint in the comment :) I'm putting the solution for others help!

我只需要将输出定义为整数.所以最后一个提示就像

I just need to define the output as an integer. So the last cout will be like

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

这篇关于在openCV中设置特定图像中像素的RGB值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆