unsafe public int GetThreshValue(Bitmap image)
        {
            BitmapData bd = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.WriteOnly, image.PixelFormat);
            byte* pt = (byte*)bd.Scan0;
            int[] pixelNum = new int[256];           //图象直方图,共256个点
            byte color;
            byte* pline;
            int n, n1, n2;
            int total;                              //total为总和,累计值
            double m1, m2, sum, csum, fmax, sb;     //sb为类间方差,fmax存储最大方差值
            int k, t, q;
            int threshValue = 1;                      // 阈值
            int step = 1;
            switch (image.PixelFormat)
            {
                case PixelFormat.Format24bppRgb:
                    step = 3;
                    break;
                case PixelFormat.Format32bppArgb:
                    step = 4;
                    break;
                case PixelFormat.Format8bppIndexed:
                    step = 1;
                    break;
            }
           //生成直方图
            for (int i = 0; i < image.Height; i++)
            {
                pline = pt + i * bd.Stride;
                for (int j = 0; j < image.Width; j++)
                {
                    color = *(pline + j * step);   //返回各个点的颜色,以RGB表示
                    pixelNum[color]++;            //相应的直方图加1
                }
            }
          //直方图平滑化
            for (k = 0; k <= 255; k++)
            {
                total = 0;
                for (t = -2; t <= 2; t++)              //与附近2个灰度做平滑化,t值应取较小的值
                {
                    q = k + t;
                    if (q < 0)                     //越界处理
                        q = 0;
                    if (q > 255)                   
                        q = 255;
                    total = total + pixelNum[q];    //total为总和,累计值
                }
                pixelNum[k] = (int)((float)total / 5.0 + 0.5);    //平滑化,左边2个+中间1个+右边2个灰度,共5个,所以总和除以5,后面加0.5是用修正值
            }
            //求阈值
            sum = csum = 0.0;
            n = 0;
            //计算总的图象的点数和质量矩,为后面的计算做准备
            for (k = 0; k <= 255; k++)
            {
                sum += (double)k * (double)pixelNum[k];     //x*f(x)质量矩,也就是每个灰度的值乘以其点数(归一化后为概率),sum为其总和
                n += pixelNum[k];                       //n为图象总的点数,归一化后就是累积概率
            }
            
            fmax = -1.0;                         //类间方差sb不可能为负,所以fmax初始值为-1不影响计算的进行
            n1 = 0;
            for (k = 0; k < 255; k++)                  //对每个灰度(从0到255)计算一次分割后的类间方差sb
            {
                n1 += pixelNum[k];                //n1为在当前阈值遍前景图象的点数
                if (n1 == 0) { continue; }            //没有分出前景后景
                n2 = n - n1;                        //n2为背景图象的点数
                if (n2 == 0) { break; }              //n2为0表示全部都是后景图象,与n1=0情况类似,之后的遍历不可能使前景点数增加,所以此时可以退出循环
                csum += (double)k * pixelNum[k];    //前景的“灰度的值*其点数”的总和
                m1 = csum / n1;                     //m1为前景的平均灰度
                m2 = (sum - csum) / n2;               //m2为背景的平均灰度
                sb = (double)n1 * (double)n2 * (m1 - m2) * (m1 - m2);   //sb为类间方差
                if (sb > fmax)                  //如果算出的类间方差大于前一次算出的类间方差
                {
                    fmax = sb;                    //fmax始终为最大类间方差(otsu)
                    threshValue = k;              //取最大类间方差时对应的灰度的k就是最佳阈值
                }
            }
            image.UnlockBits(bd);
            image.Dispose();
            return threshValue;
        }我现在直接把这个函数COPY到TestDoc.cpp最后一个空的地方,也没定义什么变量,因为对这些函数的定义不知道,现在编译时出错(我也知道会报错:(),这种外来的源码如何加入到自已的软件里面去,请高手指导一下,怎么使这函能正常加到我的程序中去,不报错类似的错误如下
TestDoc.cpp(3216) : error C2501: 'unsafe' : missing storage-class or type specifiers
TestDoc.cpp(3216) : error C2143: syntax error : missing ';' before 'public'
TestDoc.cpp(3217) : error C2447: missing function header (old-style formal list?)我是新手,请稍详细解释一下
第一次来发贴,全分送给第一个解决这个问题的高手

解决方案 »

  1.   

    已删除,但还有下面4个错误
    TestDoc.cpp(3215) : error C2065: 'Bitmap' : undeclared identifier
    TestDoc.cpp(3215) : error C2146: syntax error : missing ')' before identifier 'image'
    TestDoc.cpp.cpp(3216) : error C2143: syntax error : missing ';' before '{'
    TestDoc.cpp.cpp(3216) : error C2447: missing function header (old-style formal list?)
      

  2.   

    把参数Bitmap image改成CBitmap image编译系统才能认识.
      

  3.   


    改了变成36处错误,主要是TestDoc.cpp(3217) : error C2065: 'BitmapData' : undeclared identifier
    TestDoc.cpp(3217) : error C2146: syntax error : missing ';' before identifier 'bd'
    TestDoc.cpp(3217) : error C2065: 'bd' : undeclared identifier
    TestDoc.cpp(3217) : error C2039: 'LockBits' : is not a member of 'CBitmap'
            C:\Program Files\Microsoft Visual Studio\VC98\MFC\INCLUDE\afxwin.h(524) : see declaration of 'CBitmap'
    TestDoc.cpp(3217) : error C2061: syntax error : identifier 'Rectangle'
    TestDoc.cpp(3218) : error C2228: left of '.Scan0' must have class/struct/union type
    TestDoc.cpp(3219) : warning C4091: '' : ignored on left of 'int' when no variable is declared
    TestDoc.cpp(3219) : error C2143: syntax error : missing ';' before '['
    TestDoc.cpp(3219) : error C2143: syntax error : missing ';' before '['
    TestDoc.cpp(3228) : error C2039: 'PixelFormat' : is not a member of 'CBitmap'
            C:\Program Files\Microsoft Visual Studio\VC98\MFC\INCLUDE\afxwin.h(524) : see declaration of 'CBitmap'
    TestDoc.cpp(3229) : error C2143: syntax error : missing ')' before '{'
    TestDoc.cpp(3229) : error C2143: syntax error : missing ';' before ')'
    TestDoc.cpp(3229) : warning C4060: switch statement contains no 'case' or 'default' labels
    TestDoc.cpp(3229) : error C2143: syntax error : missing ';' before '{'
    TestDoc.cpp(3230) : error C2228: left of '.Format24bppRgb' must have class/struct/union type
    TestDoc.cpp(3230) : error C2046: illegal case
    TestDoc.cpp(3232) : error C2043: illegal break
    TestDoc.cpp(3233) : error C2228: left of '.Format32bppArgb' must have class/struct/union type
    TestDoc.cpp(3233) : error C2046: illegal case
    TestDoc.cpp(3235) : error C2043: illegal break
    TestDoc.cpp(3236) : error C2228: left of '.Format8bppIndexed' must have class/struct/union type
    TestDoc.cpp(3236) : error C2046: illegal case
    TestDoc.cpp(3238) : error C2043: illegal break
    TestDoc.cpp(3241) : error C2039: 'Height' : is not a member of 'CBitmap'
            C:\Program Files\Microsoft Visual Studio\VC98\MFC\INCLUDE\afxwin.h(524) : see declaration of 'CBitmap'
    TestDoc.cpp(3243) : error C2228: left of '.Stride' must have class/struct/union type
    TestDoc.cpp(3244) : error C2039: 'Width' : is not a member of 'CBitmap'
            C:\Program Files\Microsoft Visual Studio\VC98\MFC\INCLUDE\afxwin.h(524) : see declaration of 'CBitmap'
    TestDoc.cpp(3247) : error C2065: 'pixelNum' : undeclared identifier
    TestDoc.cpp(3247) : error C2109: subscript requires array or pointer type
    TestDoc.cpp(3247) : error C2105: '++' needs l-value
    TestDoc.cpp(3261) : error C2109: subscript requires array or pointer type
    TestDoc.cpp(3263) : error C2109: subscript requires array or pointer type
    TestDoc.cpp(3263) : error C2106: '=' : left operand must be l-value
    TestDoc.cpp(3271) : error C2109: subscript requires array or pointer type
    TestDoc.cpp(3272) : error C2109: subscript requires array or pointer type
    TestDoc.cpp(3279) : error C2109: subscript requires array or pointer type
    TestDoc.cpp(3283) : error C2109: subscript requires array or pointer type
    TestDoc.cpp(3293) : error C2039: 'UnlockBits' : is not a member of 'CBitmap'
            C:\Program Files\Microsoft Visual Studio\VC98\MFC\INCLUDE\afxwin.h(524) : see declaration of 'CBitmap'
    TestDoc.cpp(3294) : error C2039: 'Dispose' : is not a member of 'CBitmap'
            C:\Program Files\Microsoft Visual Studio\VC98\MFC\INCLUDE\afxwin.h(524) : see declaration of 'CBitmap'
    Error executing cl.exe.可是哪个没定义?!!
      

  4.   

    C#里的Bitmap对应的是GDI+中的Bitmap,所以要做三件事:
    1、#include "gdiplus.h"
    2、#pragma comment(lib, "gdiplus.lib")
    3、using namespace Gdiplus;还有好多语言上的细节差别要修改,例如类成员函数的申明,.要变成->,等等