Example #1
0
BOOL CsMIShopDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;

	// open CVoxel object
	
	m_pVoxel = new CVoxel<short>(lpszPathName);

	m_pDrawLayerZ = new cv::Mat[m_pVoxel->m_nZ];
	m_pDrawLayerY = new cv::Mat[m_pVoxel->m_nY];
	m_pDrawLayerX = new cv::Mat[m_pVoxel->m_nX];

	
	for (int i = 0; i < m_pVoxel->m_nZ; i++)
		m_pDrawLayerZ[i].create(m_pVoxel->m_nY,m_pVoxel->m_nX,CV_8UC1);
	for (int i = 0; i < m_pVoxel->m_nY; i++)
		m_pDrawLayerY[i].create(m_pVoxel->m_nX, m_pVoxel->m_nZ, CV_8UC1);
	for (int i = 0; i < m_pVoxel->m_nX; i++)
		m_pDrawLayerX[i].create(m_pVoxel->m_nZ,m_pVoxel->m_nY,CV_8UC1);

	
	m_iMaxValue = m_pVoxel->m_nMaxI;
	m_iMinValue = m_pVoxel->m_nMinI;

	//for(int z = 0; z < m_pVoxel->m_nZ; z++)
	//for(int y = 0; y < m_pVoxel->m_nY; y++)
	//for(int x = 0; x < m_pVoxel->m_nX; x++)
	//{
	//	if(m_pVoxel->m_pData[z*m_pVoxel->m_nY*m_pVoxel->m_nX + y*m_pVoxel->m_nX + x] > m_iMaxValue)
	//		m_iMaxValue = m_pVoxel->m_pData[z*m_pVoxel->m_nY*m_pVoxel->m_nX + y*m_pVoxel->m_nX + x];
	//	if (m_pVoxel->m_pData[z*m_pVoxel->m_nY*m_pVoxel->m_nX + y*m_pVoxel->m_nX + x] < m_iMinValue)
	//		m_iMinValue = m_pVoxel->m_pData[z*m_pVoxel->m_nY*m_pVoxel->m_nX + y*m_pVoxel->m_nX + x];
	//	
	//}

#if 0
	// TEST 3: to warp with thin-plate spline - layer separation
#endif 

#if 0
	// TEST 2: try to extract bone (which has no motion) 
	int imgw = m_pVoxel->m_nX;
	int imgh = m_pVoxel->m_nY;
	int nfrm = m_pVoxel->m_nZ;
	//		construct average image
	cv::Mat avg = cv::Mat::zeros(imgh, imgw, CV_32F);
	for ( int y = 0; y < m_pVoxel->m_nY; y++ ) {
		float *avgp = avg.ptr<float>(y);
		for ( int x = 0; x < m_pVoxel->m_nX; x++ ) {
			for ( int i = 0; i < m_pVoxel->m_nZ; i++ ) {
				avgp[x] += m_pVoxel->GetAt(x,y,i);
			}
		}
	}
	avg = avg/(float)nfrm;
	//		save average image
	cv::Mat avg_8u;
	avg.convertTo(avg_8u, CV_8U);
	cv::imwrite("avg_img.png", avg_8u);

	//		compute subtraction image for all frames
	for ( int i = 0; i < nfrm; i++ ) {
		cv::Mat frm_idx = m_pVoxel->VoxelZSlice2cvMat<short>(i);
		cv::Mat frm_idx_f;
		frm_idx.convertTo(frm_idx_f, CV_32F);
		cv::Mat frm_sub = cv::abs(frm_idx_f - avg);
		double minf, maxf;
		cv::minMaxIdx(frm_sub, &minf, &maxf);
		cv::Mat frm_sub_8u;
		frm_sub.convertTo(frm_sub_8u, CV_8U, 255.0/(maxf-minf), -minf);
		char path[MAX_PATH];
		sprintf(path, "frm_sub_%04d.png", i);
		cv::imwrite(path, frm_sub_8u);
	}
#endif

	// TEST 1: compare optical flow between consecutive and synchronized frames
	// 20140825 ****** OPTICAL FLOW FAILS TOTALLY FOR 2D+t CARDIAC X-RAY IMAGES ****** // 
#if 0
	//////////////////////////////////////////////////////////
	// 20140820 *** ASSUME VESSEL IMAGE HAS BEEN LOADED *** //
	// set the parameters
	//     para(1)--alpha (1), the regularization weight
	//     para(2)--ratio (0.5), the downsample ratio
	//     para(3)--minWidth (40), the width of the coarsest level
	//     para(4)--nOuterFPIterations (3), the number of outer fixed point iterations
	//     para(5)--nInnerFPIterations (1), the number of inner fixed point iterations
	//     para(6)--nSORIterations (20), the number of SOR iterations
	double alpha= 0.012;
	double ratio=0.75;
	int minWidth= 32;
	int nOuterFPIterations = 5;//5;
	int nInnerFPIterations = 1;//3;
	int nSORIterations= 20;//40;

	// compute optical flow between consecutive frames
	int n_src = 24;
	int n_dst = 36;
	DImage frm_src = m_pVoxel->VoxelZSlice2DImage(n_src);
	DImage frm_dst = m_pVoxel->VoxelZSlice2DImage(n_dst);
	DImage cvx,cvy,cwarpI2;
	OpticalFlow::Coarse2FineFlow(cvx,cvy,cwarpI2,frm_src,frm_dst,
		alpha,ratio,minWidth,nOuterFPIterations,nInnerFPIterations,nSORIterations);
	char str[MAX_PATH], str_dst[MAX_PATH];
	sprintf(str, "frm%04d.png", n_src);
	frm_src.imwrite(str);
	sprintf(str, "frm%04d.png", n_dst);
	frm_dst.imwrite(str);
	sprintf(str, "warp_%04d_%04d.png", n_src, n_dst);
	cwarpI2.imwrite(str);
	DImage vimgc = OptFlowVec2Color_DImage(&cvx, &cvy);
	sprintf(str, "motvec_%04d_%04d.png", n_src, n_dst);
	vimgc.imwrite(str);
	//DImage nimgc = OptFlowVecNorm2DImage(&cvx, &cvy, 0.5);
	DImage nimgc = OptFlowVecNorm2DImage(&cvx, &cvy);
	sprintf(str, "motnorm_%04d_%04d.png", n_src, n_dst);
	nimgc.imwrite(str);
#endif
	//////////////////////////////////////////////////////////

	return TRUE;
}