Wednesday, 20 April 2016

OpenCV Image Blending Tutorial

Image are basically matrices of pixel values.Thus Image Blending or Image Merging in layman terms simply means that adding the pixel values at a particular co-ordinates of two images.


Note:-Images should be of the same size.


For e.g if the pixel value of two gray-scale images at a particular location are 120 and 35 respectively.Then after blending the pixel value at that particular co-ordinate would become 155.


Note:
Grayscale image is the one where each pixel is stored as a single byte(8 bits). Thus the pixel values can range from 0 to 255. Where 0 denotes black and 255 denotes white.

So,What would happen if the pixel value of the two gray-scale images when merged exceed 255?
For e.g
Let the pixel value at the particular co-ordinate is 250 (would appear white)and that of the other image at the same co-ordinate is 120 (would appear dark). After merging it would appear white because the pixel value at the respective co-ordinate of the merged image would be 255.Since 120+240>255)

Thus the lowest value of the pixel is 0. So even if we multiply a pixel value by -1.
The pixel value of the modified image would be 0.
i.e. If the pixel value at a particular co-ordinate is 255 (white) and if we multiply it by -1.Then the pixel at that point of image would become 0 (black).

We can check the above concept by accessing the pixel value of the merged image at a particular point.
Refer:
http://opencv-code.blogspot.in/2016/12/how-to-access-extract-pixel-value-particular-location-image.html


The code for merging/blending the two images are as shown below:
#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <iostream>
 
 
using namespace cv;
using namespace std;
 
int main()
{
  
 Mat src1, src2, src3;
 /// Read image ( same size, same type )
 src1 = imread("C:\\Users\\arjun\\Desktop\\red.jpg");
 src2 = imread("C:\\Users\\arjun\\Desktop\\green.jpg");
  
 ///Comparing whether the two images are of same size or not
 int width1 , width2 , height1 , height2;
 width1 =src1.cols; 
 height1=src1.rows; 
 width2 =src2.cols; 
 height2=src2.rows; 
  
 if (width1!=width2 && height1!=height2)
 {
  printf("Error:Images must be of the same size \n");
  return -1;
 }
  
 //Merging two images
 src3=src1 + src2;
 
 if( !src1.data ) { printf("Error loading src1 \n"); return -1; }
 if( !src2.data ) { printf("Error loading src2 \n"); return -1; }
 if( !src3.data ) { printf("Error loading src1 \n"); return -1; }
   
 /// Create Windows
 namedWindow("First Image", 1);
 imshow( "First Image", src1 );
 
 namedWindow("Second Image", 1);
 imshow( "Second Image", src2 );
 
 namedWindow("Blend1 Image", 1);
 imshow( "Blend1 Image", src3 );
 
 waitKey(0);
 return 0;
}


Input Images:
Red

Green

Output:


Multiplying Pixel Value By -1:

What would happen if we multiply the pixels by -1?
Since the minimum value of the pixel can be 0.Thus whole of the image would appear black.
Note:Here again we have assumed that the images are of same size.Hence we have not included the code for comparing the size of images which are to be merged.
#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <iostream>
 
 
using namespace cv;
using namespace std;
 
int main()
{
  
 Mat src1, src2;
 /// Read image ( same size, same type )
 src1 = imread("C:\\Users\\arjun\\Desktop\\red.jpg");
  
 //Merging two images
 src2=src1 * (-1);
  
 if( !src1.data ) { printf("Error loading src1 \n"); return -1; }
 if( !src2.data ) { printf("Error loading src2 \n"); return -1; }
  
 //src2 = imwrite( "C:\\Users\\arjun\\Desktop\\new1.jpg",src2);
 //src2  = imread("C:\\Users\\arjun\\Desktop\\new1.jpg");
 
 /// Create Windows
 namedWindow("First Image", 1);
 imshow( "First Image", src1 );
 
 namedWindow("Second Image", 1);
 imshow( "Second Image", src2 );
 
 waitKey(0);
 return 0;
}

Output:


Dont you think by including a for loop we can achieve a smooth transition effect between two images.
#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <iostream>
 
 
using namespace cv;
using namespace std;
 
int main()
{
  
 Mat src1, src2,src3;
 /// Read image ( same size, same type )
 src1 = imread("C:\\Users\\arjun\\Desktop\\red.jpg");
 src2 = imread("C:\\Users\\arjun\\Desktop\\green.jpg");
 
 //Checking whether images are loaded or not
 if( !src1.data ) { printf("Error loading src1 \n"); return -1; }
 if( !src2.data ) { printf("Error loading src2 \n"); return -1; }
 
 //Merging two images
 for (double i=0; i<=255;i=i+5)
 {
 src3=(i*src1)/255 + ((255-i)*src2)/255;
 
 /// Create Windows
 namedWindow("Blend Image", 1);
 imshow( "Blend Image", src3 );
  
 waitKey(250);
 }
 return 0;
}

No comments:

Post a Comment