如何在dlib中保存生成的人脸关键点图像?

6
我正在使用dlib的face_landmark_detection_ex.cpp文件,该文件可以显示检测到的人脸图像以及原始图像上所有面部特征点。我想要将带有所有68个面部特征点的原始图像保存到我的电脑中。我知道可以通过dlib的save_png和draw_rectangle函数来实现,但是draw_rectangle仅提供检测到的面部矩形位置,除此之外,我还想在原始图像上绘制特征点并按如下方式保存它们: image show in window

我没有使用过dlib,但对图像处理有一定的了解。所以我猜,在draw_rectangle函数中,pixel_type参数要求传递一个颜色值,用于绘制矩形。尝试使用结构体rgb_pixel的对象(oValue)来传递参数,oValue(255, 0, 0)表示红色。如果你在调用draw_rectangle函数时适当地传递oValue,就会绘制一个红色的矩形。 - sameerkn
1个回答

3

参数pixel_type用于指定绘制矩形所使用的像素类型。在函数的头文件声明中,默认情况下将使用类型为rgb_pixelrgb_pixel(0,0,0))的黑色像素。

template <typename pixel_type>
void draw_rectangle (
        const canvas& c,
        rectangle rect,
        const pixel_type& pixel = rgb_pixel(0,0,0),
        const rectangle& area = rectangle(-infinity,-infinity,infinity,infinity)
    );

因此,要保存图像,首先使用函数draw_rectangle在图像上绘制矩形,然后使用save_png保存该图像。

针对新问题的编辑:

绘制它们的简单方法是使用函数draw_pixelsp(img,dets [j])返回的每个地标(shape.part(i))绘制在face_landmark_detection_ex.cpp上。
template <typename pixel_type>
    void draw_pixel (
        const canvas& c,
        const point& p,
        const pixel_type& pixel 
    );
    /*!
        requires
            - pixel_traits<pixel_type> is defined
        ensures
            - if (c.contains(p)) then
                - sets the pixel in c that represents the point p to the 
                  given pixel color.
    !*/

当所有地标都被绘制后,使用save_png保存图像。

然而,我建议使用如下方法绘制线条,而不仅仅是地标:enter image description here

要这样做,请使用以下函数:

template <typename image_type, typename pixel_type            >
    void draw_line (
        image_type& img,
        const point& p1,
        const point& p2,
        const pixel_type& val
    );
    /*!
        requires
            - image_type == an image object that implements the interface defined in
              dlib/image_processing/generic_image.h 
        ensures
            - #img.nr() == img.nr() && #img.nc() == img.nc()
              (i.e. the dimensions of the input image are not changed)
            - for all valid r and c that are on the line between point p1 and p2:
                - performs assign_pixel(img[r][c], val)
                  (i.e. it draws the line from p1 to p2 onto the image)
    !*/

你是如何获得眉毛的轮廓的?谢谢。 - Royi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接