2
我正在爲我的項目製作卷積濾波器,並且我設法使它適用於任何大小的矩陣,但隨着它變得更大我注意到並不是所有的位都改變了。 這裏是顯示問題圖片:C#用於任何大小的矩陣(1x1,3x3,5x5,...)未完全應用的卷積濾波器
正如你所看到的,有一點點條紋這是永遠不會改變,隨着矩陣變大,條紋也變得更大(在3x3中它不可見)
我的卷積矩陣類:
public class ConvMatrix
{
public int Factor = 1;
public int Height, Width;
public int Offset = 0;
public int[,] Arr;
//later I assign functions to set these variables
...
}
過濾功能:
Bitmap Conv3x3(Bitmap b, ConvMatrix m)
{
if (0 == m.Factor)
return b;
Bitmap bSrc = (Bitmap)b.Clone();
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);
BitmapData bmSrc = bSrc.LockBits(new Rectangle(0, 0, bSrc.Width, bSrc.Height),
ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
System.IntPtr SrcScan0 = bmSrc.Scan0;
unsafe
{
byte* p = (byte*)(void*)Scan0;
byte* pSrc = (byte*)(void*)SrcScan0;
int nOffset = stride - b.Width * m.Width;
int nWidth = b.Width - (m.Size-1);
int nHeight = b.Height - (m.Size-2);
int nPixel = 0;
for (int y = 0; y < nHeight; y++)
{
for (int x = 0; x < nWidth; x++)
{
for (int r = 0; r < m.Height; r++)
{
nPixel = 0;
for (int i = 0; i < m.Width; i++)
for (int j = 0; j < m.Height; j++)
{
nPixel += (pSrc[(m.Width * (i + 1)) - 1 - r + stride * j] * m.Arr[j, i]);
}
nPixel /= m.Factor;
nPixel += m.Offset;
if (nPixel < 0) nPixel = 0;
if (nPixel > 255) nPixel = 255;
p[(m.Width * (m.Height/2 + 1)) - 1 - r + stride * (m.Height/2)] = (byte)nPixel;
}
p += m.Width;
pSrc += m.Width;
}
p += nOffset;
pSrc += nOffset;
}
}
b.UnlockBits(bmData);
bSrc.UnlockBits(bmSrc);
return b;
}
請幫
正如你所說,我需要一個更多的循環顏色,這就是爲什麼你看到5個循環那裏;)我沒有意識到錯誤,感謝清除出來。至於解決方案,我不能只是添加一些if語句來使這個工作?我現在沒有任何其他想法。 –
哪個循環完成顏色?額外的「m.Height」?難道不是寧願去紅色,綠色和藍色?是的,如果夾緊的話可以。 'if(x <0)x = 0;'等等。可能會有更高效的方法來避免循環內部的條件,但是在擔心這些之前應該嘗試正確地工作。 –
這是計算顏色值的行:nPixel + =(pSrc [(m.Width *(i + 1)) - 1 - r + stride * j] * m.Arr [j,i]) ;' –