Tuesday, 5 April 2016

Modifying a particular pixel value of an Image

In the previous tutorials we learnt how to access a pixel value of a particular co-ordinate,
Refer :
http://opencv-code.blogspot.in/2016/12/how-to-access-extract-pixel-value-particular-location-image.html

This,

OpenCV C++ tutorial

is about accessing and changing the pixel value at a particular co-ordinate of an Image.
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;  
    } 
 
    //Display the original image
    namedWindow("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;  
       } 
 
    //Display the modified image
    namedWindow("Modified Image");               
    imshow("Modified Image", image2); 
    waitKey(0);
    return 0;
   }

Input:


Modified Image:


No comments:

Post a Comment