Beispiel #1
0
        static GrayscaleFloatImage nonmax(ColorFloatImage image, float sigma)
        {
            int    offset = 2;
            string mode   = "even";

            ColorFloatImage temp_image = img_expansion(image, mode, offset);

            temp_image = gradient(image, mode);
            temp_image = img_expansion(temp_image, mode, offset);

            GrayscaleFloatImage gray_grad = temp_image.ToGrayscaleFloatImage();
            GrayscaleFloatImage dir_img   = dir(image, sigma);
            GrayscaleFloatImage out_img   = new GrayscaleFloatImage(image.Width, image.Height);
            float max_value = 0;

            for (int y = 0; y < out_img.Height; y++)
            {
                for (int x = 0; x < out_img.Width; x++)
                {
                    float M = gray_grad[x + offset, y + offset];
                    if (M > max_value)
                    {
                        max_value = M;
                    }
                    switch (dir_img[x, y])
                    {
                    case 0:     // o
                    //break;
                    case 64:    // ->
                        if (M < gray_grad[x + offset + 1, y + offset] ||
                            M < gray_grad[x + offset - 1, y + offset])
                        {
                            out_img[x, y] = 0;
                        }
                        else
                        {
                            out_img[x, y] = M;
                        }
                        break;

                    case 128:     // ^
                        if (M < gray_grad[x + offset, y + offset + 1] ||
                            M < gray_grad[x + offset, y + offset - 1])
                        {
                            out_img[x, y] = 0;
                        }
                        else
                        {
                            out_img[x, y] = M;
                        }
                        break;

                    case 192:     // /
                        if (M < gray_grad[x + offset + 1, y + offset + 1] ||
                            M < gray_grad[x + offset - 1, y + offset - 1])
                        {
                            out_img[x, y] = 0;
                        }
                        else
                        {
                            out_img[x, y] = M;
                        }
                        break;

                    case 255:     // \
                        if (M < gray_grad[x + offset - 1, y + offset + 1] ||
                            M < gray_grad[x + offset + 1, y + offset - 1])
                        {
                            out_img[x, y] = 0;
                        }
                        else
                        {
                            out_img[x, y] = M;
                        }
                        break;
                    }
                }
            }
            float mult = 255 / max_value;

            for (int y = 0; y < out_img.Height; y++)
            {
                for (int x = 0; x < out_img.Width; x++)
                {
                    out_img[x, y] *= mult;
                }
            }
            return(out_img);
        }
Beispiel #2
0
        static GrayscaleFloatImage dir(ColorFloatImage image, float sigma)
        {
            string          mode     = "even";
            int             rad      = (int)Math.Round(3 * sigma);
            ColorFloatImage temp_img = img_expansion(image, mode, rad);

            /*
             * for (int y = 1; y < temp_image_x.Height + 1; y++)
             *  for (int x = 1; x < temp_image_x.Width + 1; x++)
             *      temp_image_x[x - 1, y - 1] = temp_image[x + 1, y] + (-1) * temp_image[x, y];
             * for (int y = 1; y < temp_image_y.Height + 1; y++)
             *  for (int x = 1; x < temp_image_y.Width + 1; x++)
             *      temp_image_y[x - 1, y - 1] = temp_image[x, y + 1] + (-1) * temp_image[x, y];
             */
            GrayscaleFloatImage temp_image = temp_img.ToGrayscaleFloatImage();
            GrayscaleFloatImage out_img    = new GrayscaleFloatImage(image.Width, image.Height);

            float[,] Gx = g_kernel_deriv(sigma, "x");
            float[,] Gy = g_kernel_deriv(sigma, "y");

            for (int y = rad; y < image.Height + rad; y++)
            {
                for (int x = rad; x < image.Width + rad; x++)
                {
                    float val_x = 0, val_y = 0;

                    int m = 0;
                    for (int j = y - rad; j < y + rad + 1; j++, m++)
                    {
                        int n = 0;
                        for (int k = x - rad; k < x + rad + 1; k++, n++)
                        {
                            val_x = val_x + Gx[n, m] * temp_image[k, j];
                            val_y = val_y + Gy[n, m] * temp_image[k, j];
                        }
                    }

                    if (val_x == 0 && val_y != 0)
                    {
                        out_img[x - rad, y - rad] = 64;
                        continue;
                    }
                    double theta = Math.Atan2(val_y - rad, val_x - rad) * (180 / Math.PI);
                    if (theta <= 22.5 && theta > -22.5 || theta <= -157.5 && theta > 157.5)
                    {
                        out_img[x - rad, y - rad] = 64; // ->
                    }
                    else if (theta <= 67.5 && theta > 22.5 || theta >= -157.5 && theta < -112.5)
                    {
                        out_img[x - rad, y - rad] = 192; // /
                    }
                    else if (theta > 67.5 && theta <= 112.5 || theta >= -112.5 && theta < -67.5)
                    {
                        out_img[x - rad, y - rad] = 128; // ^
                    }
                    else if (theta > 112.5 && theta <= 157.5 || theta >= -67.5 && theta < -22.5)
                    {
                        out_img[x - rad, y - rad] = 255; // \
                    }
                }
            }
            return(out_img);
        }