Open Computer Vision

April 9, 2008

Thresholding with an OpenCV Function

Filed under: Uncategorized — colouredpages @ 6:12 pm

Thresholding is very very Important aspect in any field and not just in Open Computer Vision..but in Image processing you need to specify some conditions which is very frequently Thresholding and this is what is the main concern of this post.Here you would get to know about what is Thresholding and also how to use the same and when its required.In almost every program you would specify to extract a pixel above a certain value which is nothing but thresholding …..

Friends I had previously put up a code which spoke about thresholding but without using thresholding functions in OpenCV.Now here is a blog post program which uses cvThreshold to threshold Images have a look..Beside is the picture lena.jpg which is frequently used for image processing functions…_Even i have used this picture in all of my Image processing functions..!

#include "cv.h"
#include "highgui.h"
#include "stdio.h"
#include "conio.h"

int main() {

int height,width,step,step_mono,channels;
/*Here i have declared step_mono
for handling the widthstep member of a monochrome image*/
uchar *data,*data_mono;/*similarly data mono for handling the
 data of monochrome image*/
int i,j,k;

IplImage* frame=cvLoadImage("lena.jpg",1);

if(frame==NULL){ /*If image not found then exit.
This is the function of this loop */
printf("the file doesnot exist");
exit(0);
}
IplImage* mono_thres=cvCreateImage( cvGetSize(frame), 8, 1 );
height = frame -> height;/*height is a member of IPLIMAGE structure
and hence it comes handy like this in such situations,
and same goes with below four statements*/
width = frame -> width;
step = frame -> widthStep;
step_mono = mono_thres -> widthStep;
channels = frame -> nChannels;/*Number of channels in the image*/
data = (uchar *)frame -> imageData;/*Image is treated as as unsigned char data
hence we use an unsigned char pointer to point to the same*/
cvNamedWindow("My Window", CV_WINDOW_AUTOSIZE );
data_mono = (uchar *)mono_thres -> imageData;/*data of mono image is handled by the data_mono*/

for(i=0;i < height;i++) for(j=0;j < width;j++)
/*I am copying the first channel from the image in "frame" in
the monochrome image with the help of this line below..*/
data_mono[i*step_mono+j*1+0]=data[i*step+j*channels+0];

cvThreshold(mono_thres,mono_thres,70,/*70 is the lower cut off*/
150,/*this is the higher cut off*/
CV_THRESH_BINARY/*The type of thresholding,
more description in the documentation*/);

/*Here if the pixel is above the threshold(200) is moved to highest value
and the pixels below the threshold are left as it is*/

cvShowImage("My Window",mono_thres);
cvSaveImage("con_img2.jpg",mono_thres);
/*THis function will save the image */
 /*Destroying the initialized Windows */
cvWaitKey();/*sometimes getch is not compatible with opencv I
 suggest you use cvwaitkey ..this time it worked with me..*/
cvDestroyWindow( "My Window" );
/*as we can see name of the image is followed by the extension..
and the second argument "frame" is the image which will
be saved .This applies similarly in the above statement
 cvShowImage as well.Be careful regarding this.*/

return 0;/*This statement is compiler specific and might change.
As i already said that I am using DEV C++ as my compiler*/
}

Lena

Lena

This is the picture of the Thresholded image .See the Result.

Thresholded Result Image

Thresholded Result Image

13 Comments »

  1. Dear Sir,
    Hope you are fine. I am doing a project on detection of surface defects for CITRON. In this connection I am trying to use opencv for image processing though I am totally new in opencv. For my research,Please help me regarding following questions. I appreciate it.
    a) how can I calculate RGB value at different X,Y position of an image?
    b)for analysing, how can I detect only the target objects (actually the circular ROI shape,for CITRON)from the source image.

    Thanking you and I look forward to hear from you.

    Comment by Momin — March 1, 2010 @ 9:30 am | Reply

    • Dear,

      to find the values of RGB i have given numerous examples in all of my posts….just use that “data[...]” strructure which i have used in all of my posts and secondly there is no circular roi and you need to first identify or detect where in the source image your required surface is..!! you can do this by making note of some points which are unique to the structure you want to identify..!!

      Comment by colouredpages — March 2, 2010 @ 5:03 am | Reply

  2. yes lena is a colour image …the meaning of a colour image is that it is composed of 3 channels of colour R,G,B… just for the example purpose i have taken one channel and I have done some operations..!!

    Comment by colouredpages — March 2, 2010 @ 5:10 am | Reply

  3. Dear Sir,

    Thanks for your prompt reply. I understood you choosed channel B. Now in the following code, If I mention 3 in replace of 1 then is it ok or not! I found little difference in the result image window. Pls let me clear.

    IplImage* mono_thres=cvCreateImage( cvGetSize(frame), 8, 1 );
    IplImage* mono_thres=cvCreateImage( cvGetSize(frame), 8, 3 );

    Thanks a lot.

    Comment by sumon — March 2, 2010 @ 5:55 am | Reply

    • Follow the opencv documentation and follow the cvcreate image documentation….you wil be able to understand….but even if you are not able to folllow the documentation …If you are not able to understand even after reading the documentation ..I am here to help you and the purpose why I am doing so is just to make you independent..!!

      Radhe Radhe

      Comment by colouredpages — March 2, 2010 @ 12:57 pm | Reply

      • Dear Sir,

        Thanks for your valuable suggessions. How can I contact with you? if possible pls let me know your email ID.

        Regards
        Momin

        Comment by Momin — March 3, 2010 @ 4:06 am

  4. Dear Sir,

    Thanks for your response. Ive a request to you, if possible pls give me a link address or some examples of your post to detect RGB values at different position using opencv.

    I am looking forward to hear from you.

    Thanks

    Comment by Momin — March 2, 2010 @ 6:08 am | Reply

  5. Dear sir,

    I got the solution to detect RGB values at different positions using opencv. But now I want to detect HSI values at different RGB values.

    Therefore pls let me know how can I detect HSI values of an image.

    Regards,
    Momin

    Comment by Momin — March 8, 2010 @ 1:54 am | Reply

    • dear…..

      see accessing is the same for an image as it is for RGB….. change in color palette just means change in the values of the RGB components…..for example RGB(125,200,170) might become HSI(x,y,z) where x,y,z are just numbers which which we have got after the application of the conversion formulae for the respective palette….but the way it is stored is the same..!!
      (you can observe the same on the examples on the blog but the values contained in them is different and we treat the image obtained after the conversion in a different way..!!
      so accesing the matrix is the same as it is for the RGB/…!

      Radhe Radhe

      Comment by colouredpages — March 8, 2010 @ 3:15 am | Reply

      • Dear Sir,

        I am trying to find HSI values for different RGB values using opencv but yet not success.

        It will be highly appreciated, if you create an example for me to detect RGB and HSI simultaneously of an image.

        Thanx
        Momin

        Comment by Momin — March 11, 2010 @ 7:57 am

  6. Hi sir,

    I am doing a project on face recognition in opencv. i have detected faces and facing difficulties in recognition. can you help me please!
    thanks in advance

    Comment by pavan — June 7, 2010 @ 11:45 am | Reply

    • There is a face detection program already present which will get a circle on the face which is already on this site.,..please chek

      Comment by colouredpages — June 7, 2010 @ 1:10 pm | Reply

  7. It is just little bit different..I have already provided all the related tutorials on this blog..!!

    Comment by colouredpages — March 11, 2011 @ 3:28 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.