c++ - OpenCV Face Detection ROI Assertion Failed -


i'm kinda new in opencv , wanted practice simple face detection , image cropping.

specifically, load images folder using cv::glob, detect faces, draw rectangle on detected face , crop detected face region.

everything works fine, face detected, rectangle drawn right @ spot. except last part: cropping. infamous assertion failed error. below code , error i'm having:

void facedetectfolder() {     mat source;      cascadeclassifier face_cascade;     face_cascade.load("c:/opencv-3.2.0/opencv/sources/data/haarcascades/haarcascade_frontalface_alt2.xml");      string path(path on pc);     std::vector<cv::string> fn;     glob(path, fn, true);      (size_t = 0; < fn.size(); i++)     {         source = imread(fn[i]);         if (source.empty()) continue;          std::string imgname = fn[i].substr(45, std::string::npos); //file name         std::vector<rect> faces;         face_cascade.detectmultiscale(source, faces, 1.1, 2, 0 | cv_haar_scale_image, size(30, 30));          (int = 0; < faces.size(); i++)         {              if (faces[i].width > 80 && faces[i].height*0.5 > 80) //threshold, detections false             {                 int x = faces[i].x;                 int y = faces[i].y;                 int h = y + faces[i].height;                 int w = x + faces[i].width;                  rectangle(source, point(x, y), point(w, h), scalar(255, 0, 0), 2, 8, 0); //drawing rectangle on detected face                  imshow(imgname, source);                  rect roi;                 roi.x = x;                 roi.y = y;                 roi.height = h;                 roi.width = w;                        mat detectedface = source(roi);                  imshow("cropped image", detectedface);                  waitkey(0);             }         }     } } 

and error:

opencv error: assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in cv::mat::mat, file c:\build\master_winpack-build-win64-vc14\opencv\modules\core\src\matrix.cpp, line 522

now understand error shows because roi out of bounds. here's troubles me though.

  1. shouldn't error when try draw rectangle in first place? why error on roi not on rectangle i'm drawing?

  2. why roi out of bounds? show image rectangle drawn on , looks fine. why error when roi has same values drawn rectangle?

please excuse me rookie mistakes, start somewhere. thank reading , have nice day!

in roi.height , roi.width, try giving faces[i].height , faces[i].width respectively. indeed you'd think error should come before works drawing rectangle takes argument 2 diagonally opposite vertexes , not width/height in case of rect roi. initialize rect instead point(x, y) , point(w,h) , should work fine.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -