# 机器视觉入门之路（二四，旋转矩形截取图像，c#）

//glob_buffer8整幅图
List<PosAndGrey> 线图像40 = new List<PosAndGrey>();
for (int i = 0; i < 40; i++)//40条线,
{
PosAndGrey cc = new PosAndGrey();
cc.tempgrey = new List<float>();
cc.position = new List<PointF>();
Size wwhh = new Size(pictureBox1.Image.Width, pictureBox1.Image.Height);
获取线图像(glob_buffer8, 一众线[i], ref cc, wwhh);
}
//显示截取的线图像ww*hh
int hh = 40;
int ww = 线图像40[0].tempgrey.Count;
byte[] 截取旋转图像 = new byte[ww * hh * 4];
for (int i = 0; i < hh; i++)//h
for (int j = 0; j < ww; j++)//w
{
int temp = (i * ww + j) * 4;
截取旋转图像[temp] = (byte)((int)线图像40[i].tempgrey[j]);
截取旋转图像[temp + 1] = (byte)((int)线图像40[i].tempgrey[j]);
截取旋转图像[temp + 2] = (byte)((int)线图像40[i].tempgrey[j]);
截取旋转图像[temp + 3] = 255;
}

{
public   List<float> tempgrey ;
public List<PointF> position ;
}

{
float x, y;
float dx, dy, k;

PointF Start = mgl.pt_start;
PointF End=mgl.pt_end;
dx = (float)(End.X - Start.X);
dy = (float)(End.Y - Start.Y);
k = dy / dx;
if (Math.Abs(dx) < 0.001)
{
k = 65535;//给定一个最大值
}
x = Start.X;
if (Start.Y < 0) Start.Y = 0;
y = Start.Y;

#region xiaoyu1
if (Math.Abs(k) < 1)
{
if (x > End.X)
{
pAg.position.Clear();
pAg.tempgrey.Clear();
for (; x >= End.X; x--)
{
float j = y;
int i = (int)(x);
float tempf = Math.Abs(j - (int)j);
PointF tempPt = new PointF(i, j);
float grey = 0;
if (k > -1 && k <= 0)
{
float avgGrey = (float)buffer8[(int)j * wh.Width + i];
float avgGrey1 = (float)buffer8[((int)j + 1) * wh.Width + i];
grey = avgGrey * (1 - tempf) + avgGrey1 * tempf;
}
else
{
float avgGrey = (float)buffer8[(int)j * wh.Width + i];
float avgGrey1 = (float)buffer8[((int)j - 1) * wh.Width + i];
grey = avgGrey * (1 - tempf) + avgGrey1 * tempf;
}

y = y - k;
}
}
else
{

pAg.position.Clear();
pAg.tempgrey.Clear();
for (; x <= End.X; x++)
{
float j = y;
int i = (int)(x);
float tempf = Math.Abs(j - (int)j);
PointF tempPt = new PointF(i, j);
float grey = 0;
if (k < 1 && k >= 0)
{
float avgGrey = (float)buffer8[(int)j * wh.Width + i];
float avgGrey1 = (float)buffer8[((int)j + 1) * wh.Width + i];
grey = avgGrey * (1 - tempf) + avgGrey1 * tempf;
}
else
{
float avgGrey = (float)buffer8[(int)j * wh.Width + i];
float avgGrey1 = (float)buffer8[((int)j - 1) * wh.Width + i];
grey = avgGrey * (1 - tempf) + avgGrey1 * tempf;
}

y = y + k;
}

}
}

#endregion xiaoyu1
#region dayu1
if (Math.Abs(k) >= 1)
{

if (y > End.Y)
{
pAg.position.Clear();
pAg.tempgrey.Clear();
for (; y >= End.Y; y--)
{
int j = (int)(y);
float i = x;

float tempf = Math.Abs(i - (int)i);
PointF tempPt = new PointF(i, j);
float grey = 0;

if (k > 1)
{
float avgGrey = (float)buffer8[j * wh.Width + (int)i];
float avgGrey1 = (float)buffer8[j * wh.Width + (int)i - 1];
grey = avgGrey * (1 - tempf) + avgGrey1 * tempf;
}
else
{
float avgGrey = (float)buffer8[j * wh.Width + (int)i];
float avgGrey1 = (float)buffer8[j * wh.Width + (int)i + 1];
grey = avgGrey * (1 - tempf) + avgGrey1 * tempf;
}
x = x - 1 / k;
}
}
else
{
pAg.position.Clear();
pAg.tempgrey.Clear();
for (; y <= End.Y; y++)
{
int j = (int)(y);
float i = x;

PointF tempPt = new PointF(i, j);
float tempf = Math.Abs(i - (int)i);

float grey = 0;
if (k < -1)
{
float avgGrey = (float)buffer8[j * wh.Width + (int)i];
float avgGrey1 = (float)buffer8[j * wh.Width + (int)i - 1];
grey = avgGrey * (1 - tempf) + avgGrey1 * tempf;
}
else
{
float avgGrey = (float)buffer8[j * wh.Width + (int)i];
float avgGrey1 = (float)buffer8[j * wh.Width + (int)i + 1];
grey = avgGrey * (1 - tempf) + avgGrey1 * tempf;
}

x = x + 1 / k;
}
}
}
#endregion dayu1
}

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

机器视觉感知实验室（兼声觉）

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

07-04 794

04-16 2538
01-06 1376