gpt4 book ai didi

ios - CIMedianFilter 是如何工作的? (算法)

转载 作者:行者123 更新时间:2023-11-28 22:20:01 25 4
gpt4 key购买 nike

CIMedianFilter 是如何工作的?我的意思是它的算法,我想要它来去除噪音,我试着用这段代码来做:

                // -------------------------- W O R K I N G   O N   R E D -----------------
// red pixels
NSNumber *red1 = [NSNumber numberWithInt:rgbaPixel1[3]];
NSNumber *red2 = [NSNumber numberWithInt:rgbaPixel2[3]];
NSNumber *red3 = [NSNumber numberWithInt:rgbaPixel3[3]];
NSNumber *red4 = [NSNumber numberWithInt:rgbaPixel4[3]];
NSNumber *red5 = [NSNumber numberWithInt:rgbaPixel5[3]];

// red array
NSMutableArray *redArray = [NSMutableArray arrayWithObjects:red1, red2, red3, red4, red5, nil];
// sorting
NSSortDescriptor *lowToHigh = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:YES];
[redArray sortUsingDescriptors:[NSArray arrayWithObject:lowToHigh]];
// getting median
int redMedian = [[redArray objectAtIndex:2] intValue];
// setting the pixels red value to the median
rgbaPixel1[3] = redMedian;
/////////////////////////////////testing if sorting and median is true
// NSLog(@"Sir, here's a test (%@, %@, %@, %@, %@) and the median is %i", [redArray objectAtIndex:0],
// [redArray objectAtIndex:1],
// [redArray objectAtIndex:2],
// [redArray objectAtIndex:3], [redArray objectAtIndex:4], shit);
// ---------------------------- E N D O F R E D ------------------------

// ----------------------------- W O R K I N G O N G R E E N ---------------
// getting green pixels first
NSNumber *green1 = [NSNumber numberWithInteger:rgbaPixel1[2]];
NSNumber *green2 = [NSNumber numberWithInteger:rgbaPixel2[2]];
NSNumber *green3 = [NSNumber numberWithInteger:rgbaPixel3[2]];
NSNumber *green4 = [NSNumber numberWithInteger:rgbaPixel4[2]];
NSNumber *green5 = [NSNumber numberWithInteger:rgbaPixel5[2]];

// creating array of greens
NSMutableArray *greenArray = [NSMutableArray arrayWithObjects:green1, green2, green3, green4, green5, nil];
// sorting the array
[greenArray sortUsingDescriptors:[NSArray arrayWithObject:lowToHigh]];
// getting the median
int greenMedian = [[greenArray objectAtIndex:2] intValue];

// setting the pixels green value to median value
rgbaPixel1[2] = greenMedian;
// ---------------------------- E N D O F G R E E N ------------------------

// -------------------------- W O R K I N G O N B L U E ---------------------
// getting blue pixel
NSNumber *blue1 = [NSNumber numberWithInteger:rgbaPixel1[1]];
NSNumber *blue2 = [NSNumber numberWithInteger:rgbaPixel2[1]];
NSNumber *blue3 = [NSNumber numberWithInteger:rgbaPixel3[1]];
NSNumber *blue4 = [NSNumber numberWithInteger:rgbaPixel4[1]];
NSNumber *blue5 = [NSNumber numberWithInteger:rgbaPixel5[1]];

// creating array for blue values
NSMutableArray *blueArray = [NSMutableArray arrayWithObjects:blue1, blue2, blue3, blue4, blue5, nil];
// sorting the array of blues
[blueArray sortUsingDescriptors:[NSArray arrayWithObject:lowToHigh]];
// getting the median
int blueMedian = [[blueArray objectAtIndex:2] intValue];


// setting pixel blue value to the median we just got :)
rgbaPixel1[1] = blueMedian;

// --------------------------------- E N D O F B L U E ----------------------

但是没有那么大的作用!或者我以错误的方式获取 RGB 值,我真的需要一些帮助。

最佳答案

