Sunday 10 January 2016

Write and Dispay Image in OpenCV

In my previous tutorial i have explained how to read images in OpenCV.
Refer : http://opencv-code.blogspot.in/2016/12/how-to-read-and-display-image-in-opencv.html

This tutorial is about how to read, write and display images in Opencv.



Syntax:
C++:bool imwrite(const string& filename, InputArray img, const vector<int>& params=vector<int>() )
Saves an image to a specified file.

Parameters:           
filename Name of file to be loaded.
image Image to be saved.
params –
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).




//OpenCV C++ code for reading, writing and displaying image
#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);  
    
//Window for displaying both the images
 namedWindow("Image1");
 imshow("Image1",image1);
 
 namedWindow("Image2");
 imshow("Image2",image2);

 waitKey(0);
 
}
Note:- Compare the size of the two images.(opencv-logo.jpg & opencv-logo-new.jpg).
To know the reason about the increase in size of the image refer:
http://opencv-code.blogspot.in/2016/12/why-does-size-of-images-increases-opencv-imwrite.html

No comments:

Post a Comment