private Bitmap makeBitmapFromPixels()
        {
            System.IntPtr srcScan, dstScan;
            BitmapData    srcBmData, dstBmData;
            Bitmap        dstBitmap = ImageExtract.InitPonitMethod(_SourceImage, _ImageWidth, _ImageHeight, out srcScan, out dstScan, out srcBmData, out dstBmData);

            Console.Write(_MaxVal);

            unsafe //啟動不安全代碼
            {
                byte *srcP      = (byte *)srcScan;
                byte *dstP      = (byte *)dstScan;
                int   srcOffset = srcBmData.Stride - _ImageWidth * 3;
                int   dstOffset = dstBmData.Stride - _ImageWidth * 3;

                for (int y = 0; y < _ImageHeight; y++)
                {
                    for (int x = 0; x < _ImageWidth; x++, srcP += 3, dstP += 3)
                    {
                        double val = Math.Abs(_output[x, y].Magnitude());

                        //*(dstP) = *(dstP + 1) = *(dstP + 2) = (byte)(Math.Log(val, 2) * _LOG_C);
                        double outVal = (val / _MaxVal) * 255;
                        *(dstP) = *(dstP + 1) = *(dstP + 2) = (byte)outVal;
                    }
                    srcP += srcOffset;
                    dstP += dstOffset;
                }
            }

            _SourceImage.UnlockBits(srcBmData);
            dstBitmap.UnlockBits(dstBmData);
            return(dstBitmap);
        }
        private static void HistogramEqualizationStatistics(Bitmap bitmap, ref int[,] statistics)
        {
            int width = bitmap.Width;
            int height = bitmap.Height;
            int R = 0, G = 1, B = 2;

            System.IntPtr srcScan;
            BitmapData    srcBmData;

            ImageExtract.InitPonitMethod(bitmap, width, height, out srcScan, out srcBmData);

            unsafe //啟動不安全代碼
            {
                byte *srcP      = (byte *)srcScan;
                int   srcOffset = srcBmData.Stride - width * 3;

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++, srcP += 3)
                    {
                        statistics[R, srcP[2]] += 1;
                        statistics[G, srcP[1]] += 1;
                        statistics[B, srcP[0]] += 1;
                    }
                    srcP += srcOffset;
                }
            }

            bitmap.UnlockBits(srcBmData);
        }
        private void getPixelsFromImage()
        {
            System.IntPtr srcScan;
            BitmapData    srcBmData;

            ImageExtract.InitPonitMethod(_SourceImage, _ImageWidth, _ImageHeight, out srcScan, out srcBmData);

            unsafe //啟動不安全代碼
            {
                byte *srcP      = (byte *)srcScan;
                int   srcOffset = srcBmData.Stride - _ImageWidth * 3;

                for (int y = 0; y < _ImageHeight; y++)
                {
                    for (int x = 0; x < _ImageWidth; x++, srcP += 3)
                    {
                        byte   gray = calGray(srcP[2], srcP[1], srcP[0]);
                        double val  = gray;
                        val          *= ((x + y) % 2 == 0) ? -1 : 1;
                        _pixels[x, y] = new Complex(val);
                    }
                    srcP += srcOffset;
                }
            }

            _SourceImage.UnlockBits(srcBmData);
        }
Beispiel #4
0
        private static Bitmap binarization(Bitmap bitmap, int value)
        {
            int width  = bitmap.Width;
            int height = bitmap.Height;

            System.IntPtr srcScan, dstScan;
            BitmapData    srcBmData, dstBmData;
            Bitmap        dstBitmap = ImageExtract.InitPonitMethod(bitmap, width, height, out srcScan, out dstScan, out srcBmData, out dstBmData);

            unsafe //啟動不安全代碼
            {
                byte *srcP      = (byte *)srcScan;
                byte *dstP      = (byte *)dstScan;
                int   srcOffset = srcBmData.Stride - width * 3;
                int   dstOffset = dstBmData.Stride - width * 3;


                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++, srcP += 3, dstP += 3)
                    {
                        *dstP = (srcP[0] > value) ? MAX : MIN;       //blue
                        *(dstP + 1) = (srcP[1] > value) ? MAX : MIN; //green
                        *(dstP + 2) = (srcP[2] > value) ? MAX : MIN; //red
                    }
                    srcP += srcOffset;
                    dstP += dstOffset;
                }
            }

            bitmap.UnlockBits(srcBmData);
            dstBitmap.UnlockBits(dstBmData);

            return(dstBitmap);
        }
Beispiel #5
0
        private static Bitmap HistogramEqualizationFillIn(Bitmap bitmap, int total, int[,] statistics)
        {
            int width = bitmap.Width;
            int height = bitmap.Height;
            int R = 0, G = 1, B = 2;

            System.IntPtr srcScan, dstScan;
            BitmapData    srcBmData, dstBmData;
            Bitmap        dstBitmap = ImageExtract.InitPonitMethod(bitmap, width, height, out srcScan, out dstScan, out srcBmData, out dstBmData);

            unsafe //啟動不安全代碼
            {
                byte *srcP      = (byte *)srcScan;
                byte *dstP      = (byte *)dstScan;
                int   srcOffset = srcBmData.Stride - width * 3;
                int   dstOffset = dstBmData.Stride - width * 3;

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++, srcP += 3, dstP += 3)
                    {
                        dstP[2] = (byte)(((double)statistics[R, srcP[2]] / total) * (COLOR_SIZE_RANGE - 1));
                        dstP[1] = (byte)(((double)statistics[G, srcP[1]] / total) * (COLOR_SIZE_RANGE - 1));
                        dstP[0] = (byte)(((double)statistics[B, srcP[0]] / total) * (COLOR_SIZE_RANGE - 1));
                    }
                    srcP += srcOffset;
                    dstP += dstOffset;
                }
            }

            bitmap.UnlockBits(srcBmData);
            dstBitmap.UnlockBits(dstBmData);

            return(dstBitmap);
        }
        protected Bitmap process(Bitmap srcBitmap)
        {
            int width  = srcBitmap.Width;
            int height = srcBitmap.Height;

            System.IntPtr srcScan, dstScan;
            BitmapData    srcBmData, dstBmData;
            Bitmap        dstBitmap = ImageExtract.InitPonitMethod(srcBitmap, width, height, out srcScan, out dstScan, out srcBmData, out dstBmData);


            unsafe //啟動不安全代碼
            {
                byte *srcP      = (byte *)srcScan;
                byte *dstP      = (byte *)dstScan;
                int   srcOffset = srcBmData.Stride - width * 3;
                int   dstOffset = dstBmData.Stride - width * 3;

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++, srcP += 3, dstP += 3)
                    {
                        *(dstP + ImageExtract.COLOR_R) = processColorR(srcP[ImageExtract.COLOR_R], srcP[ImageExtract.COLOR_G], srcP[ImageExtract.COLOR_B]);
                        *(dstP + ImageExtract.COLOR_G) = processColorG(srcP[ImageExtract.COLOR_R], srcP[ImageExtract.COLOR_G], srcP[ImageExtract.COLOR_B]);
                        *(dstP + ImageExtract.COLOR_B) = processColorB(srcP[ImageExtract.COLOR_R], srcP[ImageExtract.COLOR_G], srcP[ImageExtract.COLOR_B]);
                    }
                    srcP += srcOffset;
                    dstP += dstOffset;
                }
            }

            srcBitmap.UnlockBits(srcBmData);
            dstBitmap.UnlockBits(dstBmData);
            return(dstBitmap);
        }
        private static int TrainingByThresholding(Bitmap bitmap, int value, int color)
        {
            if (color > 2 || color < 0)
            {
                return(0);
            }

            int width  = bitmap.Width;
            int height = bitmap.Height;

            System.IntPtr srcScan, dstScan;
            BitmapData    srcBmData, dstBmData;

            ImageExtract.InitPonitMethod(bitmap, width, height, out srcScan, out dstScan, out srcBmData, out dstBmData);

            int minCount = 1, minTotal = 0;
            int maxCount = 1, maxTotal = 0;

            unsafe //啟動不安全代碼
            {
                byte *srcP      = (byte *)srcScan;
                byte *dstP      = (byte *)dstScan;
                int   srcOffset = srcBmData.Stride - width * 3;

                for (int x = 1; x < width - 1; x++)
                {
                    for (int y = 1; y < height - 1; y++)
                    {
                        byte current = (byte)(srcP[color]);  //blue
                        if (current > value)
                        {
                            minCount++;
                            minTotal += current;
                        }
                        else
                        {
                            maxCount++;
                            maxTotal += current;
                        }
                    }
                }
            }

            bitmap.UnlockBits(srcBmData);

            int res = ((minTotal / minCount) + (maxTotal / maxCount)) / 2;

            return(res);
        }
        private static Bitmap colouring(Bitmap source, Bitmap Colour)
        {
            //Bitmap grayImage = new Grayscale(source).Process();   // source image to gray
            Bitmap grayImage = source;

            int[,] statisticsColour = Statistics(Colour);

            int width  = grayImage.Width;
            int height = grayImage.Height;

            System.IntPtr srcScan, dstScan;
            BitmapData    srcBmData, dstBmData;
            Bitmap        dstBitmap = ImageExtract.InitPonitMethod(grayImage, width, height, out srcScan, out dstScan, out srcBmData, out dstBmData);

            unsafe //啟動不安全代碼
            {
                byte *srcP      = (byte *)srcScan;
                byte *dstP      = (byte *)dstScan;
                int   srcOffset = srcBmData.Stride - width * 3;
                int   dstOffset = dstBmData.Stride - width * 3;

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++, srcP += 3, dstP += 3)
                    {
                        int index = srcP[0] % COLOR_SIZE_RANGE;
                        *   dstP  = (byte)statisticsColour[ImageExtract.COLOR_B, index];   //blue
                        *(dstP + 1) = (byte)statisticsColour[ImageExtract.COLOR_G, index]; //green
                        *(dstP + 2) = (byte)statisticsColour[ImageExtract.COLOR_R, index]; //red
                    }
                    srcP += srcOffset;
                    dstP += dstOffset;
                }
            }

            grayImage.UnlockBits(srcBmData);
            dstBitmap.UnlockBits(dstBmData);
            return(dstBitmap);
        }
