Tuesday 9 June 2015

Changing or Modifying the pixel value at a particular point of an image using opencv

This tutorial is about how to change the pixel value of an image at a particular co-ordinate:


Here is the code below:

/*Displaying the Pixel value of the whole Image using Loops*/

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

  using namespace std;  
  using namespace cv;  

int main() 
  {  
    Mat image1,image2; 
 //Reading the color image 
    image1 = imread("C:\\Users\\arjun\\Desktop\\image003.png", CV_LOAD_IMAGE_COLOR);  

 //If image1 not found 
 if (!image1.data)                                                                          
    {  
     cout << "No image data \n";  
     return -1;  
    } 

 namedWindow("Original Image");               //Display the original image
    imshow("Original Image", image1);

 //Changing the pixel value at just a particular point(100,200)
  Vec3b color = image1.at<Vec3b>(Point(100,200));
      color.val[0] = 100;
      color.val[1] = 0;
      color.val[2] = 0;
 image1.at<Vec3b>(Point(100,200)) = color;

   //Save the modified image
      imwrite("C:\\Users\\arjun\\Desktop\\mod_image.png",image1);
      //Reading the modifed image
      image2 = imread("C:\\Users\\arjun\\Desktop\\mod_image.png", CV_LOAD_IMAGE_COLOR);  

   //If image2 not found 
       if (!image2.data)                                                                          
       {  
        cout << "No image data \n";  
        return -1;  
       } 

    namedWindow("Modified Image");               //Display the modified image
    imshow("Modified Image", image2); 
 waitKey(0);
 return 0;
   }


Link of the Code:

Original Image:
modifying-pixel-value-at-a-point-original-image-opecv

Modified Image:
changing-pixel-value-at-a-point-modified-image-opencv

Note:
We can see the difference by subtracting the two images:
The code for subtracting the two images is shown below:
Here i have stored the original image in the variable called 'image'.

/*Displaying the Pixel value of the whole Image using Loops*/
#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp>  
#include <iostream> 

  using namespace std;  
  using namespace cv;  

int main() 
  {  
    Mat image,image1,image2; 
 //Reading the color image 
 image = imread("C:\\Users\\arjun\\Desktop\\image003.png", CV_LOAD_IMAGE_COLOR);  
    image1 = imread("C:\\Users\\arjun\\Desktop\\image003.png", CV_LOAD_IMAGE_COLOR);  

 //If image1 not found 
 if (!image1.data)                                                                          
    {  
     cout << "No image data \n";  
     return -1;  
    } 

 namedWindow("Original Image");               //Display the original image
    imshow("Original Image", image1);

 //Changing the pixel value at just a particular point(100,200)
  Vec3b color = image1.at<Vec3b>(Point(100,200));
      color.val[0] = 100;
      color.val[1] = 0;
      color.val[2] = 0;
 image1.at<Vec3b>(Point(100,200)) = color;

   //Subtracting the two image
 image1=image-image1;
 //Save the modified image
    imwrite("C:\\Users\\arjun\\Desktop\\mod_image.png",image1);
    //Reading the modifed image
    image2 = imread("C:\\Users\\arjun\\Desktop\\mod_image.png", CV_LOAD_IMAGE_COLOR);  

   //If image2 not found 
       if (!image2.data)                                                                          
       {  cout << "No image data \n";  
         return -1; } 

    namedWindow("Modified Image");               //Display the modified image
    imshow("Modified Image", image2); 
 waitKey(0);
 return 0;
   } 
Link of the Code:
https://drive.google.com/file/d/0B9Mnn6QWcwVnTTd1OEhVUEF2S3M/view?usp=sharing


Original Image:

Modified Image:
Notice the white dot in the black image.

No comments:

Post a Comment