Showing posts with label RGB. Show all posts
Showing posts with label RGB. Show all posts

Thursday, 5 May 2016

OpenCV C++ Code for Split and Merge -II

In the previous tutorial we split the R,G,B channels of a color image using opencv's function called split().


Refer this article:
http://opencv-code.blogspot.in/2016/12/how-to-split-color-images-merge-single-channel-images-opencv-tutorials.html


First of all why there is a need to split the channels of a color image?
As explained in the previous articles it helps us to guess the individual contribution of the respective channel in the color image.Also it has other application like object detection based on color recogntion i.e we can select a green object from the background and track it.


So the process of splitting color images without using split() function in opencv is as shown below:

// OpenCV Channel Splitting  Tutorial 
#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <iostream>
 
using namespace cv;
using namespace std;
 
int main()
{
 const float pi=3.14;
 Mat src1,src2,src3,src4,src5;
 src1 = imread("C:\\Users\\arjun\\Desktop\\opencv.png",CV_LOAD_IMAGE_COLOR);
 src2 = Mat::eye(src1.rows,src1.cols, CV_8UC1);
 src3 = Mat::eye(src1.rows,src1.cols, CV_8UC1);
 src4 = Mat::eye(src1.rows,src1.cols, CV_8UC1);
  
 if( !src1.data ) { printf("Error loading src1 \n"); return -1;}
 
for (int i=0; i<src1.cols ; i++){
for (int j=0 ; j<src1.rows ; j++)
 { 
Vec3b color1 = src1.at<Vec3b>(Point(i,j));
Scalar color2 = src2.at<uchar>(Point(i,j));
Scalar color3 = src3.at<uchar>(Point(i,j));
Scalar color4 = src4.at<uchar>(Point(i,j));
 
      color2.val[0]=color1.val[0]; //Blue channel
    
   color3.val[0]=color1.val[1];  //Green Channel
 
   color4.val[0]=color1.val[2];  //Red Channel
     
   src2.at<uchar>(Point(i,j)) = color2.val[0];
   src3.at<uchar>(Point(i,j)) = color3.val[0];
   src4.at<uchar>(Point(i,j)) = color4.val[0];
  }
 }
namedWindow("Original Image",CV_WINDOW_AUTOSIZE); 
imshow("Original Image", src1);
 
namedWindow("Red Channel Image",CV_WINDOW_AUTOSIZE); 
imshow("Red Channel Image", src4);
imwrite("C:\\Users\\arjun\\Desktop\\opencv-red.png",src4);
 
namedWindow("Green Channel Image",CV_WINDOW_AUTOSIZE); 
imshow("Green Channel Image", src3); 
imwrite("C:\\Users\\arjun\\Desktop\\opencv-green.png",src3);
 
namedWindow("Blue Channel Image",CV_WINDOW_AUTOSIZE); 
imshow("Blue Channel Image", src2); 
imwrite("C:\\Users\\arjun\\Desktop\\opencv-blue.png",src2);
 
 waitKey(0);
 return 0;
}



Input:


Output:
Red:

Green:

Blue:



Similarly the process of merging the channels in opencv again so that only individual color channels are displayed can be done as:
// OpenCV Channel Merging  Tutorial 
#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <iostream>
 
using namespace cv;
using namespace std;
 
int main()
{
 const float pi=3.14;
 Mat src1,src2,src3,src4,src5;
 src1 = imread("C:\\Users\\arjun\\Desktop\\opencv.png",CV_LOAD_IMAGE_COLOR);
 src2 = Mat::eye(src1.rows,src1.cols, CV_8UC3);
 src3 = Mat::eye(src1.rows,src1.cols, CV_8UC3);
 src4 = Mat::eye(src1.rows,src1.cols, CV_8UC3);
  
 if( !src1.data ) { printf("Error loading src1 \n"); return -1;}
 
for (int i=0; i<src1.cols ; i++){
for (int j=0 ; j<src1.rows ; j++)
 { 
Vec3b color1 = src1.at<Vec3b>(Point(i,j));
Vec3b color2 = src2.at<Vec3b>(Point(i,j));
Vec3b color3 = src3.at<Vec3b>(Point(i,j));
Vec3b color4 = src4.at<Vec3b>(Point(i,j));
 
      color2.val[0]=color1.val[0]; //Blue channel
   color2.val[1]=0;
   color2.val[2]=0;
 
   color3.val[0]=0;             //Green Channel
   color3.val[1]=color1.val[1];
   color3.val[2]=0;
 
   color4.val[0]=0;             //Red Channel
   color4.val[1]=0;
   color4.val[2]=color1.val[2];
     
   src2.at<Vec3b>(Point(i,j)) = color2;
   src3.at<Vec3b>(Point(i,j)) = color3;
   src4.at<Vec3b>(Point(i,j)) = color4;
  }
 }
namedWindow("Original Image",CV_WINDOW_AUTOSIZE); 
imshow("Original Image", src1);
 
namedWindow("Red Channel Image",CV_WINDOW_AUTOSIZE); 
imshow("Red Channel Image", src4);
imwrite("C:\\Users\\arjun\\Desktop\\opencv-red.png",src4);
 
namedWindow("Green Channel Image",CV_WINDOW_AUTOSIZE); 
imshow("Green Channel Image", src3); 
imwrite("C:\\Users\\arjun\\Desktop\\opencv-green.png",src3);
 
namedWindow("Blue Channel Image",CV_WINDOW_AUTOSIZE); 
imshow("Blue Channel Image", src2); 
imwrite("C:\\Users\\arjun\\Desktop\\opencv-blue.png",src2);
 
 waitKey(0);
 return 0;
}



