Sunday, 15 November 2015

Creating Image from a Matrix

Ever wondered how to create an image of a specified matrix?
We know that we can get the matrix of the image file,since an image is just an array of pixels.
Similarly we can create an image by specifying the matrix and corresponding values for it.
Thus depending upon the values inserted in the matrix the image would change.

Here is the code below:

#include <opencv2/core/core.hpp>
#include <iostream>
#include <opencv2/highgui/highgui.hpp> 

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    Mat img(4,5, CV_8UC3,Scalar(125,0,255));
    cout << "img = \n " << img << "\n\n";
 img = imwrite( "C:\\Users\\arjun\\Desktop\\newpic.jpg",img);
    img =imread("C:\\Users\\arjun\\Desktop\\newpic.jpg",CV_LOAD_IMAGE_COLOR);  
 waitKey(-1);
    return 0;
}

Output Matrix:
opencv zeroes matrix

CV_8UC3:
8U denoted 8 bit long unsigned characters
C3 denotes that there are 3 channels of an image

Note:
Any primitive type from the list can be defined by an identifier in the form CV_<bit-depth>{U|S|F}C(<number_of_channels>)
where U is unsigned integer type, S is signed integer type, and F is float type.

We can even modify the row from 4 to 6.The syntax goes as shown below:
Mat img(6,5, CV_8UC3, Scalar(125,0,255));

To see how the image created from the matrix would look, we increase the size of the matrix.
Thus making no. of rows as 600 and columns as 500.
Here is the code below:
#include <opencv2/core/core.hpp>
#include <iostream>
#include <opencv2/highgui/highgui.hpp> 

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    Mat img(600,500, CV_8UC3,Scalar(125,0,255));
    cout << "img = \n " << img << "\n\n";
 img = imwrite( "C:\\Users\\arjun\\Desktop\\newpic.jpg",img);
    img =imread("C:\\Users\\arjun\\Desktop\\newpic.jpg",CV_LOAD_IMAGE_COLOR);  
 waitKey(-1);
    return 0;
}

The output image would look like:
opencv creating image from matrix

Similarly we can make the smallest 3 channel image of 1*1.By specifying the row attribute as 1 and column as 1 and Scalar(0,0,0).
Mat img(1,1,CV_8UC3,Scalar(0,0,0));
Note : The size of the formed jpeg image is just 631 bytes.Which is the smallest size of the 3 channel(RGB) jpeg image.
Here is the output image link:

Similary we can create the smallest size single channel jpeg image of size 1*1 pixel, by changing 8UC3 to 8UC1.Thus,
Mat img(1,1,CV_8UC1,Scalar(0,0,0));
Note: The size of the formed single channel jpeg image is just 333 bytes.
Here is the output image link:
https://drive.google.com/file/d/0B9Mnn6QWcwVnX3E4Qmo0d2k1TkU/view?usp=sharing

If we doesnt specify the scalar value.The default value is 205.
i.e Mat img(1,1,CV_8UC3);
Code:
#include <opencv2/core/core.hpp>
#include <iostream>
#include <opencv2/highgui/highgui.hpp> 

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    Mat img(4,5, CV_8UC3);
    cout << "img = \n " << img << "\n\n";
 waitKey(0);
    return 0;
}
Here is the pic of the output matrix:
opencv matrix default values

This technique is immensely used in image steganography where the message is hidden in matrix form and an image is made out of it. 

No comments:

Post a Comment