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?
Compress Mat into Jpeg And save the result into memory
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);
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 image –
Inexhaustible
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 bitmap –
Inexhaustible
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 –
Inexhaustible
So if i use` imencode(".jpg,mat ,buff1 ,param")` to compress Mat into buff1 ,and i also use
fopen
andfread
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? –
Reasonable Theoretically they are the same. In practise you gonna find some differences because of the compression algorithm and the compression ratio –
Inexhaustible
If your choice of programming language is Java, then use:
Highgui.imencode("jpg", mat, buffer)
.
OP's question is tagged c++ –
Figureground
how to do in javascript or typescript? –
Flirtatious
© 2022 - 2024 — McMap. All rights reserved.