Ask Your Question
1

Android imencode parameters

asked 2012-09-26 09:51:09 -0600

Rui Marques gravatar image

updated 2012-09-26 10:09:57 -0600

The way to set, for example, the JPEG quality with imencode with C++ is something like this:

vector<int> params;
params.push_back( cv::IMWRITE_JPEG_QUALITY );
params.push_back( 75 );
cv::imencode( ".jpg", frame, buf, params );

The question is, how to do it properly with OpenCV4Android, is this the best way?

//I am not sure which size of rows/cols should i initialize this Mat: 
MatOfInt params = new MatOfInt(size); 
int[] data = {Highgui.IMWRITE_JPEG_QUALITY, 40};
params.put(0, 0, data);
Highgui.imencode(".jpg", image, buf, params);

And also, because imencode returns buf as a MatOfByte with a single row, how do I convert it so it has the same dimensions of input image?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2012-09-27 10:08:55 -0600

Andrey Pavlenko gravatar image

Look at the following test:

public void testImencodeStringMatListOfByteListOfInteger() {
    MatOfInt  params40 = new MatOfInt(Highgui.IMWRITE_JPEG_QUALITY, 40);
    MatOfInt  params90 = new MatOfInt(Highgui.IMWRITE_JPEG_QUALITY, 90);
    /* or
    MatOfInt  params40 = new MatOfInt();
    params40.fromArray(Highgui.IMWRITE_JPEG_QUALITY, 40);
    */
    MatOfByte buff40 = new MatOfByte();
    MatOfByte buff90 = new MatOfByte();

    assertTrue( Highgui.imencode(".jpg", rgbLena, buff40, params40) );
    assertTrue( Highgui.imencode(".jpg", rgbLena, buff90, params90) );

    assertTrue(buff40.total() > 0);
    assertTrue(buff40.total() < buff90.total());
}

As for the question: "And also, because imencode returns buf as a MatOfByte with a single row, how do I convert it so it has the same dimensions of input image?" - the imencode() function returns an JPEG-encoded stream; do you expect it to be the same size at the original image?

edit flag offensive delete link more

Comments

No, but i expect it to have the same width and height as the original. The question was how do I get from that MatOfByte to a Mat with the same width and height of the original.

Rui Marques gravatar imageRui Marques ( 2012-09-27 15:54:32 -0600 )edit

I guess the only way is to do Highgui.imdecode.

Rui Marques gravatar imageRui Marques ( 2012-09-28 03:43:20 -0600 )edit

JPEG-encoded stream size is not directly related to original image size; treat it as a byte[], not as a 2D data.

Andrey Pavlenko gravatar imageAndrey Pavlenko ( 2012-09-28 09:57:05 -0600 )edit

Question Tools

Stats

Asked: 2012-09-26 09:51:09 -0600

Seen: 4,101 times

Last updated: Sep 27 '12