C ++ Realsense openCV facedetection - access

时间:2018-05-27 11:06:40

标签: c++ opencv realsense

我是Realsense&的新手。 C ++。我知道这个问题可能很容易。 但是,即使我搜索了半天,也无法解决这个问题。

我正在尝试使用Realsense进行OpenCV人脸检测(Haarcascade)。 但是当我使用'face_cascade,detectMultiScale'时,项目会出现访问冲突错误。 (就像下面的图片一样)

enter image description here

我的代码是:

// License: Apache 2.0.See LICENSE file in root directory.
// Copyright(c) 2017 Intel Corporation. All Rights Reserved.

#include <rs.hpp> // Include RealSense Cross Platform API
#include <opencv2/opencv.hpp>   // Include OpenCV API
#include "opencv2/objdetect.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"

using namespace std;
using namespace cv;

//String face_cascade_name;
CascadeClassifier face_cascade;
string window_name = "Face detection";

void detectAndDisplay(Mat frame)
{
	std::vector<Rect> faces;
	Mat frame_gray;
	
	cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
	equalizeHist(frame_gray, frame_gray);
	if (frame.empty()) {
		printf("error, no data");
	}
	else {
		printf("no problem");
	}
	//face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
	face_cascade.detectMultiScale(frame_gray, faces , 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(500, 500));

	for (size_t i = 0; i < faces.size(); i++)
	{
		Point center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2);
		ellipse(frame, center, Size(faces[i].width / 2, faces[i].height / 2),
			0, 0, 360, Scalar(0, 0, 255), 4, 8, 0);
	}
	
	imshow(window_name, frame);
}


int main(int argc, char * argv[]) try
{
	// Declare depth colorizer for pretty visualization of depth data
	rs2::colorizer color_map;

	// Declare RealSense pipeline, encapsulating the actual device and sensors
	rs2::pipeline pipe;
	
	face_cascade.load("C:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");
	//if (!face_cascade.load(face_cascade_name)) { printf("--(!)Error loading face cascade\n"); };
	

	// Start streaming with default recommended configuration
	pipe.start();

	const auto window_name = "Display Image";
	namedWindow(window_name, WINDOW_AUTOSIZE);

	while (waitKey(1) < 0 && cvGetWindowHandle(window_name))
	{
		rs2::frameset data = pipe.wait_for_frames(); // Wait for next set of frames from the camera
		//rs2::frame depth = color_map(data.get_depth_frame());
		rs2::frame color = data.get_color_frame();

		// Query frame size (width and height)
		//const int w = depth.as<rs2::video_frame>().get_width();
		//const int h = depth.as<rs2::video_frame>().get_height();

		const int color_w = color.as<rs2::video_frame>().get_width();
		const int color_h = color.as<rs2::video_frame>().get_height();

		// Create OpenCV matrix of size (w,h) from the colorized depth data
		//Mat image(Size(w, h), CV_8UC3, (void*)depth.get_data(), Mat::AUTO_STEP);
		Mat image(Size(color_w, color_h), CV_8UC3, (void*)color.get_data(), Mat::AUTO_STEP);

		// Update the window with new data
		//imshow(window_name, image);

		detectAndDisplay(image);
	}

	return EXIT_SUCCESS;
}
catch (const rs2::error & e)
{
	std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n    " << e.what() << std::endl;
	return EXIT_FAILURE;
}
catch (const std::exception& e)
{
	std::cerr << e.what() << std::endl;
	return EXIT_FAILURE;
}

在这里输入代码

0 个答案:

没有答案