Sunday, 10 April 2016

Accessing all the pixels of an Image

To access full pixel value of an image,
We can use :
    Vec3b imagepixel = image.at(x,y);
in for loop to change the value of the co-ordinates (x,y) to cover each row and column.


/*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; 
    //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;  
     } 
 
 
     //for loop for counting the number of rows and columns and displaying the pixel value at each point
     for (int i = 0; i < image.rows; i++) 
       { 
         for (int j = 0; j < image.cols; j++) 
           { 
            Vec3b imagepixel = image.at<Vec3b>(i, j);
            cout<<imagepixel ;   //Displaying the pixel value  of the whole image
            } 
       }

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

     waitKey(0);
     return 0;
  }


Input:


Output:



What will happen if we put ,
     cout<<image;
Does it prints the whole pixel array of an image?*


/*Displaying the Pixel value of the whole Image*/
#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;  
     } 

    //Displaying the pixel value  of the whole image
    cout<<image ;

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

    waitKey(0);
    return 0;
 }

Input:


Output:



Notice the difference in output of both the pixel arrays

No comments:

Post a Comment