Retinex algorithm implementation

2019-04-01 17:14发布

I need to implement Single Scale retinex and multiscale retinex algorithm in C#,
I searched a bit but couldn't find any useful practice projects and artilces with code
As I understood correctly I should:

  1. Convert RGB to YUV
  2. Blur the image using Gaussian blur filter
  3. Use I'(x, y) = 255*log10( I(x, y)/G(x, y) ) + 127.5
    I - is illumination, G - Gaussian kernel, I' - the result image
  4. Сonvert back YUV to RGB

This code is not working correctly

 public static Image<Bgr, byte> SingleScaleRetinex(this Image<Bgr, byte> img, int gaussianKernelSize, double sigma)
            {
                var radius = gaussianKernelSize / 2;
                var kernelSize = 2 * radius + 1;

                var ycc = img.Convert<Ycc, byte>();

                var sum = 0f;
                var gaussKernel = new float[kernelSize * kernelSize];
                for (int i = -radius, k = 0; i <= radius; i++, k++)
                {
                    for (int j = -radius; j <= radius; j++)
                    {
                        var val = (float)Math.Exp(-(i * i + j * j) / (sigma * sigma));
                        gaussKernel[k] = val;
                        sum += val;
                    }
                }
                for (int i = 0; i < gaussKernel.Length; i++)
                    gaussKernel[i] /= sum;

                var gray = new Image<Gray, byte>(ycc.Size);
                CvInvoke.cvSetImageCOI(ycc, 1);
                CvInvoke.cvCopy(ycc, gray, IntPtr.Zero);

                // Размеры изображения
                var width = img.Width;
                var height = img.Height;

                var bmp = gray.Bitmap;
                var bitmapData = bmp.LockBits(new Rectangle(Point.Empty, gray.Size), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

                unsafe
                {
                    for (var y = 0; y < height; y++)
                    {
                        var row = (byte*)bitmapData.Scan0 + y * bitmapData.Stride;
                        for (var x = 0; x < width; x++)
                        {
                            var color = row + x;

                            float val = 0;

                            for (int i = -radius, k = 0; i <= radius; i++, k++)
                            {
                                var ii = y + i;
                                if (ii < 0) ii = 0; if (ii >= height) ii = height - 1;

                                var row2 = (byte*)bitmapData.Scan0 + ii * bitmapData.Stride;
                                for (int j = -radius; j <= radius; j++)
                                {
                                    var jj = x + j;
                                    if (jj < 0) jj = 0; if (jj >= width) jj = width - 1;

                                    val += *(row2 + jj) * gaussKernel[k];

                                }
                            }

                            var newColor = 127.5 + 255 * Math.Log(*color / val);
                            if (newColor > 255)
                                newColor = 255;
                            else if (newColor < 0)
                                newColor = 0;
                            *color = (byte)newColor;
                        }
                    }
                }
                bmp.UnlockBits(bitmapData);

                CvInvoke.cvCopy(gray, ycc, IntPtr.Zero);
                CvInvoke.cvSetImageCOI(ycc, 0);

                return ycc.Convert<Bgr, byte>();

            }

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-04-01 18:00

Sorry for necro-posting, but it seems that there's a mistake in step 3 of your procedure that can mislead someone passing by.

In order to apply the correction, you want to divide source image by Gauss-filtered copy of it, not the Gaussian kernel itself. Approximately, in pseudo-code:

I_filtered(x,y) = G(x,y) * I(x,y)
I'(x,y) = log(I(x,y) / I_filtered(x,y))

And then apply casting of I'(x,y) to required numeric type (uint8, as I can refer from original post).

More on that topic can be found in this paper:

Ri(x, y) = log(Ii(x, y)) − log(Ii(x, y) ∗ F(x, y))

where Ii is the input image on the i-th color channel, Ri is the retinex output image on the i-th channel and F is the normalized surround function. .

查看更多
别忘想泡老子
3楼-- · 2019-04-01 18:05

Look at: http://www.fer.unizg.hr/ipg/resources/color_constancy

These algorithms are modifications of the Retinex algorithm (with speed improvement) although the author gave them funny names :)

There is a full source code (C++, but it is written very nicely).

查看更多
登录 后发表回答