Showing posts with label Image. Show all posts
Showing posts with label Image. Show all posts

Monday, 8 June 2015

Accessing pixel value of an image using vec3b function

We want to obtain the pixel value of an image:

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)

1. The code for reading value of the pixel at the co-ordinates(x,y)
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.data)             //If image not found                                                             
     {  
      cout << "No image data \n";  
      return -1;  
     } 
       Vec3b imagepixel = image.at<Vec3b>(250,500); //Reading pixel value at location (i,j)
    cout<<"imagepixel(BGR)="<<imagepixel<<"\n" ; //Displaying the pixel value  
       
        namedWindow("Display Image");               //Display the original image
  imshow("Display Image", image);  
  waitKey(0);
  return 0;
      }

Here is the link of the code for accessing the value of the image at a point:

_____________________________________________________
Output:

output-accessing-pixel-value-at-coordinate

______________________________________________________
Image:

original-image-opencv

_____________________________________________________

2. Code for dynamically entering the co-ordinates of the image by the user:


/*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; 
      }

__________________________________________________________

Here is the link of the code for accessing dynamically pixel value of the image:
 _________________________________________________________
Output:
code-dynamic-access-pixel-value-output-image-opencv

__________________________________________________________
 Image:
original-image-opencv

___________________________________________________________

3. 
Code for accessing the full pixel value of an image just by using cout function:

Here we have used cout;
See the difference between Code 3. and Code4.
Code:
/*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.data)             //If image not found                                                             
     {  
      cout << "No image data \n";  
      return -1;  
     } 
    cout<<image ;              //Displaying the pixel value  of the whole image
    namedWindow("Display Image");               //Display the original image
    imshow("Display Image", image);  
    waitKey(0);
    return 0;
      }
  
___________________________________________________

Here is the link of the code below:
_______________________________________________________________
 Output:

code-pixel-value-of-full-image-cout-output-opencv

___________________________________________________
4. Code for accessing the full pixel value of an image just by using for loop and Vec3b function:
/*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
     } 
   }
     namedWindow("Display Image");               //Display the original image
     imshow("Display Image", image);  
      waitKey(0);
      return 0;
        }


Here we have used for loops along with Vec3b to display the pixel value at each row and column.
_____________________________________________________
Output:
code-pixel-value-of-full-image-for-loop-vec3b-function-output-opencv


____________________________________________________

Here is the link of the code:
____________________________________________________
Note the difference in the output of the two codes.
In the output of the 4th code all the pixel values are represented in BGR format as [ B,G,R] e.g [0,0,255].



Tuesday, 2 June 2015

OpenCV-Image Loading and Saving it using imwrite

This Opencv tutorial is about loading and Saving an Image

Imread Syntax:
Mat imread(const string& filename, int flags=1 )

Parameters:               
filename – Name of file to be loaded.
flags –Flags specifying the color type of a loaded image:

CV_LOAD_IMAGE_ANYDEPTH - If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
CV_LOAD_IMAGE_COLOR - If set, always convert image to the color one
CV_LOAD_IMAGE_GRAYSCALE - If set, always convert image to the grayscale one

>0 Return a 3-channel color image.
=0 Return a grayscale image.
<0 Return the loaded image as is (with alpha channel).

Image Format Supported in OPENCV:
Windows bitmaps - *.bmp, *.dib (always supported)
JPEG files - *.jpeg, *.jpg, *.jpe (see the Notes section)
JPEG 2000 files - *.jp2 (see the Notes section)
Portable Network Graphics - *.png (see the Notes section)
Portable image format - *.pbm, *.pgm, *.ppm (always supported)
Sun rasters - *.sr, *.ras (always supported)
TIFF files - *.tiff, *.tif (see the Notes section)

Imwrite Syntax:
bool imwrite(const string& filename, InputArray img, const vector<int>& params=vector<int>() )
Parameters:               
filename – Name of the file.
image – Image to be saved.
params –Format-specific save parameters encoded as pairs paramId_1, paramValue_1, paramId_2, paramValue_2, ... . The following parameters are currently supported:

For JPEG, it can be a quality ( CV_IMWRITE_JPEG_QUALITY ) from 0 to 100 (the higher is the better). Default value is 95.

For PNG, it can be the compression level ( CV_IMWRITE_PNG_COMPRESSION ) from 0 to 9. A higher value means a smaller size and longer compression time. Default value is 3.

For PPM, PGM, or PBM, it can be a binary format flag ( CV_IMWRITE_PXM_BINARY ), 0 or 1. Default value is 1.

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "iostream"

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
 Mat image1,image2;

// Read the file
image1 = imread("C:\\Users\\arjun\\Desktop\\opencv-logo.jpg",CV_LOAD_IMAGE_COLOR);
// Check for invalid input
 if(! image1.data )                              
    {
        cout << "Could not open or find the image" << std::endl ;
        return -1;
    } 

//Write the File
imwrite( "C:\\Users\\arjun\\Desktop\\opencv-logo-new.jpg",image1);

// Read the Writen File
image2 =imread("C:\\Users\\arjun\\Desktop\\opencv-logo-new.jpg",CV_LOAD_IMAGE_COLOR);  
   

 namedWindow("Image1");
 imshow("Image1",image1);

 namedWindow("Image2");
 imshow("Image2",image2);
 waitKey(0);

}

Here we are first reading the image using: imread() . Note the Location of the image is: C:\Users\arjun\Desktop\a.jpg where "old.jpg" is the name of my image. (The path of the image file can be directly obtained by right clicking it and Selecting Properties. The location field gives the path of the image. Here we append additional "\" after each file or folder name. Now the file can be saved by using the function imwrite() in the desired Location. Here i have set the path as: C:\Users\arjun\Desktop\new.jpg where "new.jpg" is the name of my saved image.
Note: 
Compare the Size of old image and the new image which is saved using imwrite().
Is there any difference of size if yes then why?