[英]Setting RGB values of pixels in a certain image in openCV
我試圖在二進制圖像中設置一些像素的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;
我的測試圖像就在這里
輸出就在這里
好的,我現在得到了答案,感謝beaker在評論中的暗示:)我正在為其他人提供解決方案的幫助!
我只需要將輸出定義為整數。 所以最后一個cout會是這樣的
std::cout<<"Pixel at position (x, y) : ("<<150<<", "<<150<<") ="<<(int)img_bw.at<uchar>(150, 150)<<std::endl;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.