Showing posts with label Open CV. Show all posts
Showing posts with label Open CV. Show all posts

Wednesday, April 30, 2014

Splitting the Image in to three channels with Open CV and C Language


In image processing some tines we need to separate the channels of image. This is needful to achieve the required contrast of the image. Because some times if we remove the one or more channels from the image we can get better understanding of the image under consideration. In some cases we have to process only few channels of the image rather than the entire image. Open cv provides the functions to achieve the same. We can do the same thing with out using the functions of the open cv.

Fallowing code snippet is useful for splitting the image channels:

#include "stdafx.h" 
#include "cv.h"
#include "highgui.h"
#include <conio.h>

void main()
{
    IplImage *Simg;
    IplImage *Rimg;
    IplImage *Gimg;
    IplImage *Bimg;

    int nrows,ncols;

    Simg=cvLoadImage("E:/test.jpg");
    Rimg=cvCreateImage(cvSize(Simg->width,Simg->height),Simg->depth,3);
    Gimg=cvCreateImage(cvSize(Simg->width,Simg->height),Simg->depth,3);
    Bimg=cvCreateImage(cvSize(Simg->width,Simg->height),Simg->depth,3);

    cvNamedWindow("Red",0);
    cvNamedWindow("Green",0);
    cvNamedWindow("Blue",0);
    cvResizeWindow("Red",320,320);
    cvResizeWindow("Green",320,320);
    cvResizeWindow("Blue",320,320);

    nrows=Simg->height;
    ncols=Simg->width;
    
     for(int i=0;i<nrows*ncols*3;i++)
    {
        Rimg->imageData[i]=0;
        Gimg->imageData[i]=0;
        Bimg->imageData[i]=0;
    }
    for(int i=0;i<nrows*ncols*3;i+=3)
    {   
            Rimg->imageData[i+2]=Simg->imageData[i+2];
            Gimg->imageData[i+1]=Simg->imageData[i+1];
            Bimg->imageData[i]=Simg->imageData[i];
     }
   
    cvShowImage("Red",Rimg);
    cvShowImage("Green",Gimg);
    cvShowImage("Blue",Bimg);
    cvWaitKey(0);
    _getch();
}

for example consider the image with three channels. Load that image into the memory with the help of Open CV. Next create the three images with the same size,depth and channels. Fill the entire image with the zeros means with black pixels. Now extract the individual channels from the original image and store them in the newly created images. Open cv changes the order of the pixels to BGR instead of RGB.
If we apply the ->height and ->width on the image in Open CV we will get the number of rows and columns. But actually if the image is color we have columns more than what we get. that is if we have width as 20 the actual columns on the disk is 60 because each channel will store in separate byte. In memory they will be stored in interleaved manner. So to get the red pixels we need to get the alternate pixels with the gap of two.
By running the above code on the below image:
  

we will get the fallowing images:
 





Sunday, April 13, 2014

Image Rotation with Open cv and C++



In real time scenarios some times we need to apply the geometric transformation on the images like rotation etc. This process includes rotating the entire image around the center of the image. It is better to map the rotated image on other image like blank image. In digital computers images are treated as the matrices or two dimensional vectors.

Int his process we find some image points falling on the outside of the boundaries. There are lot of procedures to deal with this type of problems. One is leaving those points. Second one is plotting the rotated image on the larger canvas.

In digital computer this process becomes simple matrix multiplication. In geometry as well this process is represented as the matrix operations. In image processing this type of transformations are called affine transforms.

Mathematically whole precess can be represented in two steps.

x2=cos(t)*(x1-x0)-sin(t)*(y1-y0)+x0
y2=sin(t)*(x1-x0)+cos(t)*(y1-y0)+y0

x2 is the new coordinate of the rotated image corresponding to x1 of original image similarly y2.
t is the required angle of rotation.  x0 and y0 or the center coordinates of the image.

The fallowing code snippet written in open cv and c++ does the exactly same.

#include "stdafx.h" 
#include "cv.h"
#include "highgui.h"
#include <conio.h>
#include <math.h>

using namespace cv;
using namespace std;

#define PI 3.14159265

void main()
{
    int angle;
    Mat img;
    img=imread("E:/test.jpg",CV_LOAD_IMAGE_GRAYSCALE);
    Mat nimg(img.rows,img.cols,CV_8UC1,Scalar(0));
    Mat tm(2,2,CV_32SC1);
    Mat nc(2,2,CV_32SC1);
    Mat oc(2,2,CV_32SC1);
    cout<<"Enter the Rotation angle\n";
    cin>>angle;
    float cosine=cos(angle*PI/180.0);
    float sine=sin(angle*PI/180.0);

    float cx=img.cols/2.0;
    float cy=img.rows/2.0;


    for(int i=0;i<img.rows;i++)
    {
        for(int j=0;j<img.cols;j++)
        {
            int nx=(cosine*(i-cx))-(sine*(j-cy))+cx;
            int ny=(sine*(i-cx))+(cosine*(j-cy))+cy;
            if((nx>=0)&&(ny>=0)&&(nx<=img.rows)&&(ny<=img.cols))
            {
            nimg.at<uchar>(nx,ny)=img.at<uchar>(i,j);
            }
        }
    }
   imshow("Image",nimg);
   waitKey(100);
    _getch();
}

