0
我有點困惑如何計算Sobel邊緣檢測方法dx和dy .... 我GOOGLE了它,我發現.......如何在5X5矩陣的sobel邊緣檢測中計算dx和dy?
public class SobelEdgeDetector : ImageFilter<SobelEdgeDetectorParms>
{
private Color grayscale(Color cr)
{
return Color.FromArgb(cr.A, (int)(cr.R * .3 + cr.G * .59 + cr.B * 0.11),
(int)(cr.R * .3 + cr.G * .59 + cr.B * 0.11),
(int)(cr.R * .3 + cr.G * .59 + cr.B * 0.11));
}
public override Bitmap FilterProcessImage(SobelEdgeDetectorParms parms, Bitmap image)
{
Bitmap ret = new Bitmap(image.Width, image.Height);
for (int i = 1; i < image.Width - 1; i++)
{
for (int j = 1; j < image.Height - 1; j++)
{
Color cr = image.GetPixel(i + 1, j);
Color cl = image.GetPixel(i - 1, j);
Color cu = image.GetPixel(i, j - 1);
Color cd = image.GetPixel(i, j + 1);
Color cld = image.GetPixel(i - 1, j + 1);
Color clu = image.GetPixel(i - 1, j - 1);
Color crd = image.GetPixel(i + 1, j + 1);
Color cru = image.GetPixel(i + 1, j - 1);
int dx = 0, dy = 0;
switch (parms.Channel)
{
case Channels.R:
dx = cld.R + 2 * cd.R + crd.R - (clu.R + 2 * cu.R + cru.R);
dy = crd.R + 2 * cr.R + cru.R - (cld.R + 2 * cl.R + clu.R);
break;
case Channels.G:
dx = cld.G + 2 * cd.G + crd.G - (clu.G + 2 * cu.G + cru.G);
dy = crd.G + 2 * cr.G + cru.G - (cld.G + 2 * cl.G + clu.G);
break;
case Channels.B:
dx = cld.B + 2 * cd.B + crd.B - (clu.B + 2 * cu.B + cru.B);
dy = crd.B + 2 * cr.B + cru.B - (cld.B + 2 * cl.B + clu.B);
break;
case Channels.RGB:
dx = grayscale(cld).B + 2 * grayscale(cd).B + grayscale(crd).B - (grayscale(clu).B + 2 * grayscale(cu).B + grayscale(cru).B);
dy = grayscale(crd).B + 2 * grayscale(cr).B + grayscale(cru).B - (grayscale(cld).B + 2 * grayscale(cl).B + grayscale(clu).B);
break;
}
double power = Math.Abs(dx) + Math.Abs(dy);
if (power > parms.Threshold)
ret.SetPixel(i, j, parms.EdgeColor);
else
{
if (parms.CopyOriginal)
{
Color c = image.GetPixel(i, j);
if (parms.ConvertToGrayscale)
{
ret.SetPixel(i, j,
Color.FromArgb(255,
(int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11),
(int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11),
(int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11)));
}
else
ret.SetPixel(i, j, c);
}
else
ret.SetPixel(i, j, Color.White);
}
}
}
return ret;
}
public override System.Windows.Forms.Control GetParameterWindow()
{
return new SobelEdgeDetectorParmForm();
}
}
這是3x3矩陣.. .......我想實現5X5矩陣.......... 其實我想要選擇點的切線斜率。 。 。 。 tangent = Atan2(dx,dy)。 使用3X3矩陣我得到正確的切線,但它不是更準確的切線。 我對計算5X5矩陣的dx和dy感興趣。我不想檢測邊緣,我想檢測是Atan2(dx,dy)的切線........ 請幫助我....
我知道矩陣..... 假設double theta = atan2(y,x);那麼切線的角度將等於theta或90-theta? @MBo – govinda
Atan2給我們角度的梯度向量。如果你的意思是與等高線(等值線)相切 - 是的,它與梯度垂直,90-theta – MBo
謝謝.....- @MBo – govinda