找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2959|回复: 0
打印 上一主题 下一主题
收起左侧

Freeimage自动阈值分割

[复制链接]
跳转到指定楼层
楼主
ID:77367 发表于 2015-4-18 20:50 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Freeimage的图片切割分块的处理。。。。尼玛突然感到头好晕。。。。。。。。。。。




/////////////////////////////////////////////////////////////////////////////////////////
//源代码

1、otsu阈值分割


/*
parameter:   *image              --- buffer for image
rows, cols        --- size of image
     x0, y0, dx, dy   --- region of vector used for computing threshold
vvv                  --- debug option, is 0, no debug information outputed
*/

/*======================================================================*/
/*   OTSU global thresholding routine                                   */
/*   takes a 2D unsigned char array pointer, number of rows, and        */
/*   number of cols in the array. returns the value of the threshold    */
/*======================================================================*/
int CStatintDlg::otsu (unsigned char *image, int rows, int cols, int x0, int y0, int dx, int dy, int vvv)
{
unsigned char *np;      // 图像指针
int thresholdValue=1; // 阈值
int ihist[256];             // 图像直方图,256个点
int i, j, k;          // various counters
int n, n1, n2, gmin, gmax;
double m1, m2, sum, csum, fmax, sb;
// 对直方图置零...
memset(ihist, 0, sizeof(ihist));
gmin=255; gmax=0;
// 生成直方图
for (i = y0 + 1; i < y0 + dy - 1; i++) {
np = &image[i*cols+x0+1];
for (j = x0 + 1; j < x0 + dx - 1; j++) {
ihist[*np]++;
if(*np > gmax) gmax=*np;
if(*np < gmin) gmin=*np;
np++; /* next pixel */
}
}
// set up everything
sum = csum = 0.0;
n = 0;
for (k = 0; k <= 255; k++) {
sum += (double) k * (double) ihist[k];    /* x*f(x) 质量矩*/
n   += ihist[k];                                         /*  f(x)    质量    */
}
if (!n) {
// if n has no value, there is problems...
fprintf (stderr,"NOT NORMAL thresholdValue = 160\n");
return (160);
}
// do the otsu global thresholding method
fmax = -1.0;
n1 = 0;
for (k = 0; k < 255; k++) {
n1 += ihist[k];
if (!n1) { continue; }
n2 = n - n1;
if (n2 == 0) { break; }
csum += (double) k *ihist[k];
m1 = csum / n1;
m2 = (sum - csum) / n2;
sb = (double) n1 *(double) n2 *(m1 - m2) * (m1 - m2);
/* bbg: note: can be optimized. */
if (sb > fmax) {
fmax = sb;
thresholdValue = k;
}
}
// at this point we have our thresholding value
// debug code to display thresholding values
if ( vvv & 1 )
fprintf(stderr,"# OTSU: thresholdValue = %d gmin=%d gmax=%d\n",
thresholdValue, gmin, gmax);
return(thresholdValue);
}


//快速判别
void CStatintDlg::OnBnClickedDiscrimination()
{
if(m_kOrgImage.IsEmpty())  //there is no image we should exit from here
return;

size_t bpp = m_kOrgImage.GetBPP();
    if(1 == bpp) return;

m_kOrgImage.ConvertToGreyscale();
BYTE *lpBits = m_kOrgImage.GetBits();
    unsigned int width = m_kOrgImage.GetWidth();
unsigned int height = m_kOrgImage.GetHeight();

   m_Threshold = otsu(lpBits,height,width,0,0,width,height,0);
   TRACE1("otsu=%d\n\n",m_Threshold);

   ASSERT(m_Threshold >0 && m_Threshold < 256);
   //////////////////////////////////////////////////////////////////////////
   unsigned int v = ( width - 1 ) / m_nBlockWidth + 1;
   unsigned int h = ( height - 1 ) / m_nBlockheight + 1;

  dMatrix Ratiomat(h,v);   //比列矩阵
  uMatrix BitMat(h,v);     //0-1矩阵  

   long sFront,sBack;
   double fRatio;

   for(unsigned int j = 0; j < h; j ++ )
   {
   for(unsigned int i = 0; i < v; i ++ )
   {
   RECT rt;
   rt.left = i * m_nBlockWidth;
   rt.top = j * m_nBlockheight;
   rt.right = rt.left + m_nBlockWidth;
   rt.bottom = rt.top + m_nBlockheight;
   if( rt.right > (LONG)width ){
   rt.right = width;
   }

   if( rt.bottom > (LONG)height ){
   rt.bottom = height;
   }

   FIBITMAP* pCut = ::FreeImage_Copy( m_kOrgImage.GetHandle(), rt.left, rt.top, rt.right, rt.bottom );
    ::FreeImage_GetHistogram(pCut , m_hgray, FICC_RGB);

sFront = 0;
sBack = 0;
for (unsigned int k = 0;k<256;k++)
{
if(k > m_Threshold)
sFront += m_hgray[k];
else
sBack += m_hgray[k];
}

if(0 == sBack)   //防止处以0
fRatio = 1.0f;
else
    fRatio = (double)sFront/(double)sBack;

            Ratiomat[j] = fRatio;

if ( (abs(fRatio - m_fLowVal) > 1e-20 ) && (abs(fRatio - m_fHighVal) > 1e-20 ))
BitMat[j] = 1;
else
BitMat[j] = 0;


//举例怎么取矩阵的值
TRACE3("Ratiomat[%d][%d]=%f%%\n",j,i,fRatio*100);

   if( pCut )
   ::FreeImage_DestroyICCProfile( pCut );

   }
   }


   //////////////////////////////////////////////////////////////////////////

   m_kOrgImage.Threshold(m_Threshold);

   UpdateImage();

}






分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表