如何在ubuntu的opencv和dlib上裁剪图像 我想用Ubuntu上的C++在OpenCV和DLIB上做这些。p> 使用dlib检测人脸。(我已经这样做了。) 仅裁剪嘴部周围的图像
这是我的密码。它基于dlib示例代码如何在ubuntu的opencv和dlib上裁剪图像 我想用Ubuntu上的C++在OpenCV和DLIB上做这些。p> 使用dlib检测人脸。(我已经这样做了。) 仅裁剪嘴部周围的图像,c++,opencv,ubuntu,dlib,C++,Opencv,Ubuntu,Dlib,这是我的密码。它基于dlib示例代码 #include <dlib/image_processing/frontal_face_detector.h> #include <dlib/image_processing/render_face_detections.h> #include <dlib/image_processing.h> #include <dlib/gui_widgets.h> #include <dlib/image_io.
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <highgui.h>
using namespace dlib;
using namespace std;
// ----------------------------------------------------------------------------------------
int main(int argc, char** argv)
{
try
{
// This example takes in a shape model file and then a list of images to
// process. We will take these filenames in as command line arguments.
// Dlib comes with example images in the examples/faces folder so give
// those as arguments to this program.
if (argc == 1)
{
cout << "Call this program like this:" << endl;
cout << "./face_landmark_detection_ex shape_predictor_68_face_landmarks.dat faces/*.jpg" << endl;
cout << "\nYou can get the shape_predictor_68_face_landmarks.dat file from:\n";
cout << "http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
return 0;
}
// We need a face detector. We will use this to get bounding boxes for
// each face in an image.
frontal_face_detector detector = get_frontal_face_detector();
// And we also need a shape_predictor. This is the tool that will predict face
// landmark positions given an image and face bounding box. Here we are just
// loading the model from the shape_predictor_68_face_landmarks.dat file you gave
// as a command line argument.
shape_predictor sp;
deserialize(argv[1]) >> sp;
cv::Mat cimg = cv::imread(argv[1]);
image_window win, win_faces;
// Loop over all the images provided on the command line.
for (int i = 2; i < argc; ++i)
{
cout << "processing image " << argv[i] << endl;
array2d<rgb_pixel> img;
load_image(img, argv[i]);
/*
// Make the image larger so we can detect small faces.
pyramid_up(img);
*/
// Now tell the face detector to give us a list of bounding boxes
// around all the faces in the image.
std::vector<rectangle> dets = detector(img);
cout << "Number of faces detected: " << dets.size() << endl;
// Now we will go ask the shape_predictor to tell us the pose of
// each face we detected.
std::vector<full_object_detection> shapes;
for (unsigned long j = 0; j < dets.size(); ++j)
{
full_object_detection shape = sp(img, dets[j]);
cout << "number of parts: "<< shape.num_parts() << endl;
cout << "pixel position of first part: " << shape.part(0) << endl;
cout << "pixel position of second part: " << shape.part(1) << endl;
// You get the idea, you can get all the face part locations if
// you want them. Here we just store them in shapes so we can
// put them on the screen.
shapes.push_back(shape);
}
// Crop the original image to the defined ROI */
cv::Rect roi;
roi.x = 0;
roi.y = 0;
roi.width = 200;
roi.height = 200;
cv::Mat crop = cimg(roi);
cv::imshow("crop", crop);
// Now let's view our face poses on the screen.
/*
win.clear_overlay();
win.set_image(img);
win.add_overlay(render_face_detections(shapes));
// We can also extract copies of each face that are cropped, rotated upright,
// and scaled to a standard size as shown here:
//dlib::array<array2d<rgb_pixel> > face_chips;
//extract_image_chips(img, get_face_chip_details(shapes), face_chips);
//win_faces.set_image(tile_images(face_chips));
*/
cout << "Hit enter to process the next image..." << endl;
cin.get();
}
}
catch (exception& e)
{
cout << "\nexception thrown!" << endl;
cout << e.what() << endl;
}
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间dlib;
使用名称空间std;
// ----------------------------------------------------------------------------------------
int main(int argc,字符**argv)
{
尝试
{
//此示例接收一个形状模型文件,然后接收一个要创建的图像列表
//我们将把这些文件名作为命令行参数。
//Dlib在examples/faces文件夹中附带示例图像,因此请给出
//这些都是这个程序的参数。
如果(argc==1)
{
您的代码中可能有许多错误:
cv::Mat cimg=cv::imread(argv[1]);-argv[1]是检测器文件,而不是图像-您将得到空图像,这就是程序崩溃的原因
您没有遍历图像。请尝试以下操作:
for (int i = 2; i < argc; ++i)
{
cout << "processing image " << argv[i] << endl;
cv::Mat cvimg = cv::imgread(argv[i]);
dlib::cv_image<rgb_pixel> img(cvimg);
...
dlib::rectangle r = dets[j];
cv::Rect roi(r.left(), r.top(), r.width(), r.height());
cv::Mat face = cvimg(roi);
但它将是全脸图像。如果您只想裁剪嘴巴,则应使用shape predictor的输出(未测试-请检查编译是否良好):
这将产生未对齐的口腔图像很明显,您定义的roi(0,0200200)超出了图像的范围。检查cimg的行和列是否大于200。您能给我一条完整的线吗?我做不好。
std::vector<rectangle> dets = detector(img);
dlib::rectangle r = dets[j];
cv::Rect roi(r.left(), r.top(), r.width(), r.height());
cv::Mat face = cvimg(roi);
full_object_detection shape = sp(img, dets[j]);
auto mouth_left = shape.part(45);
auto mouth_right = shape.part(54);
unsigned long mouth_width = (mouth_right - mouth_left).length();
double padding = 0.2;
cv::Rect roi(mouth_left.x() - mouth_width * padding, mouth_left.y() - mouth_width*0.5, mouth_width * (1 + padding * 2), mouth_width);
cv::Mat mouth = cvimg(roi);