Wednesday, 30 March 2016

Accessing Pixel Value at a Location(x,y)

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)


We use :
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 not found
     if (!image.data)                                                                          
     {  
      cout << "No image data \n";  
      return -1;  
     } 
    
     
     //Reading pixel value at location (i,j)
     Vec3b imagepixel = image.at<Vec3b>(250,500);

     //Displaying the pixel value  
     cout<<"imagepixel(BGR)="<<imagepixel<<"\n" ;  
        
     //Display the original image
     namedWindow("Display Image");               
     imshow("Display Image", image);  
  
     waitKey(0);
     return 0;
   }



Input:


Output:





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


Input:


Output:



Friday, 25 March 2016

OpenCV C++ Code for Drawing a Chessboard Pattern

This OpenCV Tutorial is about drawing a Chess Board Pattern.
Refer the Code Below:



//Opencv Example of Drawing a Chess Board Pattern
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
  
int main( )
{ 
 int a=400/8;
  // Create black empty images
  Mat image = Mat::zeros( 400, 400, CV_8UC3 );
    
  // Draw a rectangle ( 5th argument is not -ve)  
  for(int y=0;y<=3;y++)
  for(int x=0;x<=3;x++)
  {
  rectangle( image, Point( x*a*2, y*a*2), Point( a*(2*x+1), a*(2*y+1)), Scalar( 255, 255, 255 ), -1, 4 );
  imshow("Image1",image);
  waitKey( 250 );
  }
  for(int y=0;y<=3;y++)
  for(int x=0;x<=3;x++){
  rectangle( image, Point( a*(2*x+1), a*(2*y+1)), Point( (x+1)*a*2, (y+1)*a*2), Scalar( 255, 255,255 ), -1, 4 );
  imshow("Image1",image);
  waitKey( 250 );
  }
  waitKey( 0 );
  return(0);
}



Output:

Sunday, 20 March 2016

Opencv C++ Code for drawing Rectangle

Draws a simple, thick, or filled up-right rectangle.

Syntax:
C++ :void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)

Parameters:
img Image.
pt1 Vertex of the rectangle.
pt2 Vertex of the rectangle opposite to pt1.
rec Alternative specification of the drawn rectangle.
color Rectangle color or brightness (grayscale image).
thickness Thickness of lines that make up the rectangle. Negative values, like CV_FILLED , mean that the function has to draw a filled rectangle.
lineType Type of the line. See the line() description.
shift Number of fractional bits in the point coordinates.



#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
  
int main( )
{    
  // Create black empty images
  Mat image = Mat::zeros( 400, 400, CV_8UC3 );
    
  // Draw a rectangle 
  rectangle(image , Point (100,100), Point (300,300), Scalar( 0, 0, 255 ), 1, 8);
  imshow("Image",image);
  
  waitKey( 0 );
  return(0);
}

Tuesday, 15 March 2016

OpenCV C++ Code for Drawing a Semi-Circle

This Opencv Tutorial is about drawing a Semi-Circle

You might have wondered that how to draw a Semi-Circle in Opencv when we have no direct syntax available for it.
Even in the Syntax of drawing a circle in Opencv, we dont have any such parameters which can be modified for drawing a semicircle.


C++ :void circle(Mat& img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=8, int shift=0)

But, we know that a circle is a special case of an ellipse whose eccentricity is 1. And in the Opencv Ellipse Syntax:

C++ :void ellipse(Mat& img, Point center, Size axes, double angle, double startAngle, double endAngle, const Scalar& color, int thickness=1, int lineType=8, int shift=0)

We can find the parameters like "Start Angle" and "End Angle".
And if we want to draw an Circle From an Ellipse we just need to mention the size of both the axes as same.
Thus,Here is the Opencv Code for drawing a Semi-Circle:



//Opencv C++ Tutorial for drawing a Semi-Circle
#include <opencv2 core.hpp="" core="">
#include <opencv2 highgui.hpp="" highgui="">
using namespace cv;
int main( )
{
 // Create black empty images
 Mat image = Mat::zeros( 500, 500, CV_8UC3 );
 // Draw a ellipse
 for(int i=10;i<=250;i=i+10)
 {
 ellipse( image, Point( 250, 250 ), Size( i, i ), 0, 0, 180, Scalar( 255, 255, 0 ), 2, 8 );
 imshow("Image",image);
 waitKey( 250 );
 }
 waitKey( 0 );
 
 return(0);
}


Output:

Thursday, 10 March 2016

OpenCV C++ Code for drawing a Square Spiral

In the previous tutorial we learn about drawing an Line.
http://opencv-code.blogspot.in/2016/12/how-to-draw-line-opencv-cplusplus-example.html
Thus this opencv tutorial will be an extension of that tutorial with some added mathematical logic for drawing a square spiral.



Here is the Opencv Code Below:
//Drawing a Square Spiral
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
  
int main( )
{   
  int count=0,a,b=250,i,j;
  // Create black empty images
  Mat image = Mat::zeros( 500, 500, CV_8UC3 );
  int p=0;int q=1;
  for(a=250;a<500 && a>0;)
  {
     
   count++;
   if(count%2!=0)
   { 
      p++;
    j=b;
    if(p%2!=0) 
    {i=a+5*count;}
    else
    {i=a-5*count;}
 }
   else
  {
     
      q++;
    i=a;
    if(q%2==0 )
    {j=b+5*count;}
    else
    {j=b-5*count;}
 
    }
    // Draw a line 
  line( image, Point( a, b ), Point( i, j), Scalar( 255, 255, 0 ), 2, 8 );
   
     imshow("Image",image);
     waitKey( 100 );
  a=i;
  b=j;
  
  }
  waitKey( 0 );
  return(0);
}