Beispiel #9
0
        private Bitmap superimposedNoise(int[,] noise)
        {
            int width  = _ImageSource.Width;
            int height = _ImageSource.Height;

            System.IntPtr srcScan, dstScan;
            BitmapData    srcBmData, dstBmData;
            Bitmap        dstBitmap = ImageExtract.InitPonitMethod(_ImageSource, width, height, out srcScan, out dstScan, out srcBmData, out dstBmData);


            unsafe //啟動不安全代碼
            {
                byte *srcP      = (byte *)srcScan;
                byte *dstP      = (byte *)dstScan;
                int   srcOffset = srcBmData.Stride - width * 3;
                int   dstOffset = dstBmData.Stride - width * 3;

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++, srcP += 3, dstP += 3)
                    {
                        int r = srcP[0] + noise[x, y];
                        int b = srcP[1] + noise[x, y];
                        int g = srcP[2] + noise[x, y];

                        *(dstP)     = (byte)((r > 255) ? 255 : (r < 0) ? 0 : r);
                        *(dstP + 1) = (byte)((b > 255) ? 255 : (b < 0) ? 0 : b);
                        *(dstP + 2) = (byte)((g > 255) ? 255 : (g < 0) ? 0 : g);
                    }
                    srcP += srcOffset;
                    dstP += dstOffset;
                }
            }

            _ImageSource.UnlockBits(srcBmData);
            dstBitmap.UnlockBits(dstBmData);
            return(dstBitmap);
        }
Beispiel #10
0
        private static Bitmap colorFunction(Bitmap srcBitmap)
        {
            int width  = srcBitmap.Width;
            int height = srcBitmap.Height;

            System.IntPtr srcScan, dstScan;
            BitmapData    srcBmData, dstBmData;
            Bitmap        dstBitmap = ImageExtract.InitPonitMethod(srcBitmap, width, height, out srcScan, out dstScan, out srcBmData, out dstBmData);

            Random random = new Random(); //亂數種子

            unsafe                        //啟動不安全代碼
            {
                byte *srcP      = (byte *)srcScan;
                byte *dstP      = (byte *)dstScan;
                int   srcOffset = srcBmData.Stride - width * 3;
                int   dstOffset = dstBmData.Stride - width * 3;

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++, srcP += 3, dstP += 3)
                    {
                        int color = srcP[0];

                        *dstP = (byte)(color / 0.3 % COLOR_SIZE_RANGE);
                        *(dstP + 1) = (byte)(color / 0.59 % COLOR_SIZE_RANGE);
                        *(dstP + 2) = (byte)(color / 0.11 % COLOR_SIZE_RANGE);
                    }
                    srcP += srcOffset;
                    dstP += dstOffset;
                }
            }

            srcBitmap.UnlockBits(srcBmData);
            dstBitmap.UnlockBits(dstBmData);
            return(dstBitmap);
        }
Beispiel #11
0
        protected Bitmap convolute(Bitmap srcBitmap, int maskWidth, int maskHeight)
        {
            int width  = srcBitmap.Width;
            int height = srcBitmap.Height;

            System.IntPtr srcScan, dstScan;
            BitmapData    srcBmData, dstBmData;
            Bitmap        dstBitmap = ImageExtract.InitPonitMethod(srcBitmap, width, height, out srcScan, out dstScan, out srcBmData, out dstBmData);

            int index = 0;

            byte[] mask1 = new byte[maskHeight * maskWidth];
            byte[] mask2 = new byte[maskHeight * maskWidth];
            byte[] mask3 = new byte[maskHeight * maskWidth];

            int offset_width  = (maskWidth / 2);
            int offset_height = (maskHeight / 2);

            unsafe //啟動不安全代碼
            {
                byte *srcP      = (byte *)srcScan;
                byte *dstP      = (byte *)dstScan;
                int   srcOffset = srcBmData.Stride - width * 3;
                int   dstOffset = dstBmData.Stride - width * 3;

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++, srcP += 3, dstP += 3)
                    {
                        //*(dstP + ImageExtract.COLOR_R) = processColorR(srcP[ImageExtract.COLOR_R], srcP[ImageExtract.COLOR_G], srcP[ImageExtract.COLOR_B]);
                        if ((x < offset_width) || (x == (width - offset_width)) || (y < offset_height) || (y >= (height - offset_height)))
                        {
                            continue;
                        }

                        index = 0;
                        for (int my = 0; my < maskHeight; my++)
                        {
                            for (int mx = 0; mx < maskWidth; mx++)
                            {
                                int cu_y = (my - offset_height);
                                int cu_x = (mx - offset_width) * 3;

                                mask1[index] = *(srcP + (cu_x) + (cu_y * (width * 3 + srcOffset)) + ImageExtract.COLOR_B);
                                mask2[index] = *(srcP + (cu_x) + (cu_y * (width * 3 + srcOffset)) + ImageExtract.COLOR_G);
                                mask3[index] = *(srcP + (cu_x) + (cu_y * (width * 3 + srcOffset)) + ImageExtract.COLOR_R);

                                ++index;
                            }
                        }

                        *(dstP + ImageExtract.COLOR_B) = maskFilter(mask1);
                        *(dstP + ImageExtract.COLOR_G) = maskFilter(mask2);
                        *(dstP + ImageExtract.COLOR_R) = maskFilter(mask3);
                    }
                    srcP += srcOffset;
                    dstP += dstOffset;
                }
            }

            srcBitmap.UnlockBits(srcBmData);
            dstBitmap.UnlockBits(dstBmData);
            return(dstBitmap);
        }
