Wednesday 3 June 2015

Reason for increase in the size of image as i save it using OpenCV

Why does the size of the image increases when i save the same image again with a different name in OpenCV?
Solution:
There exists various methods of compression in JPEG.
So your OpenCV used a different compression technique for jpeg image than that used by an original image.
By Default OPENCV uses the compression number of 95 while dealing with JPEG images.
The higher the number , lesser compression we would obtain.
To manually pass the compression factor refer the code below:
Note: Here i have used the compression number of 25 and 100 as the compression number for JPEG image
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;


int main(int argc, char **argv)
{
    int p[3];
    IplImage *img = cvLoadImage("C:\\Users\\arjun\\Desktop\\aa.jpg");

    p[0] = CV_IMWRITE_JPEG_QUALITY;
    p[1] = 25;
    p[2] = 0;
    cvSaveImage("C:\\Users\\arjun\\Desktop\\image1.jpg", img, p);

    p[0] = CV_IMWRITE_JPEG_QUALITY;
    p[1] = 100;
    p[2] = 0;
    cvSaveImage("C:\\Users\\arjun\\Desktop\\image2.jpg", img, p);

    exit(0);
}
Check the desktop for the two images,"image1.jpg" and "image2.jpg".
Compare the size of two images

Proof that opencv doesnt increase the file size but uses different compression factor:
Try this Code:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
IplImage* image1 = cvLoadImage("C:\\Users\\arjun\\Desktop\\a.jpg", 0);
cvSaveImage("C:\\Users\\arjun\\Desktop\\1a.jpg", image1);
IplImage* image2 = cvLoadImage("C:\\Users\\arjun\\Desktop\\reTest.jpg",0);
cvSaveImage("C:\\Users\\arjun\\Desktop\\a2.jpg", image2);

    waitKey(0);                        // Wait for a keystroke in the window
    return 0;
}
Task:
Here I have loaded the Image 'a.jpg' and saved it with a name 'a1.jpg'.And again  the image 'a1.jpg' is being loaded and saved  with a different name 'a2.jpg'.
And then the size of the image 'a1.jpg' is compared with 'a2.jpg'.

Observation:
We found that size of both images are same.

Conclusion:
Hence,OpenCv doesnt increases the size of the image.But rather uses a different compression number for compressing the image.
Refer for more information:

No comments:

Post a Comment