Ask Your Question

Revision history [back]

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:

  • [Resize](http://docs.opencv.org/modules/imgproc/doc/geometric_transformations.html?highlight=resize#void resize(InputArray src, OutputArray dst, Size dsize, double fx, double fy, int interpolation)) doc,
  • [threshold](http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html?highlight=threshold#double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)) doc,
  • tutorial on image parsing,
  • efficently parse image tutorial
  • and the basic mat container tutorial.