вот код, который я написал в 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
, но, наконец, Unhandled exception at 0x75afb9bc in san.exe: Microsoft c++ exception: cv::Exception at memory location 0x0043f9d0..
исключение типа " Unhandled exception at 0x75afb9bc in san.exe: Microsoft c++ exception: cv::Exception at memory location 0x0043f9d0..
" в подсказке окна просит сломать или продолжить поток
И в командной консоли, где im получает значения пикселей, которые будут отображаться, в этом он показывает мне как 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
также дал встроенную функцию, поэтому я разочарован, может кто-нибудь объяснить эту ошибку (исключение) и помочь мне запустить код только до тех пор, пока пиксели данных не станут их в растровом изображении и не будут выполнены nicely.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";
}
ptr[j][1] = 255;
Цветной формат
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;
}
}