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?
Asked
Active
Viewed 1.3k times
20

Humam Helfawi
- 19,566
- 15
- 85
- 160

charlie
- 267
- 1
- 3
- 15
2 Answers
30
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);

Miki
- 40,887
- 13
- 123
- 202

Humam Helfawi
- 19,566
- 15
- 85
- 160
-
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. – charlie Nov 05 '15 at 07:42
-
with jpg you cant use for example 'imshow'.. you have to 'decode' it first. – Humam Helfawi Nov 05 '15 at 07:47
-
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 image – Humam Helfawi Nov 05 '15 at 07:58
-
When you say image,do you mean a picture file or data of Mat type? – charlie Nov 05 '15 at 08:03
-
sorry I meant Mat or raw image data or bitmap – Humam Helfawi Nov 05 '15 at 08:07
-
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 txt – Humam Helfawi Nov 05 '15 at 08:21
-
So if i use` imencode(".jpg,mat ,buff1 ,param")` to compress Mat into buff1 ,and i also use `fopen`and`fread`funciton 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? – charlie Nov 05 '15 at 08:46
-
1Theoretically they are the same. In practise you gonna find some differences because of the compression algorithm and the compression ratio – Humam Helfawi Nov 05 '15 at 08:57
1
If your choice of programming language is Java, then use:
Highgui.imencode("jpg", mat, buffer)
.

Inder
- 3,711
- 9
- 27
- 42

sibendu sankar Das
- 49
- 3