영상처리

AutoThreshold (Percentile) C++

park__ 2024. 12. 11. 15:43

Java -> C++

 

Code

inline double partialSum(float* y, int j) 
{	
	double x = 0;
	
	for (int i = 0; i <= j; i++)
		x += y[i];
	
	return x;
}

int Percentile(float* histo, int n_length) {
	// W. Doyle, "Operation useful for similarity-invariant pattern recognition,"
	// Journal of the Association for Computing Machinery, vol. 9,pp. 259-267, 1962.
	// ported to ImageJ plugin by G.Landini from Antti Niemisto's Matlab code (GPL)
	// Original Matlab code Copyright (C) 2004 Antti Niemisto
	// See http://www.cs.tut.fi/~ant/histthresh/ for an excellent slide presentation
	// and the original Matlab code.

	int iter = 0;
	int threshold = -1;
	double ptile = 0.5; // default fraction of foreground pixels
	double* avec = nullptr;
	avec = new double[n_length];

	for (int i = 0; i < n_length; i++)
		avec[i] = 0.0;

	double total = partialSum(histo, n_length - 1);
	double temp = 1.0;

	for (int i = 0; i < n_length; i++) 
	{
		avec[i] = abs((partialSum(histo, i) / total) - ptile);
		//IJ.log("Ptile["+i+"]:"+ avec[i]);
		
		if (avec[i] < temp) 
		{
			temp = avec[i];
			threshold = i;
		}
	}

	if (avec)
		delete[] avec;

	return threshold;
}

 

imageJ 관련 문서 : https://imagej.net/plugins/auto-threshold

github link : https://github.com/fiji/Auto_Threshold/blob/master/src/main/java/fiji/threshold/Auto_Threshold.java