Search code examples
opencvimage-processinglibjpeg

Convert jpeg image mat to tiff


I'm using openCV and libjpeg is there a way to convert jpeg Black&white image to Tiff, I need to do so without saving the CV::mat to a file?

Thank's


Solution

  • You can use cv::imencode() to take a Mat and encode it into a vector of bytes:

    // bool imencode(const string& ext, InputArray img, vector<uchar>& buf, const vector<int>& params=vector<int>())¶
    
    cv::Mat image = cv::imread("image.jpg");
    std::vector<uchar> buf;
    imencode(".tiff", image, buf);
    

    The TIFF encoded result is in buf.