1
Hello! I recently started to venture into the Opencv library in C++. Today, I want to develop a program that applies a classifier to a polygonal ROI (5 points), obtained through 5 clicks with the mouse on the cam image. Classifier can only stay within this ROI, not outside.
Thanks to several topics here, I was able to gather some examples and assemble the code below that does what I need but in a rectangular area. The "Onmouse" function draws the rectangle. However, I can’t get past this by trying to use other topics that mention polygon ROI. Could someone help with code suggestion =).
My desire is to get a polygonal ROI with mouse instead of rectangle.
Thank you so much for your help! =)
Follows the code.
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/opencv.hpp>
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/features2d/features2d.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/flann/miniflann.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/photo/photo.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core_c.h"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/imgproc/imgproc_c.h"
using namespace std;
using namespace cv;
Mat src,img,ROI;
Rect cropRect(0,0,0,0);
Point P1(0,0);
Point P2(0,0);
const char* winName="Imagem de Origem";
bool clicked=false;
int i=0;
char imgName[15];
const char* WinComClassificador="Imagem COM CLASSIFICADOR";
const char* WinSemClassificador="Imagem SEM CLASSIFICADOR";
void detectAndDisplay( Mat frame);
String face_cascade_name = "C:/Code/Projects_Tests/Cascade_Classifier_not_displaying/Cascade_Classifier/haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "C:/Code/Projects_Tests/Cascade_Classifier_not_displaying/Cascade_Classifier/haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
void detectAndDisplay( Mat frame )
{
std::vector<Rect> faces;
Mat frame_gray;
cvtColor( frame, frame_gray, COLOR_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );
//-- Detect faces
face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30) );
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( 255, 0, 0 ), 4, 8, 0 );
Mat faceROI = frame_gray( faces[i] );
std::vector<Rect> eyes;
//-- In each face, detect eyes
eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CASCADE_SCALE_IMAGE, Size(30, 30) );
for ( size_t j = 0; j < eyes.size(); j++ )
{
Point eye_center( faces[i].x + eyes[j].x + eyes[j].width/2, faces[i].y + eyes[j].y + eyes[j].height/2 );
int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
circle( frame, eye_center, radius, Scalar( 0, 255, 255), 4, 8, 0 );
}
}
//-- Show what you got
//imshow( window_name, frame );
}
void checkBoundary(){
if(cropRect.width>img.cols-cropRect.x)
cropRect.width=img.cols-cropRect.x;
if(cropRect.height>img.rows-cropRect.y)
cropRect.height=img.rows-cropRect.y;
if(cropRect.x<0)
cropRect.x=0;
if(cropRect.y<0)
cropRect.height=0;
}
void showImage(){
img=src.clone();
checkBoundary();
if(cropRect.width>0&&cropRect.height>0){
ROI=src(cropRect);
imshow("Regiao de Interesse",ROI);
}
rectangle(img, cropRect, Scalar(0,255,0), 1, 8, 0 );
imshow(winName,img);
}
void onMouse( int event, int x, int y, int f, void* ){
switch(event){
case CV_EVENT_LBUTTONDOWN :
clicked=true;
P1.x=x;
P1.y=y;
P2.x=x;
P2.y=y;
break;
case CV_EVENT_LBUTTONUP :
P2.x=x;
P2.y=y;
clicked=false;
break;
case CV_EVENT_MOUSEMOVE :
if(clicked){
P2.x=x;
P2.y=y;
}
break;
default : break;
}
if(clicked){
if(P1.x>P2.x){ cropRect.x=P2.x;
cropRect.width=P1.x-P2.x; }
else { cropRect.x=P1.x;
cropRect.width=P2.x-P1.x; }
if(P1.y>P2.y){ cropRect.y=P2.y;
cropRect.height=P1.y-P2.y; }
else { cropRect.y=P1.y;
cropRect.height=P2.y-P1.y; }
}
showImage();
}
int main()
{
string msg("OI FELIPE");
VideoCapture cap(0);
cap.open( 0 );
cap >> src;
if ( ! cap.isOpened() ) { printf("--(!)Error opening video capture\n"); return -4; }
cout << "Recording..." << endl;
namedWindow(winName,WINDOW_NORMAL);
//-- 1. Load the cascades
if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading face cascade\n"); return -1; };
if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error loading eyes cascade\n"); return -1; };
while (cap.read(src)) {
// imshow( "MyVideo", src );
if (!clicked) {
imshow(winName,src);
setMouseCallback(winName,onMouse,NULL );
if( src.empty() )
break;
}
else {
imshow(winName,src);
setMouseCallback(winName,onMouse,NULL );
if( src.empty() )
break;
}
showImage();
int c=waitKey(10);
}
waitKey(10);
return 0;
}
Please avoid long discussions in the comments; your talk was moved to the chat
– Maniero