Hello everyone,
i've trained a Cascade classifier with opencv_traincascade.exe. In the 0-Stage it says HR 1 FA 0. But whren i use it to detect an object it doesn't detect it. I use C++ and OpenCV 3.1. The image size is 100x100px. Even if i use one of the positive images that i used for training it doesnt detect any object. Do you have any ideas what my problem could be?
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
// Function Headers
void detectAndDisplay(Mat frame);
// Global variables
// Copy this file from opencv/data/haarscascades to target folder
string face_cascade_name = "C:\\CreateSamples\\classifier\\cascade.xml";
CascadeClassifier face_cascade;
string window_name = "Capture - Face detection";
int filenumber; // Number of file to be saved
string filename;
// Function main
int main(void)
{
// Load the cascade
if (!face_cascade.load(face_cascade_name)) {
printf("--(!)Error loading\n");
return (-1);
}
// Read the image file
Mat frame = imread("C:\\CreateSamples\\rawdata\\img02.bmp");
// Apply the classifier to the frame
if (!frame.empty()) {
detectAndDisplay(frame);
}
else {
printf(" --(!) No captured frame -- Break!");
//break;
}
int c = waitKey(10000000000);
if (27 == char(c)) {
//break;
}
return 0;
}
// Function detectAndDisplay
void detectAndDisplay(Mat frame)
{
std::vector<Rect> faces;
Mat frame_gray;
Mat crop;
Mat res;
Mat gray;
string text;
stringstream sstm;
cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
// Detect faces
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 1.1, 0 | CASCADE_SCALE_IMAGE, Size(1, 1));
// Set Region of Interest
cv::Rect roi_b;
cv::Rect roi_c;
size_t ic = 0; // ic is index of current element
int ac = 0; // ac is area of current element
size_t ib = 0; // ib is index of biggest element
int ab = 0; // ab is area of biggest element
for (ic = 0; ic < faces.size(); ic++) // Iterate through all current elements (detected faces)
{
roi_c.x = faces[ic].x;
roi_c.y = faces[ic].y;
roi_c.width = (faces[ic].width);
roi_c.height = (faces[ic].height);
ac = roi_c.width * roi_c.height; // Get the area of current element (detected face)
roi_b.x = faces[ib].x;
roi_b.y = faces[ib].y;
roi_b.width = (faces[ib].width);
roi_b.height = (faces[ib].height);
ab = roi_b.width * roi_b.height; // Get the area of biggest element, at beginning it is same as "current" element
if (ac > ab)
{
ib = ic;
roi_b.x = faces[ib].x;
roi_b.y = faces[ib].y;
roi_b.width = (faces[ib].width);
roi_b.height = (faces[ib].height);
}
crop = frame(roi_b);
resize(crop, res, Size(128, 128), 0, 0, INTER_LINEAR); // This will be needed later while saving images
cvtColor(crop, gray, CV_BGR2GRAY); // Convert cropped image to Grayscale
// Form a filename
filename = "";
stringstream ssfn;
ssfn << filenumber << ".png";
filename = ssfn.str();
filenumber++;
filename = "C:\\CreateSamples\\rawdata\\img02detec.bmp";
imwrite(filename, gray);
Point pt1(faces[ic].x, faces[ic].y); // Display detected faces on main window - live stream from camera
Point pt2((faces[ic].x + faces[ic].height), (faces[ic].y + faces[ic].width));
rectangle(frame, pt1, pt2, Scalar(0, 255, 0), 2, 8, 0);
}
// Show image
sstm << "Crop area size: " << roi_b.width << "x" << roi_b.height << " Filename: " << filename;
text = sstm.str();
putText(frame, text, cvPoint(30, 30), FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar(0, 0, 255), 1, CV_AA);
imshow("original", frame);
if (!crop.empty())
{
imshow("detected", crop);
printf("detected");
}
else
destroyWindow("detected");
printf("not detected");
}
------cascade.xml
<?xml version="1.0"?>
<opencv_storage>
<cascade>
<stageType>BOOST</stageType>
<featureType>HAAR</featureType>
<height>100</height>
<width>100</width>
<stageParams>
<boostType>GAB</boostType>
<minHitRate>9.9500000476837158e-01</minHitRate>
<maxFalseAlarm>5.0000000000000000e-01</maxFalseAlarm>
<weightTrimRate>9.4999999999999996e-01</weightTrimRate>
<maxDepth>1</maxDepth>
<maxWeakCount>100</maxWeakCount></stageParams>
<featureParams>
<maxCatCount>0</maxCatCount>
<featSize>1</featSize>
<mode>BASIC</mode></featureParams>
<stageNum>1</stageNum>
<stages>
<!-- stage 0 -->
<_>
<maxWeakCount>1</maxWeakCount>
<stageThreshold>1.</stageThreshold>
<weakClassifiers>
<_>
<internalNodes>
0 -1 0 8.4346056610229425e-06</internalNodes>
<leafValues>
1. -1.</leafValues></_></weakClassifiers></_></stages>
<features>
<_>
<rects>
<_>
0 0 1 2 -1.</_>
<_>
0 1 1 1 2.</_></rects>
<tilted>0</tilted></_></features></cascade>
</opencv_storage>