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

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...