1 | initial version |
You try to print a pointer to a matrix (CvMat*) with printf. I think you want to print the pixel values? With the C++ version it's easier, but you could look at cvGet2D function to read pixel inside a loop. There is many code samples on internet and on OpenCV doc zone. But I write a c++ version of your code below. You should definitively used this as C version will be suppressed one day.
cv::Mat small, bin_img;
cv::Size size(20,20);
cv::Mat img = cv::imread( "Koala.jpg" );
if( !img.data )
{
std::cerr << "Error, unable to load the image" << std::endl;
return -1;
}
cv::resize( img, small, size );
cv::threshold( small, bin_img, 100, 255, CV_THRESH_BINARY);
// Efficient way of parsing images.
// See the doc below for all explanations
// and to see the non efficient way if it's more understandable for you.
int channels = bin_img.channels();
int nRows = bin_img.rows;
int nCols = bin_img.cols * channels;
if (bin_img.isContinuous())
{
nCols *= nRows;
nRows = 1;
}
int i,j;
uchar* p;
for( i = 0; i < nRows; ++i)
{
p = bin_img.ptr<uchar>(i);
for ( j = 0; j < nCols; ++j)
{
p[j] = 1;
}
}
std::cout << img_bin << std::endl; // print a 20x20 array of 1.
Some resources needed to "understand" c++ version of OpenCV: