//private PictureBox pictureBox2;
 /// <summary>
 /// Form_Load函数
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void newHis_Load(object sender, EventArgs e)
 {
     Multiple        = new int[bands];
     panel1.Location = new Point(0, 0);
     panel1.Show();
     /// <summary>
     /// 添加波段选择项
     /// <summary>
     if (bands == 1)
     {
         checkedListBox1.Items.Add("band0");
     }
     else
     {
         checkedListBox1.Items.Add("Rband");
         checkedListBox1.Items.Add("Gband");
         checkedListBox1.Items.Add("Bband");
     }
     /// <summary>
     /// 计算波段像素直方图
     /// <summary>
     pixel   = new int[bands][];
     max     = new double[bands];
     min     = new double[bands];
     stretch = new double[bands];
     for (int i = 0; i < bands; i++)
     {
         perHistogram ph = new perHistogram();
         ph.histogram2(StretchTemp, ColumnCounts * LineCounts, i, out pixel[i]);
         max[i] = ph.max; min[i] = ph.min; stretch[i] = ph.stretch;
     }
     pictureBox1.Height = 300;
     pictureBox1.Width  = 300;
     pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
 }
Beispiel #2
0
        /// <summary>
        /// 画累计直方图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text != "")
            {
                //pixel = new int[256];
                //overlay = new int[256];
                Histogram f2 = new Histogram(null);
                f2.Height = 500;
                f2.Width  = 500;

                int          i   = Convert.ToInt32(comboBox1.Text.Substring(4)) - 1;;
                perHistogram phs = new perHistogram();


                phs.histogram2(BandsDataD, this.ColumnCounts * this.LineCounts, i, out pixel);
                phs.totalhistogram2(pixel, out overlay);
                f2.max     = phs.max;
                f2.min     = phs.min;
                f2.stretch = phs.stretch;
                f2.draw    = new int[256];
                f2.draw    = overlay;
                f2.Show();
            }
            else
            {
                MessageBox.Show("请选择波段!");
            }
        }
Beispiel #3
0
        /// <summary>
        /// 自适应阈值分割
        /// </summary>
        /// <param name="BandsDataD"></param>
        /// <param name="m"></param>
        /// <param name="ColumnCounts"></param>
        /// <param name="LineCounts"></param>
        /// <returns></returns>
        public int[,] selfadapt(double[,] BandsDataD, int m, int ColumnCounts, int LineCounts)
        {
            result = new int[1, ColumnCounts *LineCounts];
            int[] pixel = new int[256];
            int   T1 = 127, T2 = 0;
            int   Temp1 = 0, Temp2 = 0, Temp3 = 0, Temp4 = 0;
            /// </summary>
            /// 直方图统计
            /// </summary>
            perHistogram ph = new perHistogram();

            ph.histogram2(BandsDataD, ColumnCounts * LineCounts, m, out pixel);

            /// </summary>
            /// 循环得到结果
            /// </summary>
            while (true)
            {
                for (int i = 0; i < T1 + 1; i++)
                {
                    Temp1 += pixel[i] * i;
                    Temp2 += pixel[i];
                }
                for (int i = T1 + 1; i < 256; i++)
                {
                    Temp3 += pixel[i] * i;
                    Temp4 += pixel[i];
                }
                T2 = (Temp1 / Temp2 + Temp3 / Temp4) / 2;
                if (T1 == T2)
                {
                    break;
                }
                else
                {
                    T1 = T2;
                }
            }
            double threw = T1 * ph.stretch / 255 + ph.min;

            for (int i = 0; i < ColumnCounts * LineCounts; i++)
            {
                if (BandsDataD[m, i] > threw)
                {
                    result[0, i] = 255;
                }
                else
                {
                    result[0, i] = 0;
                }
            }
            return(result);
        }
