Skip to content

Instantly share code, notes, and snippets.

@nevalsar
Last active December 29, 2015 18:19
Show Gist options
  • Save nevalsar/7709527 to your computer and use it in GitHub Desktop.
Save nevalsar/7709527 to your computer and use it in GitHub Desktop.
Thresholding of a binary image using trackbar
#include "opencv2/opencv.hpp"
#define PIX(img,i,j,k) (((uchar*)img->imageData)[i*img->widthStep+j*img->nChannels+k])
IplImage* createbinary(IplImage *img, int thresh)
{
int i,j,ht,wd;
ht=img->height;
wd=img->width;
IplImage* grey=cvCreateImage(cvSize(wd,ht),IPL_DEPTH_8U,1);
for(i=0;i<ht;i++)
for(j=0;j<wd;j++)
{
PIX(grey,i,j,0)=(.33*PIX(img,i,j,0)+.33*PIX(img,i,j,1)+.33*PIX(img,i,j,2));
if(PIX(grey,i,j,0)>thresh)
PIX(grey,i,j,0)=255;
else PIX(grey,i,j,0)=0;
}
return grey;
}
int main()
{
int thresh = 127;
CvCapture* capture=cvCreateCameraCapture(0);
IplImage *frame,*grey;
char win[]="video_stream";
char win2[]="trackbar";
cvNamedWindow(win,CV_WINDOW_AUTOSIZE);
cvNamedWindow(win2,CV_WINDOW_AUTOSIZE);
cvCreateTrackbar("trackbarwin",win2,&thresh,255);
while(1)
{
frame = cvQueryFrame(capture);
grey = createbinary(frame, thresh);
cvShowImage(win,frame);
cvShowImage(win2,grey);
char c = cvWaitKey(33);
if(c==27)break;
}
cvReleaseCapture(&capture);
cvReleaseImage(&frame);
cvReleaseImage(&grey);
cvDestroyWindow(win);
cvDestroyWindow(win2);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment