Exemple #1
0
        /// <summary>
        ///  实现功能: 基于Frequency-tuned 的图像显著性检测
        ///    参考论文: Frequency-tuned Salient Region Detection, Radhakrishna Achantay, Page 4-5, 2009 CVPR
        ///               http://ivrgwww.epfl.ch/supplementary_material/RK_CVPR09/
        ///    参考论文作者RGB转LAB的浮点版本
        /// </summary>
        /// <param name="srcBitmap"></param>
        /// <returns></returns>
        public static Bitmap SalientRegionDetectionBasedOnFT(Bitmap srcBitmap)
        {
            int        width = srcBitmap.Width, height = srcBitmap.Height;
            Rectangle  rect      = new Rectangle(0, 0, width, height);
            BitmapData srcBmData = srcBitmap.LockBits(rect,
                                                      ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            Bitmap     dstBitmap = BasicMethodClass.CreateGrayscaleImage(width, height);
            BitmapData dstBmData = dstBitmap.LockBits(rect,
                                                      ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

            IntPtr srcScan = srcBmData.Scan0;
            IntPtr dstScan = dstBmData.Scan0;

            unsafe
            {
                double * srcP = (double *)(void *)srcScan;
                byte *   dstP = (byte *)(void *)dstScan;
                int      Index = 0, CurIndex = 0, srcStride = srcBmData.Stride, dstStride = dstBmData.Stride, X, Y;
                double   MeanL = 0, MeanA = 0, MeanB = 0;
                double[] LabF;
                double[] DistMap = new double[dstStride * height];
                LabF = BasicMethodClass.sRGBtoXYZ(srcBmData, width, height, out MeanL, out MeanA, out MeanB);
                //     LabF = BasicMethodClass.GaussianSmooth(LabF, width, srcBmData.Stride, srcBmData.Height);
                double maxval = 0;
                double minval = double.MaxValue;
                int    i      = 0;
                for (Y = 0; Y < height; Y++)
                {
                    Index = Y * srcStride;

                    CurIndex = Y * dstStride;   //
                    for (X = 0; X < width; X++) //    计算像素的显著性
                    {                           //此处有时会出现数组越界,原因暂时未知(已解决,目标8位图有3字节的对齐)
                        double curValue = (MeanL - LabF[Index]) * (MeanL - LabF[Index]) + (MeanA - LabF[Index + 1]) * (MeanA - LabF[Index + 1]) + (MeanB - LabF[Index + 2]) * (MeanB - LabF[Index + 2]);
                        DistMap[CurIndex] = curValue;
                        Index            += 3;
                        CurIndex++;
                        if (maxval < curValue)
                        {
                            maxval = curValue;
                        }
                        if (minval > curValue)
                        {
                            minval = curValue;
                        }
                        i++;
                    }
                }
                BasicMethodClass.Normalize(DistMap, dstBmData.Stride, height, maxval, minval, dstP);
                //写入序列化函数中。。
            }
            srcBitmap.UnlockBits(srcBmData);
            dstBitmap.UnlockBits(dstBmData);
            return(dstBitmap);
        }
Exemple #2
0
        public static Bitmap SalientRegionDetectionBasedOnFT(Bitmap srcBitmap, float distcof)
        {
            int        width = srcBitmap.Width, height = srcBitmap.Height;
            Rectangle  rect      = new Rectangle(0, 0, width, height);
            BitmapData srcBmData = srcBitmap.LockBits(rect,
                                                      ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            Bitmap     dstBitmap = BasicMethodClass.CreateGrayscaleImage(width, height);
            BitmapData dstBmData = dstBitmap.LockBits(rect,
                                                      ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

            IntPtr srcScan = srcBmData.Scan0;
            IntPtr dstScan = dstBmData.Scan0;

            unsafe
            {
                double * srcP = (double *)(void *)srcScan;
                byte *   dstP = (byte *)(void *)dstScan;
                int      Index = 0, CurIndex = 0, srcStride = srcBmData.Stride, dstStride = dstBmData.Stride, X, Y;
                double   MeanL = 0, MeanA = 0, MeanB = 0;
                double[] LabF;
                double[] DistMap = new double[dstStride * height];
                LabF = BasicMethodClass.sRGBtoXYZ(srcBmData, width, height, out MeanL, out MeanA, out MeanB);
                //     LabF = BasicMethodClass.GaussianSmooth(LabF, width, srcBmData.Stride, srcBmData.Height);

                //加入显著位置权重
                int wk = (int)(width / 2), hk = (int)(height / 2);

                for (Y = 0; Y < height; Y++)
                {
                    Index = Y * srcStride;

                    CurIndex = Y * dstStride;   //
                    for (X = 0; X < width; X++) //    计算像素的显著性
                    {                           //此处有时会出现数组越界,原因暂时未知(已解决,目标8位图有3字节的对齐)
                        DistMap[CurIndex]  = (MeanL - LabF[Index]) * (MeanL - LabF[Index]) + (MeanA - LabF[Index + 1]) * (MeanA - LabF[Index + 1]) + (MeanB - LabF[Index + 2]) * (MeanB - LabF[Index + 2]);
                        DistMap[CurIndex] *= Math.Max(1 - (Math.Abs(wk - X) + Math.Abs(hk - Y)) / (wk + hk + 1.0), distcof);
                        Index             += 3;
                        CurIndex++;
                    }
                }
                DistMap = BasicMethodClass.Normalize(DistMap, dstBmData.Stride, height, dstP);
                //写入序列化函数中。。
            }
            srcBitmap.UnlockBits(srcBmData);
            dstBitmap.UnlockBits(dstBmData);
            return(dstBitmap);
        }