0
이미지 처리 용도로 C#에서 메디안 필터 구현에 문제가 있습니다. 안전하지 않은 잠금 비트 방법으로이 필터를 구현했으며 문제는 내가받는 이미지가 어떻게 든 horrizontal 방식으로 흐려진다는 것입니다 (노이즈는 감소합니다). "(J의 *의 stride0)) + (바이트);"메디안 필터가 제대로 작동하지 않는 이유가 무엇인지 알 수 없음
public static Bitmap ArithmeticMean(Bitmap bitmap, int filterSize)
{
if (bitmap == null)
{
throw new ArgumentNullException("bitmap");
}
Bitmap clonnedBitmap = (Bitmap)bitmap.Clone();
Bitmap secondClonnedBitmap = (Bitmap)bitmap.Clone();
BitmapData srcData = clonnedBitmap.LockBits(new Rectangle(0, 0, clonnedBitmap.Width, clonnedBitmap.Height),
ImageLockMode.ReadWrite, bitmap.PixelFormat);
BitmapData newData = secondClonnedBitmap.LockBits(new Rectangle(0, 0, secondClonnedBitmap.Width, secondClonnedBitmap.Height),
ImageLockMode.ReadWrite, bitmap.PixelFormat);
int bytesPerPixel = Image.GetPixelFormatSize(srcData.PixelFormat)/8;
int stride0 = srcData.Stride;
int stride1 = newData.Stride;
int sideOfLoop = (filterSize-1)/2;
unsafe
{
byte* scan0 = (byte*)srcData.Scan0.ToPointer();
byte* scan1 = (byte*)newData.Scan0.ToPointer();
int width = clonnedBitmap.Width * bytesPerPixel;
int height = clonnedBitmap.Height;
int i, j;
for (int y = sideOfLoop; y < height - sideOfLoop; y++)
{
byte* currentLine = scan0 + y * stride0;
byte* currentLineNewObject = scan1 + y* stride1;
for (int x = sideOfLoop; x < width - sideOfLoop*bytesPerPixel; x += bytesPerPixel)
{
var sumRed = 0;
var sumGreen = 0;
var sumBlue = 0;
for (i = -sideOfLoop; i <= sideOfLoop; i++)
{
for (j = -sideOfLoop; j <= sideOfLoop; j++)
{
int oldBlue = (int) (currentLine[x + i*bytesPerPixel] + (byte) (j* stride0));
int oldGreen = (int)(currentLine[x + 1 + i * bytesPerPixel] + (byte)(j * stride0));
int oldRed = (int)(currentLine[x + 2 + i * bytesPerPixel] + (byte)(j * stride0));
sumBlue += oldBlue;
sumGreen += oldGreen;
sumRed += oldRed;
}
}
int newBlue = sumBlue/(filterSize * filterSize);
int newGreen = sumGreen/(filterSize * filterSize);
int newRed = sumRed/(filterSize * filterSize);
currentLineNewObject[x + 2] = (byte)(newRed);
currentLineNewObject[x + 1] = (byte)(newGreen);
currentLineNewObject[x] = (byte)(newBlue);
}
}
}
clonnedBitmap.UnlockBits(srcData);
secondClonnedBitmap.UnlockBits(newData);
return secondClonnedBitmap;
}
'currentLine'에 대한 주소입니다. 주소는 정확히 같고 결과는 변경되지 않습니다. – Pawel