首页 > 代码库 > opencv二值化处理
opencv二值化处理
#include "stdafx.h"//对一张图片进行二值化处理
IplImage *pSrclmg =NULL;//载入的图片
IplImage *pDeclmg =NULL;//生成的图像
void onTrackerSlid(int thresth)
{
/*int i,j,k; //设置一个阈值,如果大于这个阈值就赋为255,小于这个阈值就赋为0
int height =pDeclmg->height;
int width =pDeclmg->width;
int step =pDeclmg->widthStep;
int Channels =pDeclmg->nChannels;
uchar *data0 =(uchar*)(pSrclmg->imageData);
uchar *data1 =(uchar*)(pSrclmg->imageData);
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
for(k=0;k<Channels;k++)
{
if(data0[i*step+j+k]>thresth)
data0[i*step+j+k]=255;
else
data1[i*step+j+k]=0;
}
}
}
cvShowImage("pDeclmg_Image",pDeclmg);*/
转为二值图
cvThreshold(pSrclmg,pDeclmg, thresth, 255, CV_THRESH_BINARY);
显示二值图
cvShowImage("pDeclmg_Image",pDeclmg);
}
int _tmain(int argc,_TCHAR* argv[])
{
int thresth=127;
pSrclmg =cvLoadImage("路面4.jpg",0);
pDeclmg =cvCreateImage(cvGetSize(pSrclmg),8,1);
cvShowImage("source_Image",pSrclmg);
pDeclmg=cvCloneImage(pSrclmg);
cvNamedWindow("source_Image",CV_WINDOW_AUTOSIZE);
cvNamedWindow("pDeclmg_Image",0);
cvCreateTrackbar("threshold","pDeclmg_Image",&thresth,255,onTrackerSlid);
onTrackerSlid(thresth);
cvSaveImage("E:\\素材\\路面5.jpg",pDeclmg);
cvWaitKey(0);
cvDestroyWindow("source_Image");
cvDestroyWindow("pDeclmg_Image");
cvReleaseImage(&pSrclmg);
cvReleaseImage(&pDeclmg);
return 0;
}
opencv二值化处理