2
我試圖爲圖像實現銳化卷積矩陣濾波器。爲此我創建矩陣3x3。也許我做了錯誤的公式嗎?我也試過其他銳化矩陣,但它沒有幫助。顏色值可能大於255或小於0,所以我決定給這個限制(0 255)。這是否正確?卷積矩陣銳化濾波器
static const int filterSmallMatrixSize = 3;
static const int sharpMatrix[3][3] = {{-1, -1, -1},{-1, 9, -1},{-1, -1, -1}};
一些限定
#define Mask8(x) ((x) & 0xFF)
#define R(x) (Mask8(x))
#define G(x) (Mask8(x >> 8))
#define B(x) (Mask8(x >> 16))
#define A(x) (Mask8(x >> 24))
#define RGBAMake(r, g, b, a) (Mask8(r) | Mask8(g) << 8 | Mask8(b) << 16 | Mask8(a) << 24)
和算法
- (UIImage *)processSharpFilterUsingPixels:(UIImage *)inputImage
{
UInt32 *inputPixels;
CGImageRef inputCGImage = [inputImage CGImage];
NSUInteger inputWidth = CGImageGetWidth(inputCGImage);
NSUInteger inputHeight = CGImageGetHeight(inputCGImage);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSUInteger bytesPerPixel = 4;
NSUInteger bitsPerComponent = 8;
NSUInteger inputBytesPerRow = bytesPerPixel * inputWidth;
inputPixels = (UInt32 *)calloc(inputHeight * inputWidth, sizeof(UInt32));
CGContextRef context = CGBitmapContextCreate(inputPixels, inputWidth, inputHeight,
bitsPerComponent, inputBytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextDrawImage(context, CGRectMake(0, 0, inputWidth, inputHeight), inputCGImage);
for (NSUInteger j = 1; j < inputHeight - 1; j++)
{
for (NSUInteger i = 1; i < inputWidth - 1; i++)
{
Float32 newRedColor = 0;
Float32 newGreenColor = 0;
Float32 newBlueColor = 0;
Float32 newA = 0;
for (int filterMatrixI = 0 ; filterMatrixI < filterSmallMatrixSize ; filterMatrixI ++)
{
for (int filterMatrixJ = 0; filterMatrixJ < filterSmallMatrixSize; filterMatrixJ ++)
{
UInt32 * currentPixel = inputPixels + ((j + filterMatrixJ - 1) * inputWidth) + i + filterMatrixI - 1;
int color = *currentPixel;
newRedColor += (R(color) * sharpMatrix[filterMatrixI][filterMatrixJ]);
newGreenColor += (G(color) * sharpMatrix[filterMatrixI][filterMatrixJ]);
newBlueColor += (B(color)* sharpMatrix[filterMatrixI][filterMatrixJ]);
newA += (A(color) * sharpMatrix[filterMatrixI][filterMatrixJ]);
}
}
int r = MAX(MIN((int)newRedColor,255), 0);
int g = MAX(MIN((int)newGreenColor,255), 0);
int b = MAX(MIN((int)newBlueColor,255), 0);
int a = MAX(MIN((int)newA,255), 0);
UInt32 *currentMainImagePixel = inputPixels + (j * inputWidth) + i;
*currentMainImagePixel = RGBAMake(r,g,b,a);
}
}
CGImageRef newCGImage = CGBitmapContextCreateImage(context);
UIImage * processedImage = [UIImage imageWithCGImage:newCGImage];
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
free(inputPixels);
return processedImage;
}
「也許我在配方中做了錯誤?」 - 不確定。你不打算這種效果嗎?我認爲它看起來很有趣。無論如何,不要垃圾語言標籤! – Olaf
不要只將顏色值剪裁到你的邊界,而是將它們縮放。使用給定的矩陣,您可以獲得從-2040到2295範圍內的值。瞭解了這一點以及所有顏色值可以介於0和255之間的事實,您可以使用該公式來縮放值,從而保留信息量: y =(x + 2040)/(2295 + 2040)*(255-0)+ 0',其中'x'是濾波後的值,'y'是您在0到255之間的新顏色值。從[a,b]到[c,d]中的y是'y =(x-a)/(ba)*(dc)+ c)') – muXXmit2X
@Olaf它假設是「銳化」添加這個標籤,因爲它有點c和C++。對不起,關於這個。 –