opencv 之二值化图片
#include "cv.h"'
#include "cxcore.h"
#include "highgui.h"
#include
using namespace std;
/* way 1
int main ()
{
IplImage* img = cvLoadImage("asdf.jpg",-1);
IplImage* biimg = cvCreateImage(cvGetSize(img),img->depth,img->nChannels);
CvScalar s;
int sum=0;
for (int i=0;iheight;i++)
{
for (int j=0;jwidth;j++)
{
s = cvGet2D(img,i,j);
//cout< 128)
{
s.val[0]=s.val[1]=s.val[2]=255;
cvSet2D(img,i,j,s);
}
else
{
s.val[0]=s.val[1]=s.val[2]=0;
cvSet2D(img,i,j,s);
}
}
}
cvCopy(img,biimg);
cvSaveImage(“combibi.JPG”,biimg);
cvNamedWindow(“imagebinary”);
cvShowImage(“imagebinary”,biimg);
cvWaitKey(0);
cvDestroyWindow(“imagebinary”);
cvReleaseImage(&img);
cvReleaseImage(&biimg);
}
*/
//way 2
int main ()
{
IplImage* img = cvLoadImage(“asdf.jpg”,-1);
IplImage* binimg = cvCreateImage(cvGetSize(img),img->depth,img->nChannels);
cvThreshold(img,binimg,120,10,CV_THRESH_BINARY);//这是指定阈值了,也可以用自适应阈值的函数,cvadaptivethreshold
//cvCopy(img,binimg);
cvSaveImage(“combibi.JPG”,binimg);
cvNamedWindow(“imagebinary”);
cvShowImage(“imagebinary”,binimg);
cvWaitKey(0);
cvDestroyWindow(“imagebinary”);
cvReleaseImage(&img);
cvReleaseImage(&binimg);
}
/* //way 2 自适应阈值
int main ()
{
IplImage* img = cvLoadImage(“asdf.jpg”,-1);
IplImage* biimg = cvCreateImage(cvGetSize(img),img->depth,img->nChannels);
CvScalar s;
int sum=0;
for (int i=0;iheight;i++)
{
for (int j=0;jwidth;j++)
{
s = cvGet2D(img,i,j);
//cout< 128)
{
s.val[0]=s.val[1]=s.val[2]=255;
cvSet2D(img,i,j,s);
}
else
{
s.val[0]=s.val[1]=s.val[2]=0;
cvSet2D(img,i,j,s);
}
}
}
cvCopy(img,biimg);
cvSaveImage(“combibi.JPG”,biimg);
cvNamedWindow(“imagebinary”);
cvShowImage(“imagebinary”,biimg);
cvWaitKey(0);
cvDestroyWindow(“imagebinary”);
cvReleaseImage(&img);
cvReleaseImage(&biimg);
}
*/
wp 的代码编辑功能真是让人蛋疼