机器视觉入门之路(二四,旋转矩形截取图像,c#)
加载一幅图像:

好,现在取线图像40组(注意对比前头:已经调整成40组横向线图像):
//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);
线图像40.Add(cc);
}
//显示截取的线图像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 struct PosAndGrey
{
public List<float> tempgrey ;
public List<PointF> position ;
}
第二,public void 获取线图像(byte[] buffer8, mg_line mgl, ref PosAndGrey pAg, Size wh)
{
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;
}
pAg.tempgrey.Add(grey);
pAg.position.Add(tempPt);
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;
}
pAg.tempgrey.Add(grey);
pAg.position.Add(tempPt);
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;
}
pAg.tempgrey.Add(grey);
pAg.position.Add(tempPt);
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;
}
pAg.tempgrey.Add(grey);
pAg.position.Add(tempPt);
x = x + 1 / k;
}
}
}
#endregion dayu1
}
最后这个函数,是前面博客中出现过,改过来的。
好,今天尝试成功。
为了截取这幅图,矩形框平移了好几次,旋转也不方便,能否用鼠标拖动呢?他们称之为橡皮筋技术,下一节讲....
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)