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.
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.
No comments:
Post a Comment