gpt4 book ai didi

image-processing - 高斯模糊问题

转载 作者:行者123 更新时间:2023-12-04 07:23:05 26 4
gpt4 key购买 nike

我正在写一个高斯滤镜,我的目标是尽可能地匹配 Photoshop 中的高斯模糊滤镜。这是我的第一次图像处理尝试。我遇到的一些问题/问题是......

用我的滤镜进一步模糊图像会使其变暗,而 Photoshop 似乎会使其变亮。

我使用的偏差值(“sigma”,在我的代码中)是 r/3,这导致矩阵内的高斯曲线接近 0.0001……有没有更好的方法来确定这个值?

Photoshop(或大多数人)如何处理这种模糊的图像边框?

int matrixDimension = (radius*2)+1;
float sigma = radius/3;
float twoSigmaSquared = 2*pow(sigma, 2);
float oneOverSquareRootOfTwoPiSigmaSquared = 1/(sqrt(M_PI*twoSigmaSquared));

float kernel[matrixDimension];

int index = 0;
for (int offset = -radius; offset <= radius; offset++) {

float xSquared = pow(offset, 2);
float exponent = -(xSquared/twoSigmaSquared);
float eToThePower = pow(M_E, exponent);
float multFactor = oneOverSquareRootOfTwoPiSigmaSquared*eToThePower;

kernel[index] = multFactor;

index++;
}

//Normalize the kernel such that all its values will add to 1
float sum = 0;
for (int i = 0; i < matrixDimension; i++) {
sum += kernel[i];
}
for (int i = 0; i < matrixDimension; i++) {
kernel[i] = kernel[i]/sum;
}

//Blur horizontally
for (int row = 0; row < imageHeight; row++) {
for (int column = 0; column < imageWidth; column++) {

int currentPixel = (row*imageWidth)+column;

int sum1 = 0;
int sum2 = 0;
int sum3 = 0;
int sum4 = 0;

int index = 0;
for (int offset = -radius; offset <= radius; offset++) {
if (!(column+offset < 0) && !(column+offset > imageWidth-1)) {

int firstByteOfPixelWereLookingAtInSrcData = (currentPixel+offset)*4;

int in1 = srcData[firstByteOfPixelWereLookingAtInSrcData];
int in2 = srcData[firstByteOfPixelWereLookingAtInSrcData+1];
int in3 = srcData[firstByteOfPixelWereLookingAtInSrcData+2];
int in4 = srcData[firstByteOfPixelWereLookingAtInSrcData+3];

sum1 += (int)(in1 * kernel[index]);
sum2 += (int)(in2 * kernel[index]);
sum3 += (int)(in3 * kernel[index]);
sum4 += (int)(in4 * kernel[index]);
}

index++;
}

int currentPixelInData = currentPixel*4;

destData[currentPixelInData] = sum1;
destData[currentPixelInData+1] = sum2;
destData[currentPixelInData+2] = sum3;
destData[currentPixelInData+3] = sum4;

}
}

//Blur vertically
for (int row = 0; row < imageHeight; row++) {
for (int column = 0; column < imageWidth; column++) {

int currentPixel = (row*imageWidth)+column;

int sum1 = 0;
int sum2 = 0;
int sum3 = 0;
int sum4 = 0;

int index = 0;
for (int offset = -radius; offset <= radius; offset++) {
if (!(row+offset < 0) && !(row+offset > imageHeight-1)) {

int firstByteOfPixelWereLookingAtInSrcData = (currentPixel+(offset*imageWidth))*4;

int in1 = destData[firstByteOfPixelWereLookingAtInSrcData];
int in2 = destData[firstByteOfPixelWereLookingAtInSrcData+1];
int in3 = destData[firstByteOfPixelWereLookingAtInSrcData+2];
int in4 = destData[firstByteOfPixelWereLookingAtInSrcData+3];

sum1 += (int)(in1 * kernel[index]);
sum2 += (int)(in2 * kernel[index]);
sum3 += (int)(in3 * kernel[index]);
sum4 += (int)(in4 * kernel[index]);
}

index++;
}

int currentPixelInData = currentPixel*4;

finalData[currentPixelInData] = sum1;
finalData[currentPixelInData+1] = sum2;
finalData[currentPixelInData+2] = sum3;
finalData[currentPixelInData+3] = sum4;

}
}

最佳答案

要对滤波器进行逆向工程,您需要找到它的脉冲响应。在一个非常暗的值的背景上,比如 32,放置一个近乎白色的像素,比如 223。你不想使用 0 和 255,因为一些过滤器会尝试创建超出起始值的值。对此图像运行过滤器,并获取输出值并将它们从 0.0 拉伸(stretch)到 1.0:(value-32)/(223-32) .现在您有了模拟过滤器所需的确切权重。

有很多方法可以处理图像边缘。我建议采用过滤器权重并将它们相加,然后将结果除以该总和;如果您试图超越边缘,请对像素值和该像素上的过滤器权重使用 0.0。

关于image-processing - 高斯模糊问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5201073/

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