Input:

Output:
Red:

Green:

Blue:



Note the difference between the two codes:
In channel splitting we have taken 8UC1 i.e a 8 bit unsigned single channel image.
In channel merging we have taken 8UC3 i.e a 8 bit unsigned three channel image.

Wednesday, 30 March 2016

Accessing Pixel Value at a Location(x,y)

Let us consider a 3 channel image of BGR color ordering
(The BGR color ordering is the default order returned  by imread)
Here the order of the channel is reverse

(We generally use RGB color model while describing about an image.In BGR the color model is same except the order of the channel is reverse)


We use :
Vec3b imagepixel = image.at(x,y);


/*Reading the pixel value of an image at a particular location*/
#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp>  
#include <iostream> 
 
  using namespace std;  
  using namespace cv;  
 
  int main() 
  {  
    Mat image; 
    //Reading the color image 
    image = imread("C:\\Users\\arjun\\Desktop\\image003.png", CV_LOAD_IMAGE_COLOR);  
 
    //If image not found
     if (!image.data)                                                                          
     {  
      cout << "No image data \n";  
      return -1;  
     } 
    
     
     //Reading pixel value at location (i,j)
     Vec3b imagepixel = image.at<Vec3b>(250,500);

     //Displaying the pixel value  
     cout<<"imagepixel(BGR)="<<imagepixel<<"\n" ;  
        
     //Display the original image
     namedWindow("Display Image");               
     imshow("Display Image", image);  
  
     waitKey(0);
     return 0;
   }



Input:


Output:





/*Reading the pixel value of an image at a particular location*/
 
 
#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp>  
#include <iostream> 
 
  using namespace std;  
  using namespace cv;  
 
int main() 
  {  
    Mat image; 
    //Reading the color image 
    image = imread("C:\\Users\\arjun\\Desktop\\image003.png", CV_LOAD_IMAGE_COLOR);  
 
     //If image not found  
       if (!image.data)                                                             
     {  
      cout << "No image data \n";  
      return -1;  
     } 
 
 
    while(1)
    {
     //Taking inputs from the user for the co-ordinates of the image 
      int i,j;
      cout<<"Enter the co-ordinates of the image where you want to find the pixel value (i,j): \n";
      cout<<"i<"<<image.rows<<"\t"<<"&"<<"\t"<<"j<"<<image.cols<<"\n";
      
     cout<<"i= ";  cin>>i;
     cout<<"j= ";  cin>>j;
     
     if(i < image.rows) 
      { 
        if(j < image.cols)
          {
           //Reading pixel value at location (i,j)
            Vec3b imagepixel = image.at<Vec3b>(i,j); 
           //Displaying the pixel value                                                        
           cout<<"imagepixel(BGR)="<<imagepixel<<"\n" ;
          }  
        }
      else
        { 
           cout<<"Image Co-ordinates value out of range \n"; 
        }
 
     }
        return 0; 
  }


Input:


Output:



Wednesday, 20 January 2016

RGB to GrayScale Conversion

In OpenCV we can convert an RGB image into Grayscale by two ways:
1.  By using cvtColor function.

2.  By using imread function, where the first parameter specifies the image name while the second parameter specifies the format in which we need to add the image.
Thus there can be various formats like:
a. CV_LOAD_IMAGE_UNCHANGED (<0) :loads the image as is (including the alpha channel if present).
b. CV_LOAD_IMAGE_GRAYSCALE ( 0) :loads the image as an intensity one.
c. CV_LOAD_IMAGE_COLOR (>0) :loads the image in the RGB format.



//Opencv C++ Code for RGB to GreyScale Conversion
#include "opencv2/core/core.hpp"  
#include "opencv2/highgui/highgui.hpp"  
#include "iostream"  
 
  using namespace std;  
  using namespace cv;  
 
  int main() 
  {  
   Mat image;  
   //Reading the color image  
   image = imread("C:\\Users\\arjun\\Desktop\\image003.png", CV_LOAD_IMAGE_COLOR); 
 
   //If image not found  
   if (!image.data) 
     {  
      cout << "No image data \n";  
      return -1;  
     }  
 
   //Converting the Image into GrayScale and Storing it in a Matrix 'img_gray'
   Mat img_gray;
   img_gray = imread("C:\\Users\\arjun\\Desktop\\image003.png",CV_LOAD_IMAGE_GRAYSCALE);
    
    
   //Display the original image  
   namedWindow("Display Original Image",CV_WINDOW_AUTOSIZE);  
   imshow("Display Original Image", image);  
 
   //Display the grayscale image 
   namedWindow("Display Grayscale Image",CV_WINDOW_AUTOSIZE); 
   imshow("Display Grayscale Image", img_gray);  
 
   //Save the grayscale image with a name 'gray.jpg'
   imwrite("C:\\Users\\arjun\\Desktop\\gray.jpg",img_gray);
 
   waitKey(0);  
   return 0;  
  } 


Input:



Output: