我正在尝试设置二进制图像中某些像素的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;
如果我在&#34;设置BGR&#34;中放置89而不是150。然后它工作。否则完整错误是:
OpenCV错误:断言失败(dims&lt; = 2&amp;&amp; data&amp;&amp;(unsigned)i0&lt;(unsigned)size.p [0]&amp;&amp;(unsigned)(i1 * DataType&lt; _Tp&gt; :: channels)&lt;(unsigned)(size.p 1 * channels())&amp;&amp;((((sizeof(size_t)&lt;&lt;&lt; 28)| 0x8442211)&gt;&gt; ((DataType&lt; _Tp&gt; :: depth)&amp;((1&lt;&lt; 3)-1))* 4)&amp; 15)== elemSize1())in cv :: Mat :: at,file 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;
我的测试图片在这里
输出就在这里
答案 0 :(得分:0)
好的,我现在得到了答案,感谢beaker在评论中的暗示:)我正在为其他人提供解决方案帮助!
我只需要将输出定义为整数。所以最后一个cout就像
std::cout<<"Pixel at position (x, y) : ("<<150<<", "<<150<<") ="<<(int)img_bw.at<uchar>(150, 150)<<std::endl;