Tuesday, 2 June 2015

OpenCV-Image Loading and Saving it using imwrite

This Opencv tutorial is about loading and Saving an Image

Imread Syntax:
Mat imread(const string& filename, int flags=1 )

Parameters:               
filename – Name of file to be loaded.
flags –Flags specifying the color type of a loaded image:

CV_LOAD_IMAGE_ANYDEPTH - If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
CV_LOAD_IMAGE_COLOR - If set, always convert image to the color one
CV_LOAD_IMAGE_GRAYSCALE - If set, always convert image to the grayscale one

>0 Return a 3-channel color image.
=0 Return a grayscale image.
<0 Return the loaded image as is (with alpha channel).

Image Format Supported in OPENCV:
Windows bitmaps - *.bmp, *.dib (always supported)
JPEG files - *.jpeg, *.jpg, *.jpe (see the Notes section)
JPEG 2000 files - *.jp2 (see the Notes section)
Portable Network Graphics - *.png (see the Notes section)
Portable image format - *.pbm, *.pgm, *.ppm (always supported)
Sun rasters - *.sr, *.ras (always supported)
TIFF files - *.tiff, *.tif (see the Notes section)

Imwrite Syntax:
bool imwrite(const string& filename, InputArray img, const vector<int>& params=vector<int>() )
Parameters:               
filename – Name of the file.
image – Image to be saved.
params –Format-specific save parameters encoded as pairs paramId_1, paramValue_1, paramId_2, paramValue_2, ... . The following parameters are currently supported:

For JPEG, it can be a quality ( CV_IMWRITE_JPEG_QUALITY ) from 0 to 100 (the higher is the better). Default value is 95.

For PNG, it can be the compression level ( CV_IMWRITE_PNG_COMPRESSION ) from 0 to 9. A higher value means a smaller size and longer compression time. Default value is 3.

For PPM, PGM, or PBM, it can be a binary format flag ( CV_IMWRITE_PXM_BINARY ), 0 or 1. Default value is 1.

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "iostream"

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
 Mat image1,image2;

// 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;
    } 

//Write the File
imwrite( "C:\\Users\\arjun\\Desktop\\opencv-logo-new.jpg",image1);

// Read the Writen File
image2 =imread("C:\\Users\\arjun\\Desktop\\opencv-logo-new.jpg",CV_LOAD_IMAGE_COLOR);  
   

 namedWindow("Image1");
 imshow("Image1",image1);

 namedWindow("Image2");
 imshow("Image2",image2);
 waitKey(0);

}

Here we are first reading the image using: imread() . Note the Location of the image is: C:\Users\arjun\Desktop\a.jpg where "old.jpg" is the name of my image. (The path of the image file can be directly obtained by right clicking it and Selecting Properties. The location field gives the path of the image. Here we append additional "\" after each file or folder name. Now the file can be saved by using the function imwrite() in the desired Location. Here i have set the path as: C:\Users\arjun\Desktop\new.jpg where "new.jpg" is the name of my saved image.
Note: 
Compare the Size of old image and the new image which is saved using imwrite().
Is there any difference of size if yes then why?

No comments:

Post a Comment