1

I want to load an image in c++ opencv that comes from a postgresql database. The image, jpg extension, is stored as a binary data (bytea type) in the base, that I can access thanks to libpqxx.

The problem is that I do not know how to convert the data into a cv::Mat instance. With a regular image I could use imread('myImage.jpg', ...), but in this case I cannot even load the database image in the data attribute of Mat because it is jpeg and not bmp.

Any idea ? Is there some opencv method I could use that could understand directly the binary data and convert it to the appropriate structure ? the imdecode() functions seems to be used for bitmap datas.

edit : Berak, using a vector the imdecode function returns null Matrice What happens "If the buffer is too short or contains invalid data, the empty matrix/image is returned." Here is the code :

pqxx::result r=bdd::requete("SELECT image FROM lrad.img WHERE id=3",1);//returns the bytea image in r[0]["image"]
const char* buffer=r[0]["image"].c_str();    
vector<uchar>::size_type size = strlen((const char*)buffer);
vector<uchar> jpgbytes(buffer, buffer+size);
Mat img = imdecode(jpgbytes, CV_LOAD_IMAGE_COLOR); 
//jpgbytes.size()=1416562 img.size()=[0 x 0]

What am I missing ?

1
  • how did you store the image ? (sql type)
    – berak
    Oct 8, 2014 at 11:55

2 Answers 2

4

still, use imdecode . it can handle png,jpg,bmp,ppm,webp,jp2,exr, but no gif.

vector<uchar> jpgbytes; // from your db
Mat img = imdecode(jpgbytes);

(you should do the same for bmp or any other supported formats, don't mess with Mat's raw data pointers!)

4
  • is not enough. I converted my request result into a vector<uchar> but imdecode does not understand the datas...
    – alexis
    Oct 8, 2014 at 9:38
  • 1
    so, what is in your data ? any chance there's some additional base64 encoding or such ? imdecode expects the same as an image on disk.
    – berak
    Oct 8, 2014 at 11:50
  • 1
    const char* buffer=r[0]["image"].c_str(); // <-- if that was binary data, this will truncate at the first 0.
    – berak
    Oct 8, 2014 at 11:53
  • Ok there is something wrong in my jpgbytes because it is twice longer than the file in the database. c_str() seem to convert into hexa to avoid the /0 trouble. How can I make it comeback to the good representation ?
    – alexis
    Oct 8, 2014 at 12:04
1

Ok I have the process to convert a bytea data to a cv::Mat, here is the code.

inline int dec(uchar x){ //convert uchar to int
    if (x>='0'&&x<='9') return (x-'0');
    else if (x>='a'&&x<='f') return (x-'a'+10);
    else if (x>='A'&&x<='F') return (x-'A'+10);
    return 0;
}
cv::Mat bytea2Mat(const pqxx::result::field& f){
    const char* buffer=f.c_str();  
    vector<uchar>::size_type size = strlen((const char*)buffer);
    vector<uchar> jpgbytes(size/2-1); 

    for (size_t i=0; i!=size/2-1;i++) { 
        jpgbytes[i]=(dec(buffer[2*(i+1)])<<4)+dec(buffer[2*(i+1)+1]);
    }
    cout <<size/2<<";"<<jpgbytes.size()<<endl;
    return imdecode(jpgbytes, CV_LOAD_IMAGE_COLOR);
}

The bytea output is encrypted as a char* looking like "\x41204230", for an original input string of "a b0" in hexa form. (the \x may not be present according to the data input) to get the original data you have to calculate the original input from the two char, ('4','1'= 0x41=65). The vector is half the size of the char*.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.