Friday, 15 January 2016

Reason for increase in the size of the image

In the previous tutorial we learnt how to save images in OpenCV, Refer:
http://opencv-code.blogspot.in/2016/12/how-to-write-and-dispay-image-in-opencv.html

This tutorial explains the reason behind increase in size of image in 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)
{
     Mat image1;
     const int JPEG_QUALITY = 25;
     const int JPEG_QUALITY2 = 25;

    // Read the file
    image1 = imread("C:\\Users\\arjun\\Desktop\\opencv-logo.jpg",CV_LOAD_IMAGE_COLOR);

    // Check for invalid input
    if(! image1.data )                              
    {
        cout << "Could not open or find the image" << std::endl ;
        return -1;
    } 

    vector<int> params;
    params.push_back(CV_IMWRITE_JPEG_QUALITY);
    params.push_back(JPEG_QUALITY);

    //Write the File
    imwrite( "C:\\Users\\arjun\\Desktop\\opencvlogo-new1.jpg",image1, params);

    vector<int> params2;
    params2.push_back(CV_IMWRITE_JPEG_QUALITY);
    params2.push_back(JPEG_QUALITY2);

    //Write the File
    imwrite( "C:\\Users\\arjun\\Desktop\\opencvlogo-new2.jpg",image1, params2);
  
    //Dispay the Image in the window 
    namedWindow("Image1");
    imshow("Image1",image1);
 
 waitKey(0);
}

Note:- Now compare the size of the original image and the two new images.

No comments:

Post a Comment