Compress Mat into Jpeg And save the result into memory
Asked Answered
R

2

21

I know that the function cv::imwrite can compress a cv::Mat into Jpeg and save it to a file. But now I want to save it into memory, like a array of uchar. So, I can send the array to another one ,and it can write the data into a jpeg file. Is there any one can help me out?

Reasonable answered 5/11, 2015 at 1:27 Comment(0)
I
31

Since You did not specify a programming language. I am going to give you the answer in C++.

    std::vector<uchar> buff;//buffer for coding
    std::vector<int> param(2);
    param[0] = cv::IMWRITE_JPEG_QUALITY;
    param[1] = 80;//default(95) 0-100
    cv::imencode(".jpg", mat, buff, param);
Inexhaustible answered 5/11, 2015 at 7:7 Comment(8)
I have trie this,but if i write buff into a jpeg file,i cant't open the file as a jpeg picture.When i tried to open it ,a error occured.Reasonable
with jpg you cant use for example 'imshow'.. you have to 'decode' it first.Inexhaustible
if he writes the received data as byte stream to some file, I think it would work. The point is " DO NOT EVER deal with JPEG stream as an image. It is NOT an imageInexhaustible
When you say image,do you mean a picture file or data of Mat type?Reasonable
sorry I meant Mat or raw image data or bitmapInexhaustible
no. jpg contain header infotmaion inside it but it is a compressed file. for example if you open a zip file in notpad what gonna you see?? a rubbish text. right? even if the original file is a nice poem before compressing. same for jpg.. you can conseider a bmp file is like txt file and jpg file is like the .zip file of that txtInexhaustible
So if i use` imencode(".jpg,mat ,buff1 ,param")` to compress Mat into buff1 ,and i also use fopenandfreadfunciton to read data from a nomal jpg file named a.jpg into buff2.What's the difference between this two kinds of data int buff1 and buff2?Reasonable
Theoretically they are the same. In practise you gonna find some differences because of the compression algorithm and the compression ratioInexhaustible
D
1

If your choice of programming language is Java, then use:

Highgui.imencode("jpg", mat, buffer).

Deterioration answered 24/3, 2019 at 6:9 Comment(2)
OP's question is tagged c++Figureground
how to do in javascript or typescript?Flirtatious

© 2022 - 2024 — McMap. All rights reserved.