Exemplo n.º 1
0
void imProcessFillHoles(const imImage* image, imImage* NewImage, int connect)
{
  // finding regions in the inverted image will isolate only the holes.
  imProcessNegative(image, NewImage);

  imImage *region_image = imImageCreate(image->width, image->height, IM_GRAY, IM_USHORT);
  if (!region_image)
    return;

  int holes_count = imAnalyzeFindRegions(NewImage, region_image, connect, 0);
  if (!holes_count)
  {
    imImageCopy(image, NewImage);
    imImageDestroy(region_image);
    return;
  }

  imushort* region_data = (imushort*)region_image->data[0];
  imbyte* dst_data = (imbyte*)NewImage->data[0];

  for (int i = 0; i < image->count; i++)
  {
    if (*region_data)
      *dst_data = 1;
    else
      *dst_data = !(*dst_data);  // Fix negative data.

    region_data++;
    dst_data++;
  }

  imImageDestroy(region_image);
}
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;
}