Exemplo n.º 1
0
/*****************************************************************************\
 image:Duplicate()
\*****************************************************************************/
static int imluaImageDuplicate (lua_State *L)
{
  imImage* image = imlua_checkimage(L, 1);
  imImage *new_image = imImageDuplicate(image);
  imlua_pushimage(L, new_image);
  return 1;
}
Exemplo n.º 2
0
int imProcessBrinkThreshold(const imImage* image, imImage* dest, bool white_is_255 )
{
	imImage *src = imImageDuplicate( image );
     
	if ( !white_is_255 )
		imProcessNegative( src, src );
	
	int i, j;
	int Topt = 0;

	double p[MAX_GRAY];				// pmf (i.e. normalized histogram)
	unsigned long histo[MAX_GRAY];			// from imlib: "Histogram is always 256 positions long"

	double m_f[MAX_GRAY];			// first foreground moment
	double m_b[MAX_GRAY];			// first background moment

	double tmp0[MAX_GRAY][MAX_GRAY];			
	double tmp1[MAX_GRAY][MAX_GRAY];			
	double tmp3[MAX_GRAY][MAX_GRAY];			
	double tmp4[MAX_GRAY][MAX_GRAY];			

	double tmpVec1[MAX_GRAY];
	double tmpVec2[MAX_GRAY];

	imCalcGrayHistogram(src, histo, NON_CUMULATIVE);  	// gray histogram computed

	double invHistSum = 1.0 / vecSum(histo, MAX_GRAY);	// inverse of the sum

	for (i = 0; i < MAX_GRAY; ++i)
		p[i] = histo[i] * invHistSum;		// equivalent to dividing by the sum, but faster!


	m_f[0] = 0.0;
	for (i = 1; i < MAX_GRAY; ++i)			// m_f = cumsum(g .* p).'; 
		m_f[i] = i * p[i] + m_f[i - 1];

	memcpy(m_b, m_f, VEC_DBL_SZ);		// m_b = m_f(end) - m_f; 

	for (i = 0; i < MAX_GRAY; ++i)
		m_b[i] = m_f[MAX_GRAY - 1] - m_b[i];		

	/****************** END OF BINAR_ASHLEY PORTION, START OF ENTROPY_BRINK********************************/

	for (i = 0; i < MAX_GRAY; ++i)		// tmp0 = m_f_rep ./ g_rep;
	{
		for (j = 0; j < MAX_GRAY; ++j)
		{
			tmp0[i][j] = m_f[j] / i;

			if ((m_f[j] == 0) || (i == 0)) 
			{
				tmp1[i][j] = 0.0;				// replace inf or NaN values with 0
				tmp3[i][j] = 0.0;
			}
			else
			{
				tmp1[i][j] = log(tmp0[i][j]);
				tmp3[i][j] = log(1.0 / tmp0[i][j]);;
			}
			tmp4[i][j] = p[i] * (m_f[j] * tmp1[i][j] + i * tmp3[i][j]);
		}
	}

	diagCumSum(tmpVec1, tmp4);	// tmpVec is now the diagonal of the cumulative sum of tmp4

	// same operation but for background moment, NOTE: tmp1 through tmp4 get overwritten
	for (i = 0; i < MAX_GRAY; ++i)		
	{
		for (j = 0; j < MAX_GRAY; ++j)
		{
			tmp0[i][j] = m_b[j] / i;	// tmpb0 = m_b_rep ./ g_rep;

			if ((m_b[j] == 0) || (i == 0)) 
			{
				tmp1[i][j] = 0.0;				// replace inf or NaN values with 0
				tmp3[i][j] = 0.0;
			}
			else
			{
				tmp1[i][j] = log(tmp0[i][j]);
				tmp3[i][j] = log(1.0 / tmp0[i][j]);;
			}
			tmp4[i][j] = p[i] * (m_b[j] * tmp1[i][j] + i * tmp3[i][j]);
		}
	}

	sumMinusDiagCumSum(tmpVec2, tmp4);		// sum columns, subtract diagonal of cumsum of tmp4 

	for (i = 0; i < MAX_GRAY; ++i)
		tmpVec1[i] += tmpVec2[i];

	Topt = calcTopt(m_f, m_b, tmpVec1);		// DO I NEED TO ADD ONE?

	imProcessThreshold(src, dest, Topt, true);
	imProcessBitwiseNot(dest, dest);		// HACK ALERT: HAVE TO FLIP BITS

	return Topt;
}