Beispiel #4
0
        /// <summary>
        /// 任意范围拉伸
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem6_ItemClick(object sender, ItemClickEventArgs e)
        {
            int[][]  pixel   = new int[bands][];
            double[] max     = new double[bands];
            double[] min     = new double[bands];
            double[] stretch = new double[bands];
            for (int i = 0; i < bands; i++)
            {
                perHistogram ph = new perHistogram();
                ph.histogram2(BandsDataD, ColumnCounts * LineCounts, i, out pixel[i]);
                max[i]     = ph.max;
                min[i]     = ph.min;
                stretch[i] = ph.stretch;
            }
            ChooseRange CR = new ChooseRange();

            CR.bands   = bands; CR.pixel = pixel;
            CR.max     = max;
            CR.min     = min;
            CR.stretch = stretch;
            //if (bands == 1)
            //{
            //    CR.g1 = CR.pictureBox1.CreateGraphics();
            //    CR.g1.TranslateTransform(30, CR.pictureBox1.Height * 14 / 15);
            //    CR.DrawRGB(0);
            //    CR.g2 = CR.pictureBox1.CreateGraphics();
            //    CR.g2.TranslateTransform(30, CR.pictureBox1.Height * 14 / 15);
            //    CR.g2.DrawLine(System.Drawing.Pens.Gray, CR.pt2.X, 0, CR.pt2.X, -1 * (int)(CR.pictureBox1.Height / 1.2));
            //    CR.g2.DrawLine(System.Drawing.Pens.Gray, CR.pt3.X, 0, CR.pt3.X, -1 * (int)(CR.pictureBox1.Height / 1.2));

            //}
            CR.ShowDialog();
            if (CR.WinState == true)
            {
                double a = CR.a;
                double b = CR.b;
                CR.WinState = false;
                BandpixelInitialize bpi = new BandpixelInitialize();//画图数组初始化
                bpi.Initialization(out bandstemp, showdata, ColumnCounts, LineCounts, bands);

                imagestretch imgstrch = new imagestretch(ColumnCounts, LineCounts, bands);
                imgstrch.GlobalLinear(BandsDataD, bandstemp, a, b, CR.rabuttonState);
                pictureBox1.Refresh();
                map = new Bitmap(pictureBox1.Width, pictureBox1.Height);
                ImageShow ims = new ImageShow();
                ims.showimage(bandstemp, ColumnCounts, LineCounts, bands, map);
                pictureBox1.Image = map;
                //CR.ShowDialog();
            }
            isValue = false;
        }
