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.
Hence here images should be of the same size.
For e.g if the pixel value of two gray-scale images 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 seem dark 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://opencvhub.blogspot.in/2015/06/accessing-pixel-value-of-image-using-vec3b-function-obtain-coordinates-get-location-point-display-opencv.html
Note:We can merge more than 2 images also.
The code for merging/blending the two images are as shown below:
Input Images:
For 3 images:
The modified code of merging the images is as shown below, where we have taken 3 images of blue,green and red color and mixed them which other:
Note:Here 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
Input Image:
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
Output:
Think:
By including a for loop we can achieve a smooth transition effect between two images.Can't We?
Here is the code for it:
Here is the Output:
You could note the smooth blending between the two images
Hence here images should be of the same size.
For e.g if the pixel value of two gray-scale images 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 seem dark 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://opencvhub.blogspot.in/2015/06/accessing-pixel-value-of-image-using-vec3b-function-obtain-coordinates-get-location-point-display-opencv.html
Note:We can merge more than 2 images also.
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 Image:
The modified code of merging the images is as shown below, where we have taken 3 images of blue,green and red color and mixed them which other:
Note:Here 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, src3,src4, src5, src6, src7;
/// Read image ( same size, same type )
src1 = imread("C:\\Users\\arjun\\Desktop\\red.jpg");
src2 = imread("C:\\Users\\arjun\\Desktop\\green.jpg");
src3 = imread("C:\\Users\\arjun\\Desktop\\blue.jpg");
//Merging two images
src4=src1 + src2;
src5=src2 + src3;
src6=src1 + src3;
src7=src1 + src2 + src3;
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; }
if( !src4.data ) { printf("Error loading src1 \n"); return -1; }
if( !src5.data ) { printf("Error loading src1 \n"); return -1; }
if( !src6.data ) { printf("Error loading src1 \n"); return -1; }
if( !src7.data ) { printf("Error loading src1 \n"); return -1; }
//src4 = imwrite( "C:\\Users\\arjun\\Desktop\\new1.jpg",src4);
//src5 = imwrite( "C:\\Users\\arjun\\Desktop\\new2.jpg",src5);
//src6 = imwrite( "C:\\Users\\arjun\\Desktop\\new3.jpg",src6);
//src7 = imwrite( "C:\\Users\\arjun\\Desktop\\new4.jpg",src7);
//src4 = imread("C:\\Users\\arjun\\Desktop\\new1.jpg");
//src5 = imread("C:\\Users\\arjun\\Desktop\\new2.jpg");
//src6 = imread("C:\\Users\\arjun\\Desktop\\new3.jpg");
//src7 = imread("C:\\Users\\arjun\\Desktop\\new4.jpg");
/// Create Windows
namedWindow("First Image", 1);
imshow( "First Image", src1 );
namedWindow("Second Image", 1);
imshow( "Second Image", src2 );
namedWindow("Third Image", 1);
imshow( "Third Image", src3 );
namedWindow("Blend1 Image", 1);
imshow( "Blend1 Image", src4 );
namedWindow("Blend2 Image", 1);
imshow( "Blend2 Image", src5 );
namedWindow("Blend3 Image", 1);
imshow( "Blend3 Image", src6 );
namedWindow("Blend4 Image", 1);
imshow( "Blend4 Image", src7 );
waitKey(0);
return 0;
}
Input Image:
Red
Green
Blue
Red+Green
Green+Blue
Red+Blue
Multiplying Pixel Value 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:
Think:
By including a for loop we can achieve a smooth transition effect between two images.Can't We?
Here is the code for it:
#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;
}
Here is the Output:
You could note the smooth blending between the two images










Very nice blog..with detailed description
ReplyDeleteHello I want to Assemble 7 bit plane into it's original image how I can please help me
ReplyDelete