Esempio n. 1
0
//----------------------------------------------------------------------------------------
// Iterate
void CSirtAlgorithm::run(int _iNrIterations)
{
    // check initialized
    ASTRA_ASSERT(m_bIsInitialized);

    m_bShouldAbort = false;

    int iIteration = 0;

    // data projectors
    CDataProjectorInterface* pForwardProjector;
    CDataProjectorInterface* pBackProjector;
    CDataProjectorInterface* pFirstForwardProjector;

    m_pTotalRayLength->setData(0.0f);
    m_pTotalPixelWeight->setData(0.0f);

    // forward projection data projector
    pForwardProjector = dispatchDataProjector(
                            m_pProjector,
                            SinogramMaskPolicy(m_pSinogramMask),														// sinogram mask
                            ReconstructionMaskPolicy(m_pReconstructionMask),											// reconstruction mask
                            DiffFPPolicy(m_pReconstruction, m_pDiffSinogram, m_pSinogram),								// forward projection with difference calculation
                            m_bUseSinogramMask, m_bUseReconstructionMask, true											// options on/off
                        );

    // backprojection data projector
    pBackProjector = dispatchDataProjector(
                         m_pProjector,
                         SinogramMaskPolicy(m_pSinogramMask),														// sinogram mask
                         ReconstructionMaskPolicy(m_pReconstructionMask),											// reconstruction mask
                         DefaultBPPolicy(m_pTmpVolume, m_pDiffSinogram), // backprojection
                         m_bUseSinogramMask, m_bUseReconstructionMask, true // options on/off
                     );

    // first time forward projection data projector,
    // also computes total pixel weight and total ray length
    pFirstForwardProjector = dispatchDataProjector(
                                 m_pProjector,
                                 SinogramMaskPolicy(m_pSinogramMask),														// sinogram mask
                                 ReconstructionMaskPolicy(m_pReconstructionMask),											// reconstruction mask
                                 Combine3Policy<DiffFPPolicy, TotalPixelWeightPolicy, TotalRayLengthPolicy>(					// 3 basic operations
                                     DiffFPPolicy(m_pReconstruction, m_pDiffSinogram, m_pSinogram),								// forward projection with difference calculation
                                     TotalPixelWeightPolicy(m_pTotalPixelWeight),												// calculate the total pixel weights
                                     TotalRayLengthPolicy(m_pTotalRayLength)),													// calculate the total ray lengths
                                 m_bUseSinogramMask, m_bUseReconstructionMask, true											 // options on/off
                             );



    // forward projection, difference calculation and raylength/pixelweight computation
    pFirstForwardProjector->project();

    float32* pfT = m_pTotalPixelWeight->getData();
    for (int i = 0; i < m_pTotalPixelWeight->getSize(); ++i) {
        float32 x = pfT[i];
        if (x < -eps || x > eps)
            x = 1.0f / x;
        else
            x = 0.0f;
        pfT[i] = x;
    }
    pfT = m_pTotalRayLength->getData();
    for (int i = 0; i < m_pTotalRayLength->getSize(); ++i) {
        float32 x = pfT[i];
        if (x < -eps || x > eps)
            x = 1.0f / x;
        else
            x = 0.0f;
        pfT[i] = x;
    }

    // divide by line weights
    (*m_pDiffSinogram) *= (*m_pTotalRayLength);

    // backprojection
    m_pTmpVolume->setData(0.0f);
    pBackProjector->project();

    // divide by pixel weights
    (*m_pTmpVolume) *= (*m_pTotalPixelWeight);
    (*m_pReconstruction) += (*m_pTmpVolume);

    if (m_bUseMinConstraint)
        m_pReconstruction->clampMin(m_fMinValue);
    if (m_bUseMaxConstraint)
        m_pReconstruction->clampMax(m_fMaxValue);

    // update iteration count
    m_iIterationCount++;
    iIteration++;




    // iteration loop
    for (; iIteration < _iNrIterations && !m_bShouldAbort; ++iIteration) {
        // forward projection and difference calculation
        pForwardProjector->project();

        // divide by line weights
        (*m_pDiffSinogram) *= (*m_pTotalRayLength);


        // backprojection
        m_pTmpVolume->setData(0.0f);
        pBackProjector->project();

        // divide by pixel weights
        (*m_pTmpVolume) *= (*m_pTotalPixelWeight);
        (*m_pReconstruction) += (*m_pTmpVolume);

        if (m_bUseMinConstraint)
            m_pReconstruction->clampMin(m_fMinValue);
        if (m_bUseMaxConstraint)
            m_pReconstruction->clampMax(m_fMaxValue);

        // update iteration count
        m_iIterationCount++;
    }


    ASTRA_DELETE(pForwardProjector);
    ASTRA_DELETE(pBackProjector);
    ASTRA_DELETE(pFirstForwardProjector);
}
Esempio n. 2
0
//----------------------------------------------------------------------------------------
// Iterate
void CCglsAlgorithm::run(int _iNrIterations)
{
	// check initialized
	ASTRA_ASSERT(m_bIsInitialized);

	// data projectors
	CDataProjectorInterface* pForwardProjector;
	CDataProjectorInterface* pFirstForwardProjector;
	CDataProjectorInterface* pBackProjector;

	// Clear reconstruction volume.
	if (m_bClearReconstruction) {
		m_pReconstruction->setData(0.0f);
	}

	// forward projection data projector
	if (m_bUseJacobiPreconditioner) {
		// w = A t
		pForwardProjector = dispatchDataProjector(
			m_pProjector, 
				SinogramMaskPolicy(m_pSinogramMask),					// sinogram mask
				ReconstructionMaskPolicy(m_pReconstructionMask),		// reconstruction mask
				DefaultFPPolicy(t, w),									// forward projection
				m_bUseSinogramMask, m_bUseReconstructionMask, true		// options on/off
			); 
	} else {
		// w = A p
		pForwardProjector = dispatchDataProjector(
			m_pProjector, 
				SinogramMaskPolicy(m_pSinogramMask),					// sinogram mask
				ReconstructionMaskPolicy(m_pReconstructionMask),		// reconstruction mask
				DefaultFPPolicy(p, w),									// forward projection
				m_bUseSinogramMask, m_bUseReconstructionMask, true		// options on/off
			); 
	}

	// backprojection data projector
	pBackProjector = dispatchDataProjector(
			m_pProjector, 
			SinogramMaskPolicy(m_pSinogramMask),														// sinogram mask
			ReconstructionMaskPolicy(m_pReconstructionMask),											// reconstruction mask
			DefaultBPPolicy(z, r),																		//  backprojection
			m_bUseSinogramMask, m_bUseReconstructionMask, true // options on/off
		); 

	// First forward projector to compute the Jacobi preconditioner, which
	// is just the norm squares of the columns of the projection matrix A
	// (it is diagonal of A' * A)
	pFirstForwardProjector = dispatchDataProjector(
		m_pProjector, 
			SinogramMaskPolicy(m_pSinogramMask),					// sinogram mask
			ReconstructionMaskPolicy(m_pReconstructionMask),		// reconstruction mask
			TotalPixelWeightPolicy(c, false, true),									// forward projection
			m_bUseSinogramMask, m_bUseReconstructionMask, true		// options on/off
		); 
	// Compute the Jacobi preconditioner.
	if (m_bUseJacobiPreconditioner) {
		c->setData(0.f);
		pFirstForwardProjector->project();
		// Compute sqrt
		c->sqrt();
	}

	int i;

	if (m_iIteration == 0) {
		// r = b;
		r->copyData(m_pSinogram->getData());
		// w = A*x0
		p->copyData(m_pReconstruction->getData());
		pForwardProjector->project();
		// r = b - A*x0
		*r -= *w;

		// z = A' * (b - A * x0) = A' * r
		z->setData(0.0f);
		pBackProjector->project();
		// CHECK
		//if (m_bUseMinConstraint)
		//	z->clampMin(m_fMinValue);
		//if (m_bUseMaxConstraint)
		//	z->clampMax(m_fMaxValue);

		// Precondition?
		if (m_bUseJacobiPreconditioner) {
			*z /= *c;
		}

		// p = z;
		p->copyData(z->getData());
		
		// gamma = dot(z,z);
		gamma = 0.0f;
		for (i = 0; i < z->getSize(); ++i) {
			gamma += z->getData()[i] * z->getData()[i];
		}

		m_iIteration++;
	}

	// start iterations
	//for (int iIteration = _iNrIterations-1; iIteration >= 0; --iIteration) {
	for (int iIteration = 0; iIteration < _iNrIterations; ++iIteration) {
		// start timer
		m_ulTimer = CPlatformDepSystemCode::getMSCount();

		if (m_bUseJacobiPreconditioner) {
			// t = C^-1 p
			t->copyData(p->getData());
			*t /= *c;
		}	

		// w = A*p (or A*t if precondioning)
		w->setData(0);
		pForwardProjector->project();
	
		// alpha = gamma/dot(w,w);
		float32 tmp = 0;
		for (i = 0; i < w->getSize(); ++i) {
			tmp += w->getData()[i] * w->getData()[i];
		}
		alpha = gamma / tmp;

		if (m_bUseJacobiPreconditioner) {
			// x = x + alpha*t;
			for (i = 0; i < m_pReconstruction->getSize(); ++i) {
				m_pReconstruction->getData()[i] += alpha * t->getData()[i];
			}
		} else {
			// x = x + alpha*p;
			for (i = 0; i < m_pReconstruction->getSize(); ++i) {
				m_pReconstruction->getData()[i] += alpha * p->getData()[i];
			}
		}

		// r = r - alpha*w;
		for (i = 0; i < r->getSize(); ++i) {
			r->getData()[i] -= alpha * w->getData()[i];
		}

		// z = A'*r;
		z->setData(0.0f);
		pBackProjector->project();

		// Precondition?
		if (m_bUseJacobiPreconditioner) {
			// z = C^-1 C
			*z /= *c;
		}

		// CHECKME: should these be here?
		// This was z. CHECK
		if (m_bUseMinConstraint)
			m_pReconstruction->clampMin(m_fMinValue);
		if (m_bUseMaxConstraint)
			m_pReconstruction->clampMax(m_fMaxValue);

		// beta = 1/gamma;
		beta = 1.0f / gamma;

		// gamma = dot(z,z);
		gamma = 0;
		for (i = 0; i < z->getSize(); ++i) {
			gamma += z->getData()[i] * z->getData()[i];
		}

		// beta = gamma*beta;
		beta *= gamma; 

		// p = z + beta*p;
		for (i = 0; i < z->getSize(); ++i) {
			p->getData()[i] = z->getData()[i] + beta * p->getData()[i];
		}
		
		// end timer
		m_ulTimer = CPlatformDepSystemCode::getMSCount() - m_ulTimer;

		// Compute metrics.
		computeIterationMetrics(iIteration, _iNrIterations, r);

		m_iIteration++;
	}

	ASTRA_DELETE(pForwardProjector);
	ASTRA_DELETE(pBackProjector);
	ASTRA_DELETE(pFirstForwardProjector);

	//if (m_iIteration == 0) {
	//	// r = b;
	//	r->copyData(m_pSinogram->getData());

	//	// z = A'*b;
	//	z->setData(0.0f);
	//	pBackProjector->project();
	//	// CHECK
	//	//if (m_bUseMinConstraint)
	//	//	z->clampMin(m_fMinValue);
	//	//if (m_bUseMaxConstraint)
	//	//	z->clampMax(m_fMaxValue);

	//	// p = z;
	//	p->copyData(z->getData());

	//	// gamma = dot(z,z);
	//	gamma = 0.0f;
	//	for (i = 0; i < z->getSize(); ++i) {
	//		gamma += z->getData()[i] * z->getData()[i];
	//	}
	//	
	//	m_iIteration++;
	//}


	//// start iterations
	////for (int iIteration = _iNrIterations-1; iIteration >= 0; --iIteration) {
	//for (int iIteration = 0; iIteration < _iNrIterations; ++iIteration) {
	//	// start timer
	//	m_ulTimer = CPlatformDepSystemCode::getMSCount();
	//
	//	// w = A*p;
	//	pForwardProjector->project();
	//
	//	// alpha = gamma/dot(w,w);
	//	float32 tmp = 0;
	//	for (i = 0; i < w->getSize(); ++i) {
	//		tmp += w->getData()[i] * w->getData()[i];
	//	}
	//	alpha = gamma / tmp;

	//	// x = x + alpha*p;
	//	for (i = 0; i < m_pReconstruction->getSize(); ++i) {
	//		m_pReconstruction->getData()[i] += alpha * p->getData()[i];
	//	}

	//	// r = r - alpha*w;
	//	for (i = 0; i < r->getSize(); ++i) {
	//		r->getData()[i] -= alpha * w->getData()[i];
	//	}

	//	// z = A'*r;
	//	z->setData(0.0f);
	//	pBackProjector->project();

	//	// CHECKME: should these be here?
	//	// This was z. CHECK
	//	if (m_bUseMinConstraint)
	//		m_pReconstruction->clampMin(m_fMinValue);
	//	if (m_bUseMaxConstraint)
	//		m_pReconstruction->clampMax(m_fMaxValue);

	//	// beta = 1/gamma;
	//	beta = 1.0f / gamma;

	//	// gamma = dot(z,z);
	//	gamma = 0;
	//	for (i = 0; i < z->getSize(); ++i) {
	//		gamma += z->getData()[i] * z->getData()[i];
	//	}

	//	// beta = gamma*beta;
	//	beta *= gamma; 

	//	// p = z + beta*p;
	//	for (i = 0; i < z->getSize(); ++i) {
	//		p->getData()[i] = z->getData()[i] + beta * p->getData()[i];
	//	}
	//	
	//	// end timer
	//	m_ulTotalTime += CPlatformDepSystemCode::getMSCount() - m_ulTimer;

	//	// Compute metrics.
	//	computeIterationMetrics(iIteration, _iNrIterations);

	//	m_iIteration++;
	//}

}
Esempio n. 3
0
//----------------------------------------------------------------------------------------
// Iterate
void CSirtAlgorithm::run(int _iNrIterations)
{
	// check initialized
	ASTRA_ASSERT(m_bIsInitialized);

	m_bShouldAbort = false;

	// data projectors
	CDataProjectorInterface* pForwardProjector;
	CDataProjectorInterface* pBackProjector;
	CDataProjectorInterface* pFirstForwardProjector;

	// Initialize m_pReconstruction to zero.
	if (m_bClearReconstruction) {
		m_pReconstruction->setData(0.f);
	}

	// forward projection data projector
	pForwardProjector = dispatchDataProjector(
		m_pProjector, 
			SinogramMaskPolicy(m_pSinogramMask),														// sinogram mask
			ReconstructionMaskPolicy(m_pReconstructionMask),											// reconstruction mask
			DiffFPPolicy(m_pReconstruction, m_pDiffSinogram, m_pSinogram),								// forward projection with difference calculation
			m_bUseSinogramMask, m_bUseReconstructionMask, true											// options on/off
		); 

	// backprojection data projector
	pBackProjector = dispatchDataProjector(
			m_pProjector, 
			SinogramMaskPolicy(m_pSinogramMask),														// sinogram mask
			ReconstructionMaskPolicy(m_pReconstructionMask),											// reconstruction mask
			SIRTBPPolicy(m_pReconstruction, m_pDiffSinogram, 
			m_pTotalPixelWeight, m_pTotalRayLength, m_fAlpha),  // SIRT backprojection
			m_bUseSinogramMask, m_bUseReconstructionMask, true // options on/off
		); 

	// first time forward projection data projector,
	// also computes total pixel weight and total ray length
	pFirstForwardProjector = dispatchDataProjector(
			m_pProjector, 
			SinogramMaskPolicy(m_pSinogramMask),														// sinogram mask
			ReconstructionMaskPolicy(m_pReconstructionMask),											// reconstruction mask
			Combine3Policy<DiffFPPolicy, TotalPixelWeightPolicy, TotalRayLengthPolicy>(					// 3 basic operations
				DiffFPPolicy(m_pReconstruction, m_pDiffSinogram, m_pSinogram),								// forward projection with difference calculation
				TotalPixelWeightPolicy(m_pTotalPixelWeight),												// calculate the total pixel weights
				TotalRayLengthPolicy(m_pTotalRayLength)),													// calculate the total ray lengths
			m_bUseSinogramMask, m_bUseReconstructionMask, true											 // options on/off
		);

	// Perform the first forward projection to compute ray lengths and pixel weights
	m_pTotalRayLength->setData(0.0f);
	m_pTotalPixelWeight->setData(0.0f);
	pFirstForwardProjector->project();

	// iteration loop, each iteration loops over all available projections
	for (int iIteration = 0; iIteration < _iNrIterations && !m_bShouldAbort; ++iIteration) {
		// start timer
		m_ulTimer = CPlatformDepSystemCode::getMSCount();
		// forward projection to compute differences.
		pForwardProjector->project(); 
		// backprojection
		pBackProjector->project(); 
		// update iteration count
		m_iIterationCount++;

		if (m_bUseMinConstraint)
			m_pReconstruction->clampMin(m_fMinValue);
		if (m_bUseMaxConstraint)
			m_pReconstruction->clampMax(m_fMaxValue);

		// end timer
		m_ulTimer = CPlatformDepSystemCode::getMSCount() - m_ulTimer;

		// Compute metrics.
		computeIterationMetrics(iIteration, _iNrIterations, m_pDiffSinogram);
	}

	ASTRA_DELETE(pForwardProjector);
	ASTRA_DELETE(pBackProjector);
	ASTRA_DELETE(pFirstForwardProjector);


	//// check initialized
	//ASTRA_ASSERT(m_bIsInitialized);

	//m_bShouldAbort = false;

	//int iIteration = 0;

	//// data projectors
	//CDataProjectorInterface* pForwardProjector;
	//CDataProjectorInterface* pBackProjector;
	//CDataProjectorInterface* pFirstForwardProjector;

	//m_pTotalRayLength->setData(0.0f);
	//m_pTotalPixelWeight->setData(0.0f);

	//// forward projection data projector
	//pForwardProjector = dispatchDataProjector(
	//	m_pProjector, 
	//		SinogramMaskPolicy(m_pSinogramMask),														// sinogram mask
	//		ReconstructionMaskPolicy(m_pReconstructionMask),											// reconstruction mask
	//		DiffFPPolicy(m_pReconstruction, m_pDiffSinogram, m_pSinogram),								// forward projection with difference calculation
	//		m_bUseSinogramMask, m_bUseReconstructionMask, true											// options on/off
	//	); 

	//// backprojection data projector
	//pBackProjector = dispatchDataProjector(
	//		m_pProjector, 
	//		SinogramMaskPolicy(m_pSinogramMask),														// sinogram mask
	//		ReconstructionMaskPolicy(m_pReconstructionMask),											// reconstruction mask
	//		DefaultBPPolicy(m_pTmpVolume, m_pDiffSinogram), // backprojection
	//		m_bUseSinogramMask, m_bUseReconstructionMask, true // options on/off
	//	); 

	//// first time forward projection data projector,
	//// also computes total pixel weight and total ray length
	//pFirstForwardProjector = dispatchDataProjector(
	//		m_pProjector, 
	//		SinogramMaskPolicy(m_pSinogramMask),														// sinogram mask
	//		ReconstructionMaskPolicy(m_pReconstructionMask),											// reconstruction mask
	//		Combine3Policy<DiffFPPolicy, TotalPixelWeightPolicy, TotalRayLengthPolicy>(					// 3 basic operations
	//			DiffFPPolicy(m_pReconstruction, m_pDiffSinogram, m_pSinogram),								// forward projection with difference calculation
	//			TotalPixelWeightPolicy(m_pTotalPixelWeight),												// calculate the total pixel weights
	//			TotalRayLengthPolicy(m_pTotalRayLength)),													// calculate the total ray lengths
	//		m_bUseSinogramMask, m_bUseReconstructionMask, true											 // options on/off
	//	);



	//// forward projection, difference calculation and raylength/pixelweight computation
	//pFirstForwardProjector->project();

	//float32* pfT = m_pTotalPixelWeight->getData();
	//for (int i = 0; i < m_pTotalPixelWeight->getSize(); ++i) {
	//	float32 x = pfT[i];
	//	if (x < -eps || x > eps)
	//		x = 1.0f / x;
	//	else
	//		x = 0.0f;
	//	pfT[i] = x;
	//}
	//pfT = m_pTotalRayLength->getData();
	//for (int i = 0; i < m_pTotalRayLength->getSize(); ++i) {
	//	float32 x = pfT[i];
	//	if (x < -eps || x > eps)
	//		x = 1.0f / x;
	//	else
	//		x = 0.0f;
	//	pfT[i] = x;
	//}

	//// divide by line weights
	//(*m_pDiffSinogram) *= (*m_pTotalRayLength);

	//// backprojection
	//m_pTmpVolume->setData(0.0f);
	//pBackProjector->project();

	//// divide by pixel weights
	//(*m_pTmpVolume) *= (*m_pTotalPixelWeight);
	//(*m_pReconstruction) += (*m_pTmpVolume);

	//if (m_bUseMinConstraint)
	//	m_pReconstruction->clampMin(m_fMinValue);
	//if (m_bUseMaxConstraint)
	//	m_pReconstruction->clampMax(m_fMaxValue);

	//// update iteration count
	//m_iIterationCount++;
	//iIteration++;


	//

	//// iteration loop
	//for (; iIteration < _iNrIterations && !m_bShouldAbort; ++iIteration) {
	//	// forward projection and difference calculation
	//	pForwardProjector->project();

	//	// divide by line weights
	//	(*m_pDiffSinogram) *= (*m_pTotalRayLength);


	//	// backprojection
	//	m_pTmpVolume->setData(0.0f);
	//	pBackProjector->project();

	//	// divide by pixel weights
	//	(*m_pTmpVolume) *= (*m_pTotalPixelWeight);
	//	(*m_pReconstruction) += (*m_pTmpVolume);

	//	if (m_bUseMinConstraint)
	//		m_pReconstruction->clampMin(m_fMinValue);
	//	if (m_bUseMaxConstraint)
	//		m_pReconstruction->clampMax(m_fMaxValue);

	//	// update iteration count
	//	m_iIterationCount++;
	//}


	//ASTRA_DELETE(pForwardProjector);
	//ASTRA_DELETE(pBackProjector);
	//ASTRA_DELETE(pFirstForwardProjector);
}