Output:-

Saturday, 5 March 2016

OpenCV C++ Tutorial for drawing a Star

In the previous tutorial we learnt about drawing a LINE:
http://opencv-code.blogspot.in/2016/12/how-to-draw-line-opencv-cplusplus-example.html

Thus this opencv tutorial will be an extension of that tutorial with some added mathematical logic for drawing a star.

To begin with we first start by drawing a pentagon:
And name the vertex as a ,b ,c ,d, e.
The co-ordinates of which can be obtained by mathematical rules as explained before:

a=( 2*r*cos(36)*cos(72) , x )
b=( x-2*r*cos(36)*cos(72) , x )
c=( x , 2*r*cos(36)*sin(72) )
d=( x/2 , 0 )
e=( 0 , 2*r*cos(36)*sin(72) )




Now, The magic begins. 1. Join vertex a with d. 2. Join vertex d with b. 3. Join vertex b with e. 4. Join vertex e with c. 5. Join vertex c with a.



Here is the opencv code for drawing a Star:
//Opencv Example of drawing a Star 
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;
  
int main( )
{    
  double pi=3.14;
  int a=500/(1+cos(pi*54/180));
  
  // Create black empty images
  Mat image = Mat::zeros( 500, 500, CV_8UC3 );
   
  line( image, Point((a*cos(pi*72/180)), 500),  Point(250, 0), Scalar( 255, 255, 0 ), 2, 8 );
  imshow("Image",image); 
  waitKey( 500 );
 
  line( image, Point(250, 0), Point(500-(a*cos(pi*72/180)),500), Scalar( 255, 255, 0 ), 2, 8 );
  imshow("Image",image); 
  waitKey( 500 ); 
 
  line( image, Point(500-(a*cos(pi*72/180)),500), Point(0, 500-(a*sin(pi*72/180))), Scalar( 255, 255, 0 ), 2, 8 );
  imshow("Image",image); 
  waitKey( 500 );
 
  line( image, Point(0, 500-(a*sin(pi*72/180))), Point( 500, 500-(a*sin(pi*72/180)) ), Scalar( 255, 255, 0 ), 2, 8 );
  imshow("Image",image); 
  waitKey( 500 );
 
  line( image, Point( 500, 500-(a*sin(pi*72/180)) ), Point((a*cos(pi*72/180)), 500), Scalar( 255, 255, 0 ), 2, 8 );
  imshow("Image",image); 
  waitKey( 0 );
  return(0);
}




Output:-

Tuesday, 1 March 2016

OpenCV C++ Code for Drawing a Pentagon

This Opencv C++ Tutorial is about drawing a Pentagon.

In the previous tutorials we learn about drawing a Rectangle and a Line.
To draw the Square we obtained the Co-ordinates of the vertices of the square and then joined those vertices with a Line.

Similarly in order to draw the Pentagon we first need to obtain the Co-ordinates of its Vertices.



Refer the Figure Below:



ϴ=72º (∵ ϴ = 360º/5)
Now, In ∆OBC,
Seg OB=Seg OC;
Thus, m∠OBC=m∠OCB=x;
x+x+72º=180º ( Since Sum of All angles of a Triangle is 180º )
2x=180º - 72º ;
x=54º
i.e. m∠OBC=m∠OCB=54º;
Also,
m∠ABC=108º;
Thus, m∠ABQ=72º; & Seg BC=a;
Seg QB=a*Cos(72º);
∴ QR=QB + BC + CR;
& QB=CR;
Thus, 2*a*Cos(72º) + a =QR
where QR is the Length of the Side of the window.
Here QR=500
Thus a=500/(1+2*Cos(72º));



//Opencv C++ Example for drawing a Pentagon 
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;
  
int main( )
{    double pi=3.14;
 
  //Length of a Side of Regular Pentagon
  int a=500/(1+2*cos(pi*72/180));
  
  // Create black empty images
  Mat image = Mat::zeros( 500, 500, CV_8UC3 );
   
  line( image, Point((a*cos(pi*72/180)), 500), Point(500-(a*cos(pi*72/180)),500), Scalar( 255, 255, 0 ), 2, 8 );
  imshow("Image",image); 
  waitKey( 500 );
  line( image, Point(500-(a*cos(pi*72/180)),500), Point( 500, 500-(a*sin(pi*72/180)) ), Scalar( 255, 255, 0 ), 2, 8 );
  imshow("Image",image); 
  waitKey( 500 );
  line( image, Point(500, 500-(a*sin(pi*72/180))), Point(250, 0), Scalar( 255, 255, 0 ), 2, 8 );
  imshow("Image",image); 
  waitKey( 500 );
  line( image, Point(250, 0), Point(0, 500-(a*sin(pi*72/180))), Scalar( 255, 255, 0 ), 2, 8 );
  imshow("Image",image); 
  waitKey( 500 ); 
  line( image, Point(0, 500-(a*sin(pi*72/180))), Point((a*cos(pi*72/180)), 500), Scalar( 255, 255, 0 ), 2, 8 );
  imshow("Image",image); 
  waitKey( 0 );
  return(0);
}


Output:-