I am trying to detect faces using the below code making use of GPU
#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <iostream>
#include <stdio.h>
#include <opencv2\ocl\ocl.hpp>
std::string face_cascade = "C:\\opencv\\data\\haarcascades\\haarcascade_frontalface_alt.xml";
std::vector<cv::Rect> detectFaces(cv::Mat gray){
cv::ocl::oclMat oclGray;
std::vector<cv::Rect> faces;
cv::ocl::OclCascadeClassifier face_detector;
oclGray.upload(gray);
face_detector.load(face_cascade);
face_detector.detectMultiScale(oclGray, faces, 1.1, 3, 0|CV_HAAR_SCALE_IMAGE, cv::Size(30, 30), cv::Size(0, 0));
return faces;
}
int main(){
cv::VideoCapture webcam;
cv::Mat mainImage;
std::vector<cv::Rect> faces;
webcam.open(0);
cv::namedWindow("face",CV_WINDOW_AUTOSIZE);
while(webcam.isOpened()){
webcam.read(mainImage);
if(!mainImage.empty()){
cv::resize(mainImage,mainImage,cv::Size(640,480),0,0,1);
cv::Mat gray(mainImage);
cv::cvtColor(gray,gray,CV_BGR2GRAY);
cv::equalizeHist(gray,gray);
faces = detectFaces(gray);
for(unsigned int i=0;i<faces.size();i++){
cv::Rect f_rect = faces[i];
cv::rectangle(mainImage,f_rect,CV_RGB(255,0,0),1,8,0);
}
cv::imshow("face",mainImage);
}
cv::waitKey(40);
}
return 0;
}
I wasnt satisfied with t speed of the normal cascade classifier and thus coded for Ocl based classifier. The program gets started but shows error message:
I have installed APP SDK v 2.9.1
I am using Visual Studio 2012 express edition, Opencv 2.4.10
Where did I go wrong??
Thanks
EDIT>>
cv::ocl::oclMat oclGray;
oclGray.upload(gray);
The above code is causing error..
It looks like it fails on this line face_detector.load(face_cascade); (use debugger to make sure that i'm right). Make sure that the path is correct and that format of cascade file is valid, you may try to use different cascade as well and of course make sure that OCL is installed and configured correctly.
Related
I am using OpenCV 4.4.0 on Ubuntu 20.04 with the installed latest opencv_contrib extra modules. For detecting the face landmarks (based on this tutorial) I use the following #include and namespace sections related to the extra face module :
#include <opencv2/face.hpp>
using namespace cv::face;
The face.hpp file is detected (therefore I assume correct installation of the opencv_contrib modules), but e.g. the line
Ptr<facemark> facemark = FacemarkLBF::create();
throws an error
error: ‘facemark’ was not declared in this scope
I have already tried installing the extra modules with both cmake-gui and with the cmake terminal command. The results are the same. I assume there is an error related to the namespace cv::face. Any ideas on what kind of mistake I am doing here?
The minimal code is here:
#include <opencv2/opencv.hpp>
#include <opencv2/face.hpp>
#include "drawLandmarks.hpp"
using namespace std;
using namespace cv;
using namespace cv::face;
int main(int argc,char** argv)
{
// Load Face Detector
CascadeClassifier faceDetector("haarcascade_frontalface_alt2.xml");
// Create an instance of Facemark
Ptr<facemark> facemark = FacemarkLBF::create();
// Load landmark detector
facemark->loadModel("lbfmodel.yaml");
// Set up webcam for video capture
VideoCapture cam(0);
// Variable to store a video frame and its grayscale
Mat frame, gray;
// Read a frame
while(cam.read(frame))
{
// Find face
vector<rect> faces;
// Convert frame to grayscale because
// faceDetector requires grayscale image.
cvtColor(frame, gray, COLOR_BGR2GRAY);
// Detect faces
faceDetector.detectMultiScale(gray, faces);
// Variable for landmarks.
// Landmarks for one face is a vector of points
// There can be more than one face in the image. Hence, we
// use a vector of vector of points.
vector< vector<point2f> > landmarks;
// Run landmark detector
bool success = facemark->fit(frame,faces,landmarks);
if(success)
{
// If successful, render the landmarks on the face
for(int i = 0; i < landmarks.size(); i++)
{
drawLandmarks(frame, landmarks[i]);
}
}
// Display results
imshow("Facial Landmark Detection", frame);
// Exit loop if ESC is pressed
if (waitKey(1) == 27) break;
}
return 0;
}
As #john and #idclev 463035818 have suggested, the way for creating an instance of Facemark is
Ptr<FacemarkLBF> facemark = FacemarkLBF::create();
e.g. if I want to call it facemark.
I am trying to run a small and simple program using opencv in VS2015. The code is the following:
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv2/nonfree/features2d.hpp>
#include<string>
using namespace cv;
using namespace std;
int main() {
Mat input1=imread("d_1.jpg", 0);
Mat input2 = imread("d_2.jpg", 0);
SiftFeatureDetector detector1,detector2;
vector<KeyPoint> keypoints1,keypoints2;
detector1.detect(input1, keypoints1);
/*detector2.detect(input2, keypoints2);
vector<DMatch> matches1to2;
Mat output1,output2,output;
drawKeypoints(input1, keypoints1, output1);
drawKeypoints(input2, keypoints2, output2);
drawMatches(input1,keypoints1,input2,keypoints2,matches1to2,output);
imwrite("first.jpg", output1);
imwrite("second.jpg", output2);
*/
return 0;
}
I get a Debug Assertion failed Error and the expression mentioned is "reinterpret_cast(_ptr_ptr)[-1]==_BIG_ALLOCATION_SENTINEL"&&0 and the file mentioned is Microsoft visual studio14\vc\include\xmemory0 Line:110.
I looked up other solutions here such as updating the platform to 2010 and checking the configuration to ensure all the included lib files belong to the correct VC version. That just ended up giving me more errors. So I reverted to the original config and ran this code line by line to see which line has the issue cos other wise, the program ran completely, gave output and the error was being reported after the return statement.
The error pops up after the detector.detect statement, I am not sure what the problem is. Any help is appreciated!
when I try debug my project, I get Debug Assertion Failed, at the end of the execution up. You can see it below.
I think that the problem is related to FAST function from OpenCV, because when I comment out the line which contains this function, it works fine.
This is my code:
#pragma once
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int main()
{
string table[2] = { "background_2.jpg","foreground_2.jpg" };
Mat firstImage = imread(table[0], IMREAD_COLOR);
Mat secondImage = imread(table[1], IMREAD_COLOR);
Mat result;
subtract(firstImage, secondImage, result);
Mat resultGray, firstImageGray, secondImageGray;
cvtColor(firstImage, firstImageGray, COLOR_BGR2GRAY);
cvtColor(secondImage, secondImageGray, COLOR_BGR2GRAY);
cvtColor(result, resultGray, COLOR_BGR2GRAY);
Canny(firstImageGray, firstImageGray, 33, 100, 3);
Canny(secondImageGray, secondImageGray, 33, 100, 3);
Canny(resultGray, resultGray, 33, 100, 3);
vector <KeyPoint> keyPoints;
FAST(resultGray, keyPoints, 9, true);//Probably here is the problem.
Mat resultKeyPoints;
drawKeypoints(resultGray, keyPoints, resultKeyPoints, 156);
return 0;
}
I build my project in Visual Studio 2015 Preview.
When I clicked "Retry" to break at that assertion, I got the following exception:
Unhandled exception at 0x00007FF851891B4B (ucrtbased.dll) in
OpenCVProject.exe: An invalid parameter was passed to a function that considers invalid parameters fatal.
This is in xmemory0 file from VS in 120 line.
I found the answer. I changed in Property Pages in my project Platform Toolset from Visual Studio 2015 (v140) to Visual Studio 2013 (v120). Now it works fine. It seems that the problem wasn't in FAST function, but in this case, that I use VS 2015 with OpenCV 3.0. #drescherjm thank you for help me do this.
I'm trying to use this function:
fastNlMeansDenoising(image, image, 3.0, 7, 21);
Using OpenCV with Visual Studio 2010 express, but it said "identifier not found".
I did a quick search and found that this must be a ".lib" is missing, but I did not find which library should I add in my project for this function to work. Anyone could help me with this?
Ok. In order to use fastNlMeansDenoising(image, image, 3.0, 7, 21);
1) You need to configure opencv 2.4.8 or 2.4.9.
Here is procedure to link opencv 249 with Visual studio.
2) Use the following code to test opencv function
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
int main()
{
// load the image
Mat img = imread("lenna.jpg");
if(!img.data)
{
cout << "File not found" << endl;
return -1;
}
// show it in a window
namedWindow( "Image", WINDOW_AUTOSIZE );
imshow("Image", img);
// image window will immediately disappear if the program ends, so
// we'll wait for a keypress, indefinitely
waitKey();
// do a simple transformation: convert to grayscale
// first copy the image
Mat img_gray = img.clone();
Mat img1;
cvtColor(img, img_gray, CV_RGB2GRAY);
fastNlMeansDenoising(img_gray,img1,3.0,7,21);
imshow("Image", img1);
waitKey();
return 0;
}
Hope, this helps you.
Cheers,
The function is defined in the photo.hpp file. So you have to get the opencv_photo300.lib
Edit 1:
I searched a little bit (sorry im at work, dont have more time) and i couldnt find the library itself. You can go ahead and build opencv yourself from: https://github.com/Itseez/opencv
Then you can just search that folder for the lib.
An installationguide for the build process is here: http://docs.opencv.org/trunk/doc/tutorials/introduction/windows_install/windows_install.html
Edit 2:
Berak is right, the opencv_photo300.lib is not in the 2.3 Version of OpenCV. Update your OpenCV to the current version 2.4.9 and you'll have what you need.
you will have to use opencv 2.4.9, it is not available in 2.3.0
I wrote a simple program in OpenCV that detects SURF feature in a given image and diplays the detected features in a namedWindow.
#include <iostream>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\features2d\features2d.hpp>
using namespace cv;
int main(int argc,char** argv)
{
if(argc!=3)//Check cmd number of argumets
{
std::cout<<"Usage: "<<argv[0]<<" <image-file> <method>"<<std::endl;
return -1;
}
//LOAD THE SOURCE IMAGE
Mat Img = imread(argv[1],CV_LOAD_IMAGE_GRAYSCALE);
if(!Img.data)//Check correct image load
{
std::cout<<"Cannot read image file. Check file path!"<<std::endl;
return -1;
}
//COMPUTE FEATURES
SurfFeatureDetector detector;
std::vector<KeyPoint> features;
detector.detect(Img,features);
//SHOW RESULT
Mat ImgF;
drawKeypoints(Img,features,ImgF);
namedWindow("Features", CV_GUI_NORMAL);
imshow("Features",ImgF);
waitKey();
return 0;
}
Everything is OK, the programs do what it have to do. The problem is when pressing a key to terminate the program a crash error occurs.
It doesn't crash for me... but in order for me to compile your code, I had to add
#include <opencv2/nonfree/features2d.hpp>
because SURF was moved to the nonfree module at some point.
So, I would have to recommend trying the newest version (2.4.6 as of today).