我正试图在视频上画出现场检测到的面孔,但我似乎很难让它正常工作。
var haarcascade = new CascadeClassifier("C:/Users/NotMyName/Desktop/haar/haarcascade_frontalface_alt2.xml");
using (Window window = new Window("capture"))
using (Mat image = new Mat())//Image Buffer
{
while (true)
{
Video.Read(image);
var gray = image.CvtColor(ColorConversionCodes.RGB2GRAY);
OpenCvSharp.Rect[] faces = haarcascade.DetectMultiScale(gray, 1.08, 2, HaarDetectionTypes.ScaleImage, new OpenCvSharp.Size(30,30));
foreach (Rect i in faces)
{
Cv2.Rectangle(image, (faces[i].BottomRight.X, faces[i].BottomRight.Y), (faces[i].TopLeft.X, faces[i].TopLeft.Y), 255, 1);
}然而,编译器只是吐出错误。将faces数组直接传递给函数也不起作用。错误消息是(翻译自德语)。
错误CS0029类型"OpenCvSharp.Rect“不能转换为"int"
发布于 2021-05-19 13:00:51
我认为正确的做法应该是:
...
foreach (Rect i in faces)
{
Cv2.Rectangle(image, new Point(i.BottomRight.X, i.BottomRight.Y), new Point(i.TopLeft.X, i.TopLeft.Y), 255, 1);
}
....前瞻引用当前i中的局部变量Rect。
Cv2.Rectangle方法接受OpenCvSharp.Point。
发布于 2021-05-19 13:09:58
您可以直接使用Rect变量:
foreach (Rect r in faces)
Cv2.Rectangle(image, (r.BottomRight.X, r.BottomRight.Y), (r.TopLeft.X, r.TopLeft.Y), 255, 1);或者使用int如下:
foreach (int i = 0; i < faces.Length; i++)
Cv2.Rectangle(image, (faces[i].BottomRight.X, faces[i].BottomRight.Y), (faces[i].TopLeft.X, faces[i].TopLeft.Y), 255, 1);https://stackoverflow.com/questions/67603572
复制相似问题