Beispiel #5
0
        /// <summary>
        /// 交互阈值分割
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem7_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (bands == 1)
            {
                perHistogram ph    = new perHistogram();
                int[]        pixel = new int[256];
                ph.histogram2(this.BandsDataD, ColumnCounts * LineCounts, 0, out pixel);
                Histogram h2 = new Histogram(pictureBox1);
                h2.Windowname = this.windowname;
                h2.flag       = 1;
                h2.max        = ph.max; h2.min = ph.min; h2.stretch = ph.stretch;
                h2.draw       = pixel;
                isValue       = true;
                h2.Show();

                //保存数据
                //map = h2.map;
                //int R = map.GetPixel(0, 0).R;
            }
            else
            {
                MessageBox.Show("请选择单波段图像窗口!");
            }
        }
        /// <summary>
        /// 2%Linear
        /// </summary>
        /// <param name="BandsDataD"></param>
        /// <param name="ColumnCounts"></param>
        /// <param name="LineCounts"></param>
        /// <param name="m">波段号</param>
        public int[,] TwoPercentStretch(double[,] BandsDataD)
        {
            int[,] bandstemp = new int[bands, ColumnCounts *LineCounts];
            int[]  pixel = new int[256];
            double c = 1; double d = 255;

            for (int p = 0; p < bands; p++)
            {
                int          a = 0; int b = 0;
                int          count2  = 0;
                int          count98 = 0;
                perHistogram ph      = new perHistogram();
                ph.histogram2(BandsDataD, ColumnCounts * LineCounts, p, out pixel);

                for (int i = 0; i < 256; i++)
                {
                    for (int j = 1; j <= pixel[i]; j++)
                    {
                        count2++;
                        count98++;
                        if (count2 == Convert.ToInt32(0.02 * ColumnCounts * LineCounts))
                        {
                            a = i;
                        }
                        if (count98 == Convert.ToInt32(0.98 * ColumnCounts * LineCounts))
                        {
                            b = i;
                        }
                    }
                }
                //特殊情况处理
                if (a == b)
                {
                    if (a <= 0)
                    {
                        for (int i = 0; i < ColumnCounts * LineCounts; i++)
                        {
                            bandstemp[p, i] = 0;
                        }
                    }
                    else if (a >= 255)
                    {
                        for (int i = 0; i < ColumnCounts * LineCounts; i++)
                        {
                            bandstemp[p, i] = 255;
                        }
                    }
                    else
                    {
                    }
                }
                //空间范围内原灰度级不连续、值差别分布情况
                else
                {
                    for (int i = 0; i < ColumnCounts * LineCounts; i++)
                    {
                        int Comp = (int)Math.Ceiling((BandsDataD[p, i] - ph.min) * 255 / ph.stretch);
                        if (Comp < a)
                        {
                            bandstemp[p, i] = (int)c;
                        }
                        else if (Comp > b)
                        {
                            bandstemp[p, i] = (int)d;
                        }
                        else if (Comp >= a && Comp <= b)
                        {
                            double a1 = (a - 1) * ph.stretch / 255 + ph.min;
                            double b1 = b * ph.stretch / 255 + ph.min;
                            bandstemp[p, i] = (int)((d - c) / (b1 - a1) * (BandsDataD[p, i] - a1) + c);
                        }
                    }
                }
            }
            return(bandstemp);
        }
        /// <summary>
        /// 直方图均衡化
        /// </summary>
        /// <param name="BandsDataD"></param>
        /// <param name="m"></param>
        public void Equalization(double[,] BandsDataD)
        {
            int N;//像素点个数
            int L;

            int[]    pixel;    //像素直方图
            int[]    overlay;  //累积直方图
            double[] overlayD; //累积频率数组
            double[] newpixel; //灰度映射数组
            for (int m = 0; m < bands; m++)
            {
                /// </summary>
                /// 统计图像像素点个数
                /// </summary>
                L        = 0;
                N        = 0;
                pixel    = new int[256];
                overlay  = new int[256];
                overlayD = new double[256];
                newpixel = new double[256];
                for (int i = 0; i < ColumnCounts * LineCounts; i++)
                {
                    if (BandsDataD[m, i] != Double.NaN)
                    {
                        N++;
                    }
                }
                //N++;

                perHistogram ph = new perHistogram();
                /// </summary>
                ///计算像素直方图
                /// </summary>
                ph.histogram2(BandsDataD, ColumnCounts * LineCounts, m, out pixel);
                /// </summary>
                ///计算累计直方图overlay[,]
                /// </summary>
                for (int i = 1; i < 256; i++)
                {
                    for (int j = 1; j <= i; j++)
                    {
                        if (pixel[i] > 0)
                        {
                            overlay[i] += pixel[j];
                        }
                    }
                }

                for (int i = 0; i < 256; i++)
                {
                    if (pixel[i] > 0)
                    {
                        L++;
                    }
                }


                //原灰度级像素累积频率和理论像素累积频率差值数组。
                double[,] srcMin = new double[256, 256];
                double min; int Y = 0;

                /// </summary>
                /// 新像素值
                /// </summary>
                for (int i = 1; i < 256; i++)
                {
                    overlayD[i] = Convert.ToDouble(overlay[i]) / N;
                    for (int j = 1; j < L; j++)
                    {
                        srcMin[i, j] = Math.Abs(overlayD[i] - (double)j / L);
                    }
                }
                for (int i = 1; i < 256; i++)
                {
                    min = 9999;
                    for (int j = 1; j < L; j++)
                    {
                        if (min > srcMin[i, j])
                        {
                            min = srcMin[i, j];
                            Y   = j;//得到灰度映射
                        }
                    }
                    /// </summary>
                    /// 得到最接近的灰度级映射
                    /// </summary>
                    newpixel[i] = Y;
                }

                for (int i = 1; i < 256; i++)
                {
                    newpixel[i] = (newpixel[i] - 1) / (L - 1) * ph.stretch + ph.min;
                }
                /// </summary>
                /// 替换像素
                /// </summary>
                for (int i = 0; i < ColumnCounts * LineCounts; i++)
                {
                    int k = (int)Math.Ceiling((BandsDataD[m, i] - ph.min) / ph.stretch * (double)255);
                    stretResult[m, i] = newpixel[k];
                }
            }
        }
        /// <summary>
        /// 直方图匹配(规定化)
        /// </summary>
        /// <param name="BandDataD1">待匹配图像</param>
        /// <param name="BandDataD2">目标图像</param>
        /// <param name="ColumnCounts">列数</param>
        /// <param name="LineCounts">行数</param>
        /// <param name="bands">波段数</param>
        /// <param name="Reflect">灰度级映射</param>
        public void HistogramMatch(double[,] BandDataD1, double[,] BandDataD2, int Tarbands)
        {
            double[,] Reflect = new double[bands, 256];
            //目标图像波段序号
            int m;

            /// </summary>
            /// 各波段...
            /// </summary>
            for (int k = 0; k < bands; k++)
            {
                if (Tarbands == 1)
                {
                    m = 0;
                }
                else
                {
                    m = k;
                }

                int[] orgin = new int[256]; int[] target = new int[256]; int[] pixel1 = new int[256]; int[] pixel2 = new int[256];
                int[,] srcMin = new int[256, 256]; int min; int Y = 0;
                /// </summary>
                /// 计算原图像累计直方图
                /// </summary>
                perHistogram ph1 = new perHistogram();
                ph1.histogram2(BandDataD1, ColumnCounts * LineCounts, k, out pixel1);
                for (int i = 1; i < 256; i++)//计算原图像累计直方图
                {
                    for (int j = 1; j <= i; j++)
                    {
                        if (pixel1[i] > 0)
                        {
                            orgin[i] += pixel1[j];
                        }
                    }
                }

                /// </summary>
                /// 计算目标图像累计直方图
                /// </summary>
                perHistogram ph2 = new perHistogram();
                ph2.histogram2(BandDataD2, ColumnCounts * LineCounts, m, out pixel2);
                for (int i = 1; i < 256; i++)//计算目标图像累计直方图
                {
                    for (int j = 1; j <= i; j++)
                    {
                        if (pixel2[i] > 0)
                        {
                            target[i] += pixel2[j];
                        }
                    }
                }

                /// </summary>
                /// 两直方图直方图互减,得到[255,255]二维差值
                /// </summary>
                for (int i = 1; i < 256; i++)
                {
                    for (int j = 1; j < 256; j++)
                    {
                        srcMin[i, j] = Math.Abs(orgin[i] - target[j]);
                    }
                }
                /// </summary>
                /// 待匹配图像每灰度级对应目标图像差值最小的灰度级
                /// </summary>
                for (int i = 1; i < 256; i++)
                {
                    min = 9999;
                    for (int j = 1; j < 256; j++)
                    {
                        if (orgin[i] > 0 && target[j] > 0)
                        {
                            if (min > srcMin[i, j])
                            {
                                min = srcMin[i, j];
                                Y   = j;
                            }
                        }
                    }
                    /// </summary>
                    /// 得到最接近的灰度级映射
                    /// </summary>
                    Reflect[k, i] = (double)Y * ph2.stretch / (double)255 + ph2.min;
                }
                /// </summary>
                /// 开始映射
                /// </summary>
                for (int j = 0; j < ColumnCounts * LineCounts; j++)
                {
                    int T = (int)Math.Ceiling((BandDataD1[k, j] - ph1.min) / (ph1.max - ph1.min) * 255);
                    stretResult[k, j] = (Reflect[k, T] - ph2.min) / (ph2.max - ph2.min) * (ph1.max - ph1.min) + ph1.min;
                }
            }
        }