9月 142013
 

Your ads will be inserted here by

Easy AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

通过访问像素来进行二值化处理

int main()
{
    IplImage *src = cvLoadImage("D:/1.jpg");  
	IplImage *dst = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U,1);
	CvSize size = cvGetSize(src);
	CvScalar s;
	if(src==NULL)
	{
		printf("Load failed!\n");
	    return -1;
	}
	//(int i;i<src.)
	//cvCvtColor(src,dst,CV_BGR2GRAY);
	for(int i=0;i<size.height;i++)
		for(int j=0;j<size.width;j++)
		{
		    s=cvGet2D(src,i,j);
			double tmp = (s.val[0]+s.val[1]+s.val[2])/3;
			if(tmp > 128)
				s.val[0]=s.val[1]=s.val[2] = 255;
			else if(tmp <= 128)
				s.val[0]=s.val[1]=s.val[2] = 0;			
			cvSet2D(dst,i,j,s);
		}
	cvNamedWindow("Image", 1);
	cvShowImage("Image", dst);
	cvWaitKey(0);

	cvDestroyWindow("Image");
	cvReleaseImage(&dst);
	cvReleaseImage(&src);
	return 0;
}

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)