我在 matlab 中做了一个人脸检测器,我正在将它翻译成 c# 代码,我已经完成了大部分工作。我主要使用
System.Drawing.Bitmap b = new
System.Drawing.Bitmap("C:*Location of file on computer*");
开始获得图像,在最后的步骤中我有这个代码
public static void ratio(System.Drawing.Bitmap b, Dictionary<int, List<int>> map)
{
double height=0;
double width=0;
foreach (KeyValuePair<int, List<int>> place in map)
{
height = place.Value[2] - place.Value[3];
width = place.Value[0] - place.Value[1];
if( ((height/width) >= 1) && ((height/width) <= 2 ) )
draw(b, place, map);
}
}
public static void draw(System.Drawing.Bitmap bmp, KeyValuePair<int, List<int>> place, Dictionary<int, List<int>> map)
{
// Create pen.
Pen blackPen = new Pen(Color.Black, 3);
// Create coordinates of points that define line.
int x1 = place.Value[1]; //topleft to topright
int y1 = place.Value[3];
int x2 = place.Value[0];
int y2 = place.Value[3];
int x3 = place.Value[0]; //topright to bottomright
int y3 = place.Value[3];
int x4 = place.Value[0];
int y4 = place.Value[2];
int x5 = place.Value[0]; //bottomright to bottomleft
int y5 = place.Value[2];
int x6 = place.Value[1];
int y6 = place.Value[2];
int x7 = place.Value[1]; //bottomleft to topleft
int y7 = place.Value[2];
int x8 = place.Value[1];
int y8 = place.Value[3];
// Draw line to screen.
using (var graphics = Graphics.FromImage(bmp))
{
graphics.DrawLine(blackPen, x1, y1, x2, y2);
}
using (var graphics = Graphics.FromImage(bmp))
{
graphics.DrawLine(blackPen, x3, y3, x4, y4);
}
using (var graphics = Graphics.FromImage(bmp))
{
graphics.DrawLine(blackPen, x5, y5, x6, y6);
}
using (var graphics = Graphics.FromImage(bmp))
{
graphics.DrawLine(blackPen, x7, y7, x8, y8);
}
}
在脸上画一个方框。 Ratio 使用从连接组件标签中获得的标签的边界来找到人脸的正确比例(我的数字刚刚组成) map 是包含标签编号的字典,以及作为值的 xmax、xmin、ymax 和 ymin .一切都没有错误地编译但是,我现在想做的是用脸部周围的绘制框显示所述图像,我不确定该怎么做
假设这是一个 Windows.Forms 应用程序,您可以从工具箱中将 PictureBox 控件拖放到窗体上,将其 Dock 属性设置为 Fill,并在代码中设置其 Image 属性:
PictureBox1.Image = b;
在窗体设计器中,在窗体上放置一个
PictureBox
控件,并根据需要调整它的位置和大小。如果您愿意(或者如果有必要),您可以通过编程方式添加一个。
然后,为表单的
Load
事件添加事件处理程序,并在该方法中应用此代码:
System.Drawing.Bitmap b = new System.Drawing.Bitmap("C:*Location of file on computer*");
pictureBox1.Image = b;
那么,你的绘制方法就可以变成:
public static void draw(KeyValuePair<int, List<int>> place, Dictionary<int, List<int>> map)
{
// Create pen.
Pen blackPen = new Pen(Color.Black, 3);
// Create coordinates of points that define line.
int x1 = place.Value[1]; //topleft to topright
int y1 = place.Value[3];
int x2 = place.Value[0];
int y2 = place.Value[3];
int x3 = place.Value[0]; //topright to bottomright
int y3 = place.Value[3];
int x4 = place.Value[0];
int y4 = place.Value[2];
int x5 = place.Value[0]; //bottomright to bottomleft
int y5 = place.Value[2];
int x6 = place.Value[1];
int y6 = place.Value[2];
int x7 = place.Value[1]; //bottomleft to topleft
int y7 = place.Value[2];
int x8 = place.Value[1];
int y8 = place.Value[3];
// Draw line to screen.
using (Graphics g = Graphics.FromHwnd(pictureBox1.Handle))
{
g.DrawLine(blackPen, x1, y1, x2, y2);
g.DrawLine(blackPen, x3, y3, x4, y4);
g.DrawLine(blackPen, x5, y5, x6, y6);
g.DrawLine(blackPen, x7, y7, x8, y8);
}
pictureBox1.Invalidate();
}