Пример #1
0
void COpenCVCheck::OpenCVBinary(CString fileName)
{
	CvScalar colors[] = {{255,255,255},{0,0,0}};
	IplImage* pImg; //声明IplImage指针
	if((pImg = cvLoadImage(fileName, 0)) != 0)
	{
		IplImage* dst = NULL;
		dst=cvCreateImage(cvSize(pImg->width,pImg->height),IPL_DEPTH_8U,1);
		//cvThreshold(pImg,dst,185,255,CV_THRESH_BINARY);
		cvAdaptiveThreshold(pImg,dst,255,CV_ADAPTIVE_THRESH_MEAN_C,CV_THRESH_BINARY,5,3);//二值化

		ReverseColor(dst);
		for (int kk = 0;kk<2;kk++)   //去噪
		{
			CvSeq *contours;
			CvMemStorage* storage = cvCreateMemStorage(0);

			cvFindContours( dst, storage, &contours, sizeof(CvContour), CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
			//此函数以黑色为背景色
			while(contours)
			{
				//approximate contour with accuracy proportional
				CvSeq* result = cvApproxPoly( contours, sizeof(CvContour),
					storage,CV_POLY_APPROX_DP, 3, 1);
				//to filter the noisy contour
				if(fabs(cvContourArea(result,CV_WHOLE_SEQ)) < 2)
				{
					if (result->total > 0)
					{
						for(int  i = 0; i < (result ? result->total : 0); i++)
						{
							CvRect* r = (CvRect*)cvGetSeqElem(result,i);
							cvSet2D(dst,r->y,r->x,colors[1]);

						}

					}
				}
				contours = contours->h_next;
			}	
		}
		ReverseColor(dst);
		ClearNoise(dst);
		cvSaveImage(fileName,dst);
		cvReleaseImage(&dst);
		cvReleaseImage(&pImg);
	}
}
Пример #2
0
//ray tracing
void CPURayTracer::RayTrace()
{
	//reset the timer
	m_Timer.Reset();
	//start the timer
	m_Timer.Start();

	//bind the buffer
	BindBuffer();

	for( int j = m_iImageHeight - 1 ; j > -1 ; j-- )
	{
		for( int i = 0 ; i < m_iImageWidth; i++ )
		{
			//current index
			int	currentIndex = j * m_iImageWidth + i;

			//current ray
			_float4 dir , ori;

			//generate a ray for current pixel
			GenerateRay( i , j , &ori , &dir );

			//copy the image
			m_pImageBackBuffer[ currentIndex ] = Trace( ori , dir );

			//copy the image
			m_pImageBuffer[ currentIndex ] = RGB_FLOAT4( m_pImageBackBuffer[ currentIndex ] );

			//update current pixel number
			m_iCurrentPixelNum++;

			if( m_bForceStop )
				break;
		}

		if( m_bForceStop )
			break;
	}

	//clear the noise
	ClearNoise( m_pImageBackBuffer );

	//end the timer
	m_Timer.Stop();
	m_ElapsedTime = (int)m_Timer.GetElapsedTime();
}