我不能代表 CIMedianFilter,因为我不知道 Core Image 在那里做什么的细节,但我已经为我的 GPUImage 框架编写了一个中值过滤器,我可以描述那里的过程。

首先,我要说的是,在遍历图像的像素时,您在上面通过使用 NSNumber 和 NSMutableArray 中的大型对象所做的事情在性能方面会很糟糕。此外,所有这些自动释放对象的内存管理也很棘手。为此,您至少需要转向标量类型和 C 数组,以及用于排序的内联函数。更好的是,您可以将其迁移到 GPU。

我在 GPUImage 中的基于 GPU 的实现是基于 "A Fast, Small-Radius GPU Median Filter" Morgan McGuire 和 Kyle Whitson 在 ShaderX6 中的章节。本文介绍了一些可用于加速片段着色器中 GPU 端中值过滤的优化。我在片段着色器中实现的 3x3 中值滤波器如下所示:

 precision highp float;

varying vec2 textureCoordinate;
varying vec2 leftTextureCoordinate;
varying vec2 rightTextureCoordinate;

varying vec2 topTextureCoordinate;
varying vec2 topLeftTextureCoordinate;
varying vec2 topRightTextureCoordinate;

varying vec2 bottomTextureCoordinate;
varying vec2 bottomLeftTextureCoordinate;
varying vec2 bottomRightTextureCoordinate;

uniform sampler2D inputImageTexture;

#define s2(a, b) temp = a; a = min(a, b); b = max(temp, b);
#define mn3(a, b, c) s2(a, b); s2(a, c);
#define mx3(a, b, c) s2(b, c); s2(a, c);

#define mnmx3(a, b, c) mx3(a, b, c); s2(a, b); // 3 exchanges
#define mnmx4(a, b, c, d) s2(a, b); s2(c, d); s2(a, c); s2(b, d); // 4 exchanges
#define mnmx5(a, b, c, d, e) s2(a, b); s2(c, d); mn3(a, c, e); mx3(b, d, e); // 6 exchanges
#define mnmx6(a, b, c, d, e, f) s2(a, d); s2(b, e); s2(c, f); mn3(a, b, c); mx3(d, e, f); // 7 exchanges

void main()
{
vec3 v[6];

v[0] = texture2D(inputImageTexture, bottomLeftTextureCoordinate).rgb;
v[1] = texture2D(inputImageTexture, topRightTextureCoordinate).rgb;
v[2] = texture2D(inputImageTexture, topLeftTextureCoordinate).rgb;
v[3] = texture2D(inputImageTexture, bottomRightTextureCoordinate).rgb;
v[4] = texture2D(inputImageTexture, leftTextureCoordinate).rgb;
v[5] = texture2D(inputImageTexture, rightTextureCoordinate).rgb;
// v[6] = texture2D(inputImageTexture, bottomTextureCoordinate).rgb;
// v[7] = texture2D(inputImageTexture, topTextureCoordinate).rgb;
vec3 temp;

mnmx6(v[0], v[1], v[2], v[3], v[4], v[5]);

v[5] = texture2D(inputImageTexture, bottomTextureCoordinate).rgb;

mnmx5(v[1], v[2], v[3], v[4], v[5]);

v[5] = texture2D(inputImageTexture, topTextureCoordinate).rgb;

mnmx4(v[2], v[3], v[4], v[5]);

v[5] = texture2D(inputImageTexture, textureCoordinate).rgb;

mnmx3(v[3], v[4], v[5]);

gl_FragColor = vec4(v[4], 1.0);
}

这速度足以在 iOS 设备上运行实时视频,但 3x3 半径足够小,您看不到最终图像的显着变化。它提供了少量的空间降噪,但您可能需要扩展到 5x5 区域才能看到更显着的降噪效果。这也将开始稍微模糊图像,因此需要权衡取舍。对于视频,您可以将其与强度较低的低通滤波器结合使用,以进行一些时间降噪。

我将把它作为练习留给您,让您将上述论文应用于大于 3x3 的情况。

关于ios - CIMedianFilter 是如何工作的? (算法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20681762/

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