[英]Reading pixels data values from a bitmap image file
這是我用C ++編寫的代碼
int main()
{
Mat im = imread("C:/santhu/bitmap.bmp");
int rows = im.rows;
int cols = im.cols;
cout<<"rows\n"<<rows;
cout<<"cols"<<cols;
if (im.empty())
{
cout << "Cannot load image!" << endl;
return -1;
}
cout<<"the output for matrix of pixels";
for (int i = 0; i <cols ; i++)
{
Vec3b *ptr = im.ptr<Vec3b>(i);
for (int j = 0; j < rows; j++)
{
Vec3b pixel = ptr[j];
cout<<pixel<<"\t";
}
cout<<"\n";
}
getchar();
imshow("Image", im);
waitKey(0);
}
該代碼可以正常工作,直到顯示出按照Vec3b
表示的每個像素值Vec3b
,但最后會出現類似“ Unhandled exception at 0x75afb9bc in san.exe: Microsoft C++ exception: cv::Exception at memory location 0x0043f9d0..
提示窗口要求中斷或繼續執行流程
在命令控制台中,我正在獲取要顯示的像素值,在此顯示為opencv error:assertion failed(y==0 ||(data && dims)=1 &&(unsigned) y <(unsigned)size.p[0] in cv::Mat::ptr,file c:\\opencv\\build\\include\\opencv2\\core\\mat.hpp,line 428
顯示像素數據后opencv error:assertion failed(y==0 ||(data && dims)=1 &&(unsigned) y <(unsigned)size.p[0] in cv::Mat::ptr,file c:\\opencv\\build\\include\\opencv2\\core\\mat.hpp,line 428
。
我檢查了整個網絡和mat.hpp
它也提供了內聯函數,所以我很沮喪,有人可以解釋這個錯誤(異常)並幫助我讓代碼運行直到數據像素在位圖中並執行之前nice.plz
您在這里混淆行和列。
for (int i = 0; i <rows; i++) // rows, not cols
{
Vec3b *ptr = im.ptr<Vec3b>(i);
for (int j = 0; j < cols; j++) // cols, not rows
{
Vec3b pixel = ptr[j];
cout<<pixel<<"\t";
}
cout<<"\n";
}
色彩格式
for(int j = 0; j < img.rows; j++) {
for(int i = 0; i < img.cols; i++) {
uchar b = img.ptr<cv::Vec3b>(j)[i][0];
ucahr g = img.ptr<cv::Vec3b>(j)[i][1];
uchar r = img.ptr<cv::Vec3b>(j)[i][2];
std::cout << "b = " << (int) b << std::endl
<< "g = " << (int) g << std::endl
<< "r = " << (int) r << std::endl;
}
}
灰色格式
cv::Mat img;
cv::cvtColor(src,img,CV_BGR2RGB);
for(int j = 0; j < img.rows; j++) {
for(int i = 0; i < img.cols; i++) {
std::cout << "gray value = " << img.ptr<uchar>(j)[i] << std::endl;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.