Showing posts with label for loop. Show all posts
Showing posts with label for loop. 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].



Sunday, 7 June 2015

Reading images sequentially without using videocapture function or displaying image successively using for loop

Reading images using successive frames in OpenCv without using VideoCapture function:
OR
Reading images using a for loop in OpenCV

We want to read images in a folder by using for loop.
Thus we need to number the images sequentially

Here i have renamed the images as image001.png , image002.png etc.

Note:
You just need to rename the image sequentially and provide a proper path to it.
It can be any type of image .bmp , .jpg, .png etc.
For e.g Arjun001.jpg , Arjun002.jpg ,Arjun003.jpg etc
But the name which you are using before the sequence should be given in the path while coding.

Here is the code below:
 Here the path to my image file is C:\Users\arjun\Desktop.

While the name of my image starts from "image" i.e. image001.png
If it was Arjun001.png then the path should have been:
C:\\Users\\arjun\\Desktop\\Arjun

%03i denotes the no of zeros in the name and total size of the string for e.g 001 , 002 , 003, 004 etc
So change your name accordingly if you make changes in the code.
Here %03i denotes there are there digits in the number with the last digit being ‘i’ and preceding it are all zeroes.

Explanation of the code:
//defines the variable 'filename' taking character input of max 100.
char filename[100]

sprintf(filename, "C:\\Users\\arjun\\Desktop\\image,number");
//The sprintf() function prototype is
int sprintf(char *str, const char *format, arg_1, arg_2, arg_3, ... , arg_n);
Composes a string with the same text that would be printed if format was used on printf, but instead of being printed, the content is stored as a C string in the buffer pointed by str.

//The image is loaded by a function cvLoadImage in a variable called 'image'
image = cvLoadImage(filename);

//No. of image =6
 int nImages = 6;

//For loop for concating the path of the image
for (int i = 1; i &lt; nImages; ++i);
 {
  loadImage(im, i);
  char filename[100];
  strcpy(filename, "C:\\Users\\arjun\\Desktop\\image");
   char frameNo[10];
   sprintf(frameNo, "%03i", i);

  strcat(filename, frameNo);
  strcat(filename, ".png");

//Load the image
  IplImage *im = cvLoadImage(filename,CV_LOAD_IMAGE_COLOR);
  cvNamedWindow("pic");        //Name of the image window 
  cvShowImage("pic",im);         //Display the image window
  cvWaitKey(1000);                  //Wait for 1 sec
  }


 Here is the link of the code:
Here is the code:
/* Read images using successive frames in OpenCv without using VideoCapture function*/
#include "stdlib.h"
#include "math.h"
#include "opencv/cv.h"       // include it to used Main OpenCV functions.
#include "opencv/highgui.h"  //include it to use GUI functions.

 using namespace std;
 using namespace cv;

void loadImage(IplImage *image, int number)
  {
    // Store path to directory
    char filename[100];
    sprintf(filename, "C:\\Users\\arjun\\Desktop\\image,number");
    image = cvLoadImage(filename);
  }

  int main(int argc, char* argv[])
  {
   IplImage *im=0;
   int nImages = 6;
   for (int i = 1; i < nImages; ++i)
     {
       loadImage(im, i);
       char filename[100];
       strcpy(filename, "C:\\Users\\arjun\\Desktop\\image");
       char frameNo[10];
       sprintf(frameNo, "%03i", i);
    strcat(filename, frameNo);
       strcat(filename, ".png");
       IplImage *im = cvLoadImage(filename,CV_LOAD_IMAGE_COLOR);
       cvNamedWindow("pic");
       cvShowImage("pic",im);
       cvWaitKey(1000);
     }
  }