我需要计算以下裁剪图像的角度。 Image
所以我为计算角度开发了C#(emguCV& openCV)代码。
就在这里,
选择该行的像素
public ArrayList getLinepixels()
{
//bi.RotateFlip(RotateFlipType.Rotate90FlipXY);
testPicBox.Image = bi;
Color pixelColor;
ArrayList list = new ArrayList();
for (int y=0; y <bi.Height; y++)
{
int count=0;
for (int x = 0; x < bi.Width; x++)
{
pixelColor = bi.GetPixel(x, y);
if (pixelColor.R == 255 && pixelColor.G == 255 && pixelColor.B == 255)
{
count++;
if (count == 1)
{
Point p1 = new Point(x, y);
list.Add(p1);
drawPoint(x, y);
break;
}
}
}
int a = list.Count;
// label2.Text = a.ToString();
}
选择合适的像素坐标
public ArrayList minimizePixels(ArrayList minimizeArL)
{
mlist = new ArrayList();
int minimizeArlCount = minimizeArL.Count;
for (int i = 2; i < minimizeArlCount; i++)
{
if (i == 2)
{
mlist.Add(minimizeArL[i]);
}
else if (i == minimizeArlCount - 2)
{
mlist.Add(minimizeArL[i]);
}
else if (i == (minimizeArlCount / 2) / 2)
{
mlist.Add(minimizeArL[i]);
}
else if (i == (minimizeArlCount / 2) + ((minimizeArlCount / 2) / 2))
{
mlist.Add(minimizeArL[i]);
}
}
用于计算角度的代码
public void calculateAngle(ArrayList angleList)
{
minimizePixels(getLinepixels());
int firstRowX = ((Point)mlist[0]).X;
int firstRowY = ((Point)mlist[0]).Y;
int secondRowX = ((Point)mlist[1]).X;
int secondRowY = ((Point)mlist[1]).Y;
int thirdRowX = ((Point)mlist[2]).X;
int thirdRowY = ((Point)mlist[2]).Y;
int fourthRowX = ((Point)mlist[3]).X;
int fourthRowY = ((Point)mlist[3]).Y;
double tanTheta;
double tanAlfa;
double ang;
tanTheta = Math.Atan2((Math.Abs(secondRowY - firstRowY)), (Math.Abs(secondRowX - firstRowX)));
tanAlfa = Math.Atan2((Math.Abs(fourthRowY - thirdRowY)), (Math.Abs(fourthRowX - thirdRowX)));
ang = (tanTheta + tanAlfa) * (180 / Math.PI);
metroLabel1_angle.Text = Convert.ToString(ang);
}
实施上述代码后,程序会显示角度值的某些值,但每次运行时,它会给出不同的答案,有时会给出不正确的答案。请帮我找到解决这个问题的方法。