opencvimreadpbm

C++ Can't open pbm files with Opencv


I'm trying to load a .pbm image with opencv.

I'm using the imread function; the documentation says that it should open pbm files but I can't make it work.

It does work with other format like png/jpeg/...

#define CV_LOAD_IMAGE_ANYDEPTH 2
#define CV_LOAD_IMAGE_ANYCOLOR 4
[...]
//argv[1] is the file's name
cv::Mat img = cv::imread(argv[1], CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR);
if (img.rows*img.cols==0){
    printf("\nImage not loaded");
    return 0;
}

..of course, with pbm files, it returns 0.


Solution

  • If you are using OpenCV 3, you can use:

    Mat img = imread("image.pbm",IMREAD_ANYCOLOR|IMREAD_ANYDEPTH);
    

    If you want to rule out the possibility that your starting images are corrupt/incorrect, you can create test images easily at the command line with ImageMagick, which is installed on most Linux distros, and is available for OSX and Windows.

    # Create P1 (ASCII/plain) PBM and P4 (binary/rawbits) PBM
    convert -size 200x100 xc:white -bordercolor black -border 50 -compress none P1.pbm
    convert -size 200x100 xc:white -bordercolor black -border 50 P4.pbm