Beispiel #12
0
        private Bitmap test(int effect)
        {
            int width  = _SourceImage.Width;
            int height = _SourceImage.Height;

            System.IntPtr srcScan, dstScan;
            BitmapData    srcBmData, dstBmData;
            Bitmap        dstBitmap = ImageExtract.InitPonitMethod(_SourceImage, width, height, out srcScan, out dstScan, out srcBmData, out dstBmData);

            int offset_width  = effect;
            int offset_height = effect;
            int mask_size     = effect * effect;

            unsafe //啟動不安全代碼
            {
                byte *srcP      = (byte *)srcScan;
                byte *dstP      = (byte *)dstScan;
                int   srcOffset = srcBmData.Stride - width * 3;
                int   dstOffset = dstBmData.Stride - width * 3;



                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        //*(dstP + ImageExtract.COLOR_R) = processColorR(srcP[ImageExtract.COLOR_R], srcP[ImageExtract.COLOR_G], srcP[ImageExtract.COLOR_B]);
                        if ((x < offset_width) || (x == (width - offset_width)) || (y < offset_height) || (y >= (height - offset_height)))
                        {
                            continue;
                        }

                        int sum_r = 0, sum_g = 0, sum_b = 0;

                        for (int my = 0; my < effect; my++)
                        {
                            for (int mx = 0; mx < effect; mx++)
                            {
                                int cu_y = (my - offset_height);
                                int cu_x = (mx - offset_width) * 3;

                                sum_r += *(srcP + (cu_x) + (cu_y * (width * 3 + srcOffset)) + ImageExtract.COLOR_B);
                                sum_g += *(srcP + (cu_x) + (cu_y * (width * 3 + srcOffset)) + ImageExtract.COLOR_G);
                                sum_b += *(srcP + (cu_x) + (cu_y * (width * 3 + srcOffset)) + ImageExtract.COLOR_R);
                            }
                        }

                        for (int my = 0; my < effect; my++)
                        {
                            for (int mx = 0; mx < effect; mx++)
                            {
                                int cu_y = (my - offset_height);
                                int cu_x = (mx - offset_width) * 3;

                                *(dstP + ImageExtract.COLOR_B) = (byte)(sum_r / mask_size);
                                *(dstP + ImageExtract.COLOR_B) = (byte)(sum_g / mask_size);
                                *(dstP + ImageExtract.COLOR_B) = (byte)(sum_b / mask_size);
                            }
                        }

                        srcP += (effect * 3);
                        dstP += (effect * 3);
                    }
                    srcP += srcOffset;
                    dstP += dstOffset;
                }
            }

            _SourceImage.UnlockBits(srcBmData);
            dstBitmap.UnlockBits(dstBmData);
            return(dstBitmap);
        }
        private void _process(Bitmap srcBitmap)
        {
            int width  = srcBitmap.Width;
            int height = srcBitmap.Height;

            _ImageOfR = new Bitmap(srcBitmap);
            _ImageOfG = new Bitmap(srcBitmap);
            _ImageOfB = new Bitmap(srcBitmap);


            System.IntPtr srcScan, scan_R, scan_G, scan_B, scan_Gray;
            BitmapData    srcBmData, BmData_R, BmData_G, BmData_B, BmData_Gray;

            _ImageGray = ImageExtract.InitPonitMethod(srcBitmap, width, height, out srcScan, out scan_Gray, out srcBmData, out BmData_Gray);
            ImageExtract.InitPonitMethod(_ImageOfR, width, height, out scan_R, out BmData_R);
            ImageExtract.InitPonitMethod(_ImageOfG, width, height, out scan_G, out BmData_G);
            ImageExtract.InitPonitMethod(_ImageOfB, width, height, out scan_B, out BmData_B);

            unsafe //啟動不安全代碼
            {
                byte *srcP     = (byte *)srcScan;
                byte *dst_R    = (byte *)scan_R;
                byte *dst_G    = (byte *)scan_G;
                byte *dst_B    = (byte *)scan_B;
                byte *dst_Gray = (byte *)scan_Gray;


                int srcOffset = srcBmData.Stride - width * 3;
                int dstOffset = BmData_Gray.Stride - width * 3;

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++, srcP += 3, dst_R += 3, dst_G += 3, dst_B += 3, dst_Gray += 3)
                    {
                        byte gray = (byte)(.299 * srcP[2] + .587 * srcP[1] + .114 * srcP[0]);
                        dst_Gray[0] = dst_Gray[1] = dst_Gray[2] = gray;

                        dst_R[0] = dst_R[1] = dst_R[2] = srcP[2];
                        //dst_R[2] = srcP[2];
                        //dst_R[1] = dst_R[0] = 0;

                        dst_G[0] = dst_G[1] = dst_G[2] = srcP[1];
                        //dst_G[1] = srcP[1];
                        //dst_G[0] = dst_G[2] = 0;

                        dst_B[0] = dst_B[1] = dst_B[2] = srcP[0];
                        //dst_B[0] = srcP[0];
                        //dst_B[1] = dst_B[2] = 0;
                    }

                    srcP     += srcOffset;
                    dst_R    += dstOffset;
                    dst_G    += dstOffset;
                    dst_B    += dstOffset;
                    dst_Gray += dstOffset;
                }
            }

            srcBitmap.UnlockBits(srcBmData);
            _ImageGray.UnlockBits(BmData_Gray);
            _ImageOfR.UnlockBits(BmData_R);
            _ImageOfG.UnlockBits(BmData_G);
            _ImageOfB.UnlockBits(BmData_B);
        }