Saturday, March 8, 2014

Image thresholding using Open CV and C language with track bar

In previous post we have seen the image thresholding opeeeration. In that the user provides the thresholding level before start of the operation after if he wants to change he has to restart the app. In this post I will explain how interactively user can provide the threshold level. Open CV has lot of GUI tools like in built support for windows etc. We can add one track bar for the window to change the threshold value.

#include "stdafx.h" 
#include "cv.h"
#include "highgui.h"
#include <conio.h>
#include <string.h>

    int rows,cols,level,i,j,threshold_value = 0;

    IplImage * image;
    IplImage * img;

    char path[20];
    const int max_value=255;
    char* trackbar_value = "Value";
    char* imagedata;
    char * name="Threshold";
   

int main()
{
   
    puts("Enter the path of the image");
    gets(path);

    image=cvLoadImage(path,CV_LOAD_IMAGE_GRAYSCALE);
    rows=image->height;
    cols=image->width;
   
    img=cvLoadImage(path,CV_LOAD_IMAGE_GRAYSCALE);
       
    cvNamedWindow(name,0);
    cvResizeWindow(name,352,288);
    cvCreateTrackbar( trackbar_value,"Threshold", &threshold_value,max_value);
   
    while(true)
    {
    imagedata=(char*)image->imageData;
   
    for(i=0;i<rows*cols;i++)
    {
        img->imageData[i]=imagedata[i]>threshold_value?imagedata[i]:0;
    }
    cvShowImage(name,img);
    int c;
    c = cvWaitKey( 20 );
    if( (char)c == 27 )
    {
        cvReleaseImage(&image);
        cvReleaseImage(&img);
        cvDestroyWindow("Threshold");
          break;
    }
   }

    getch();
    return 0;
}

To add the track bar for the window we can use the cvCreateTrackbar function from the Open CV library. The first argument is the name to be displayed for the bar. Second one is the name of the window on which track must be showed. Third one is thee value we want to change with bar. fourth one is the maximum allowed value on track bar.

Thursday, March 6, 2014

ImageThresholding uisng Open CV and C

Thresholding is the very basic image processing technique. This splits the pixels of image into two subsets. One set has the pixel values less than the thresholding level and other has the pixel values greater than or equal to the thresholding value. Means thresholding produces binary images having only two values of pixels.

If we draw the histogram of the image after thresholding we will get only two impulse functions in the histogram because we have only two sets of pixels.

By using Open CV and any other programming language like C or C++ we can threshold the images. This is very simple operation. In the fallowing code snippet I explained how we can achieve the image thresholding on images.

The thresholded image looks like:



#include "stdafx.h" 
#include "cv.h"
#include "highgui.h"
#include <conio.h>
#include <string.h>

int main()
{
    IplImage * image;
    char path[20];
    int rows,cols,level,i;
    char* imagedata;

    puts("Enter the path of the image");
    gets(path);

    image=cvLoadImage(path,CV_LOAD_IMAGE_GRAYSCALE);
    rows=image->height;
    cols=image->width;

    puts("Enter the threshold level");
    scanf("%d",&level);
    imagedata=(char*)image->imageData;

    for(i=0;i<rows*cols;i++)
    {
        imagedata[i]=imagedata[i]>level?level:0;
    }

    cvShowImage("Threshold",image);
    if(cvWaitKey(1)==27)
    {
        exit(0);
    }

    getch();
    return 0;
}

I think by now you have gone through the code. As usual we started the coding with including the most important header files like cv.h and highgui.h. In the starting of the code we have one char array it stores the path of the image. After we have five integer variables. These are used for storing the height, width of the images and two more variables to iterate the image data and other one is for holding the threshold value given by the user.

The code work like this user enters the path of the image next he/she enters the threshold value. Based on the threshold the image is segmented into two separate pixel values. Here we used inplace modification. We directly changed the pixel values of image with out using other image to store the result.

We compared the image pixel value with the user entered threshold value if the pixel value is greater than the threshold we replaced that pixel with the threshold value other wise we replaced with zero value i.e black pixel.

In thresolding we have variations. The variations include threshold binary, threshold binary inverted,truncate, threshold to zero, threshold to zero inverted*.
We can implement all these types of thresholding methods just by changing the code line

imagedata[i]=imagedata[i]>level?level:0;

for threshold binary use   imagedata[i]=imagedata[i]>level?255:0; 

for threshold binary inverted use   imagedata[i]=imagedata[i]>level?0:255; 


for truncate use   imagedata[i]=imagedata[i]>level?level:imagedata[i]; 


for threshold to zero use   imagedata[i]=imagedata[i]>level?imagedata[i]:0; 


for threshold to zero inverted use   imagedata[i]=imagedata[i]>level?0:imagedata[i];

* according to Open CV documentation.

DC motor control with Pulse Width Modulation Part 1

DC Motor intro DC motor is a device which converts electrical energy into kinetic energy. It converts the DC power into movement. The typica...