gpt4 book ai didi

c# - Retinex算法实现

转载 作者:行者123 更新时间:2023-12-02 04:44:46 28 4
gpt4 key购买 nike

我需要实现 Single Scale retinex and multiscale retinex algorithm在 C# 中,
我搜索了一下,但找不到任何有用的练习项目和代码
据我正确理解,我应该:

  1. 将 RGB 转换为 YUV
  2. 使用高斯模糊滤镜对图像进行模糊处理
  3. 使用 I'(x, y) = 255*log10( I(x, y)/G(x, y) ) + 127.5
    I - 是光照,G - 高斯核,I' - 结果图像
  4. 将 YUV 转换回 RGB

这段代码不能正常工作

 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>();

}

最佳答案

看看: http://www.fer.unizg.hr/ipg/resources/color_constancy

这些算法是对 Retinex 算法的修改(速度有所提高),尽管作者给了它们有趣的名字:)

有完整的源代码(C++,但写得非常好)。

关于c# - Retinex算法实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20006582/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com