Detector::Status Detector::detectAndDraw() { const Scalar eye_color({0,255,255}); const Scalar face_color({255,128,0}); vector<Rect> objects; vector<Rect> faces; //check the data if( eyesCascade.empty() || faceCascade.empty() ) { return StatusNoModel; } image = imread(imageSrc.c_str() ); if (!image.data) { return StatusNoImage; } if (!spectacles.data) { return StatusNoSpectacles; } doThreshold(spectacles, specMask, 150, 255, CV_THRESH_BINARY_INV ); if (debug) { namedWindow("Threshold"); imshow("Threshold",specMask); } //convert the image to grayscale Mat gray(image.rows, image.cols, CV_8UC1); cvtColor(image, gray, CV_BGR2GRAY); equalizeHist(gray, gray); // detect faces and eyes faceCascade.detectMultiScale(gray, faces, 1.1, 2|CV_HAAR_SCALE_IMAGE); if (debug) doMarkObjects(faces, face_color); eyesCascade.detectMultiScale( gray, objects, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE ); if (debug) doMarkObjects(objects, eye_color); //sort the results from left-to-right std::sort(faces.begin(), faces.end(), by_x()); std::sort(objects.begin(), objects.end(), by_x()); //iterate over all found faces pred_within_rect pwr; for ( vector<Rect>::iterator face = faces.begin();face<faces.end();face++) { //process the detected face: if there are eyes found within it, then put spectacles on it pwr.r = (*face); vector<Rect>::iterator eye =std::find_if(objects.begin(), objects.end(),pwr); if (eye!=objects.end()) { doPutSpectaclesOnFace(*face, eye); } } imshow(title, image); return StatusOK; }
//! 直方图均衡,为判断车牌颜色做准备 Mat CCharsSegment::histeq(Mat in) { Mat out(in.size(), in.type()); if(in.channels()==3) { Mat hsv; vector<Mat> hsvSplit; cvtColor(in, hsv, CV_BGR2HSV); split(hsv, hsvSplit); equalizeHist(hsvSplit[2], hsvSplit[2]); merge(hsvSplit, hsv); cvtColor(hsv, out, CV_HSV2BGR); } else if(in.channels()==1) { equalizeHist(in, out); } return out; }
void FaceDetector::run(){ CascadeClassifier cascade; if (!cascade.load("res/haarcascade_frontalface_alt.xml") ) { qDebug() << "Erreur lors du chargement du haar cascade frontalface..."; exit(0); } QSize processSize = keepAspectRatio(_image.cols, _image.rows, MAX_PROCESS_WIDTH, MAX_PROCESS_HEIGHT); Mat color; if ( _image.cols > processSize.width() ){ cv::resize( _image, color, cv::Size(processSize.width(), processSize.height() ) ); } else { color = _image.clone(); } QSize displaySize = keepAspectRatio(color.cols, color.rows); _ratio = (double) color.cols/displaySize.width(); Mat gray; cvtColor(color, gray, CV_BGR2GRAY); //PRETRAITEMENT equalizeHist(gray, gray); vector<Rect> facesRects; cascade.detectMultiScale(gray, facesRects, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, cvSize(16,16)); // qDebug() << "detected faces size: " << facesRects.size(); for (unsigned int i = 0; i < facesRects.size(); ++i){ Rect r = facesRects[i]; Mat croppedFace; getRectSubPix(color, Size(r.width, r.height), Point2f(r.x + r.width/2, r.y + r.height / 2), croppedFace); // qImageFaces << Mat2QImage(croppedFace); struct DetectorData tmpData; tmpData.id = i; cv::resize( croppedFace, croppedFace, Size(WORK_SIZE,WORK_SIZE) ); tmpData.image = Mat2QImage(croppedFace); emit sendFace( tmpData.image ); tmpData.rect = QRect(r.x/_ratio+1, r.y/_ratio+1, r.width/_ratio+1, r.height/_ratio+1); tmpData.cvRect = r; tmpData.mat = croppedFace; cvtColor(croppedFace, tmpData.gray, CV_RGB2GRAY); // cv::resize( tmpData.gray, tmpData.gray, Size(WORK_SIZE,WORK_SIZE) ); detectorData << tmpData; } qRegisterMetaType< QList<struct DetectorData> >( "QList<struct DetectorData>" ); emit detectionFinished( _index, detectorData ); }
cv::Mat FaceRecognizer::ImageNormalizer::normalize(const cv::Mat& in) const { // sanity check if( in.type() != CV_8UC1 ) throw Util::Exception( UTIL_LOCSTRM << "face image must be grayscale (CV_8UC1)" ); // normalization cv::medianBlur(in, tmp_[0], 1+2); // remove noise equalizeHist(tmp_[0], tmp_[1]); // equalize histogram cv::resize(tmp_[1], tmp_[0], size_); // resize image to the fixed-size return tmp_[0]; }
/** * @function detectAndDisplay */ int detectAndDisplay( cv::Mat frame ) { std::vector<cv::Rect> faces; //cv::Mat frame_gray; std::vector<cv::Mat> rgbChannels(3); cv::split(frame, rgbChannels); cv::Mat frame_gray = rgbChannels[2]; face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE|CV_HAAR_FIND_BIGGEST_OBJECT, cv::Size(150, 150) ); // findSkin(debugImage); for( int i = 0; i < faces.size(); i++ ) { rectangle(debugImage, faces[i], 1234); } //-- Show what you got if (faces.size() > 0) { findEyes(frame_gray, faces[0]); if (frame_gray.data) { frame_gray.convertTo(frame_gray, CV_32FC1); cv::Mat target = CropFace(frame_gray,leftPupil, rightPupil, offset_pct, dest_sz); if (errorflag) { return -1; } if (target.data) { target.convertTo(target, CV_8UC1); equalizeHist(target,target); target.convertTo(target, CV_32FC1); int index = query(target,ef); index+= startnum; char temp[3]; sprintf(temp, "%d", index); std::string s(temp); std::cout << "face recognized, index : " << index << std::endl; // imshow("target", ef.norm_0_255(target).reshape(1, dest_sz.x)); imshow("result"+s, ef.getdb()[index-startnum]); waitKey(0); destroyWindow("result"+s); return index; } } } return -1; }
cv::Mat DetectRegions::histeq(const cv::Mat& in) { cv::Mat out(in.size(), in.type()); if (in.channels() == 3) { cv::Mat hsv; std::vector<cv::Mat> hsvSplit; cvtColor(in, hsv, CV_BGR2HSV); split(hsv, hsvSplit); equalizeHist(hsvSplit[2], hsvSplit[2]); merge(hsvSplit, hsv); cvtColor(hsv, out, CV_HSV2BGR); } else if (in.channels() == 1) equalizeHist(in, out); return out; }
RawImage* OpenCvHistogramEqualization::ProcessInput(CommandLineArgModel* arg, RawImage* image) { OpenCvHistogramEqualizationModel* model = (OpenCvHistogramEqualizationModel*)arg->ParsedModel; Rectangle roi = model->Roi; Mat img = image->CloneToMat(roi); vector<Mat> channels; split(img, channels); equalizeHist(channels[0], channels[0]); equalizeHist(channels[1], channels[1]); equalizeHist(channels[2], channels[2]); merge(channels, img); image->Import(img, roi.X, roi.Y); return image; }
void MainWindow::on_actionEqualize_triggered() { if(!activeMdiChild()) return; CvGlWidget *actCV = activeMdiChild(); if(actCV->cvImage.channels() == 1){ equalizeHist(actCV->cvImage, actCV->cvImage); actCV->showImage(actCV->cvImage); actCV->buildHistogram(); makeHistogram(); } }
/** @function detectAndDisplay */ void MotionDetection::FaceDetectByCascade(Mat frame, Mat mask, double ScaleFactor) { Mat frameCopy = frame.clone(); if(ScaleFactor != 1) resize(frameCopy, frameCopy, Size(), ScaleFactor, ScaleFactor, INTER_AREA); imshow("test1",frameCopy); //initialize frameCopy_mask // cvtColor(frameCopy_mask, frameCopy_mask, CV_BGR2GRAY); vector<Rect> faces; Mat frame_gray; cvtColor( frameCopy, frame_gray, CV_BGR2GRAY ); //Apply histogram equalization with the function equalizeHist equalizeHist( frame_gray, frame_gray ); face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(28, 28) ); Mat drawing = Mat::zeros(frame_gray.size(),CV_8UC1); for( size_t i = 0; i < faces.size(); i++ ) { //draw the face //Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 ); //ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 ); //slightly adjust the size of the face faces[i] = Rect_AdjustSizeAroundCenter(faces[i],0.8,0.8); // int tlx = faces[i].x, tly = faces[i].y; // for(int j = tlx; j < (tlx + faces[i].width) ; j++) // { // for(int ii = tly; ii < (tly + faces[i].height); ii++) // { // frameCopy_mask.at<uchar>(Point(j,ii)) = 255; // } // } rectangle( drawing, faces[i].tl(), faces[i].br(), Scalar(255,255,255), -1, 8, 0); } imshow("test2",drawing); if(ScaleFactor != 1) resize(drawing, drawing, Size(), 1/ScaleFactor, 1/ScaleFactor, INTER_NEAREST); if(option_str == "-f") { static int counter = 0; String output_str = outPut_Mask_Path + NumberToString(++counter) + ".png"; imwrite(output_str, drawing); } for(int i = 0; i < drawing.cols; i ++) { for(int j = 0; j < drawing.rows; j++) { Point p = Point(i,j); if(drawing.at<uchar>(p) == 255) mask.at<uchar>(p) += mask_add_step; } } }
void equalize_fastnldenoising(vector<Mat>& frame) { float filter_length = 3; int template_window_size = 7; int search_window_size = 21; for (int space = 0; space < frame.size(); space++) { //cvtColor(frame[space], frame[space], CV_BGR2GRAY); equalizeHist(frame[space], frame[space]); //fastNlMeansDenoising(frame[space], frame[space],template_window_size, search_window_size); } }
/** @function detectAndDisplay */ vector<PlateRegion> RegionDetector::doCascade(Mat frame) { //float scale_factor = 1; int w = frame.size().width; int h = frame.size().height; vector<Rect> plates; equalizeHist( frame, frame ); resize(frame, frame, Size(w * this->scale_factor, h * this->scale_factor)); //-- Detect plates timespec startTime; getTime(&startTime); Size minSize(config->minPlateSizeWidthPx * this->scale_factor, config->minPlateSizeHeightPx * this->scale_factor); Size maxSize(w * config->maxPlateWidthPercent * this->scale_factor, h * config->maxPlateHeightPercent * this->scale_factor); if (config->opencl_enabled) { ocl::oclMat openclFrame(frame); ((ocl::OclCascadeClassifier*) plate_cascade)->detectMultiScale(openclFrame, plates, config->detection_iteration_increase, 3, 0, minSize, maxSize); } else { plate_cascade->detectMultiScale( frame, plates, config->detection_iteration_increase, 3, 0, //0|CV_HAAR_SCALE_IMAGE, minSize, maxSize ); } if (config->debugTiming) { timespec endTime; getTime(&endTime); cout << "LBP Time: " << diffclock(startTime, endTime) << "ms." << endl; } for( int i = 0; i < plates.size(); i++ ) { plates[i].x = plates[i].x / scale_factor; plates[i].y = plates[i].y / scale_factor; plates[i].width = plates[i].width / scale_factor; plates[i].height = plates[i].height / scale_factor; } vector<PlateRegion> orderedRegions = aggregateRegions(plates); return orderedRegions; }
void ShapeFeature::compute(Mat& src) { resultingImage = src; Mat channel[3]; // The actual splitting. split(resultingImage, channel); channel[1].setTo(Scalar(0)); Mat br; merge(channel,3,br); channel[0].setTo(Scalar(0)); Mat r; merge(channel,3,r); cvtColor(r, r, CV_BGR2GRAY); medianBlur(r, r, 9 ); equalizeHist(r, r); threshold(r, r, 120, 255, THRESH_BINARY); erode(r, r, Mat()); dilate(r, r, Mat()); vector< vector <Point> > contours; // Vector for storing contour vector< Vec4i > hierarchy; int largest_contour_index=0; int largest_area=0; findContours( r.clone(), contours, hierarchy,RETR_EXTERNAL, CHAIN_APPROX_SIMPLE ); // Find the contours in the image for( int i = 0; i< contours.size(); i++ ){ double a=contourArea( contours[i],false); // Find the area of contour if(a>largest_area){ largest_area=a; largest_contour_index=i; //Store the index of largest contour } } Mat pointsf; Mat(contours[largest_contour_index]).convertTo(pointsf, CV_32F); RotatedRect boxE = fitEllipse(pointsf); if(boxE.size.width<boxE.size.height) value = (float)boxE.size.width/boxE.size.height; else value = (float)boxE.size.height/boxE.size.width; }
void FaceDetector::findFacesInImage(const Mat *img, vector<Rect> *res) { Mat tmp; // chuyển đổi hình ảnh sang màu xám và bình thường hóa biểu đồ: cvtColor(*img, tmp, CV_BGR2GRAY); //Cân bằng lại equalizeHist(tmp, tmp); // làm sạch vector res->clear(); // phát hiện khuôn mặt: _cascade->detectMultiScale(tmp, *res, DET_SCALE_FACTOR, DET_MIN_NEIGHBORS, CV_HAAR_FIND_BIGGEST_OBJECT | CV_HAAR_SCALE_IMAGE, Size(30, 30)); }
//get position of center of eye CvPoint2D32f getEyeCenter(Mat &face_frame, Rect eye){ //crop eye to get rid of some noise int crop_y = 10; eye.y += crop_y; eye.height -= crop_y * 2; //eye.width -= 20; Mat eye_color = face_frame(eye); Mat eye_scelra; //convert to HSV and get saturation channel cvtColor(eye_color, eye_scelra, CV_RGB2HSV); std::vector<Mat> hsvChannels(3); split(eye_scelra, hsvChannels); eye_scelra = hsvChannels[1].mul(hsvChannels[2]) / 30; //invert bitwise_not(eye_scelra, eye_scelra); //blur blur(eye_scelra, eye_scelra, Size(4,4)); //apply histogram equalization equalizeHist(eye_scelra, eye_scelra); //threshold //threshold(eye_scelra, eye_scelra, 10, 255, THRESH_BINARY_INV); //threshold type 3, thesh. to 0 //calc center of mass float sum = 0; float sum_x = 0; float sum_y = 0; for (int y = 0; y < eye_scelra.rows; ++y) { uchar* row = eye_scelra.ptr<uchar>(y); for (int x = 0; x < eye_scelra.cols; ++x) { sum += row[x]; sum_x += row[x]*x; sum_y += row[x]*y; } } CvPoint2D32f max = cvPoint2D32f(sum_x/sum, sum_y/sum); //circle(eye_scelra, max, 3, 0); //imshow("eye", eye_scelra); //adjust for crop max.y += crop_y; return max; }
bool OpenCVItemProcessing::Loop() { char exit = waitKey(20); if (exit == 27) return false; else { if (camera.isOpened()) { //camera.set(CV_CAP_PROP_EXPOSURE, (double)exposure - 11.0); //Grab our image camera.read(videoImage); std::vector<Mat> videoCopy; split(videoImage, videoCopy); cvtColor(videoImage, videoImage, CV_BGR2GRAY); std::vector<Mat> channels; split(videoImage, channels); equalizeHist(channels[0], channels[0]); merge(channels, videoImage); cvtColor(videoImage, videoImage, CV_GRAY2BGR); //Make a pseudo grayscale image //ImageProcess_ColorMask(videoImage); //actually convert to grayscale //cvtColor(videoImage, videoImage, CV_BGR2GRAY); //To Binary //threshold(videoImage, videoImage, 25.0, 255.0, THRESH_BINARY); //Canny? //Canny(videoImage, videoImage, 0.0, 1.0, 3, false); //Show image on screen imshow(windowName, videoImage); } return true; } }
/* * Returns a list of all the vehicles found in a frame. * The base classifier used for this process can be set in CLASSIFIER_PATH. */ vector<Rect> RoadDetection::getVehicles(Mat frame) { Mat copy, equalizedFrame; CascadeClassifier vehiclesCascade; vector<Rect> vehicles; frame.copyTo(copy); cvtColor(copy, equalizedFrame, CV_BGR2GRAY); equalizeHist(equalizedFrame, equalizedFrame); vehiclesCascade.load(CLASSIFIER_PATH); vehiclesCascade.detectMultiScale(equalizedFrame, vehicles, CASCADE_SCALE, CASCADE_MIN_NEIGHBORS, 0 | CASCADE_SCALE_IMAGE, Size(CASCADE_MIN_SIZE, CASCADE_MAX_SIZE)); return vehicles; }
void FaceDetectAnalytic::process(analytic::ConcurrentQueue<analytic::api::Image_t>* pInputQueue, analytic::ConcurrentQueue<analytic::api::Image_t>* pOutputQueue) { /* Analytic process starts from here */ while(pHaarCascade) { /* 1. get a image from input queue */ api::Image_t image = pInputQueue->pop(); cv::Mat matInputImage = image.matImage; // got it /* 2. clone the input image */ cv::Mat matToBeProcessed = matInputImage.clone(); /* 3. release the input image */ matInputImage.release(); //please do this release! /* 4. OK, now use cloned image for analysis */ // Start processing cv::Mat matGray; cv::cvtColor(matToBeProcessed, matGray, CV_BGR2GRAY); equalizeHist(matGray, matGray); vector<Rect_<int> > vFaces; pHaarCascade->detectMultiScale(matGray, vFaces, _dScaleFactor, _iMinNeighbors, 0|CV_HAAR_SCALE_IMAGE, _minSize, _maxSize); for (size_t i = 0; i < vFaces.size(); ++i) { Rect recFace = vFaces[i]; Mat matFace = matGray(recFace); rectangle(matToBeProcessed, recFace, CV_RGB(0, 255, 0), 1); matFace.release(); } if(_bDisplayOutput) { cv::imshow("Output", matToBeProcessed); cv::waitKey(1); } // freeing generated Mats matGray.release(); matToBeProcessed.release(); /* 5. set output */ if(vFaces.size() > 0) { image.bGenerateAnalyticEvent = true; resultXml(vFaces, image.sCustomTextResult); } /* 6. push into output queue */ pOutputQueue->push(image); } }
void Detection::getFaces(Mat& a) { std::vector<Rect> faces; Mat frameGray; Mat frameGray2; cvtColor( a, frameGray, CV_BGR2GRAY ); equalizeHist( frameGray, frameGray2 ); face_cascade.detectMultiScale( frameGray2, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) ); for( int i = 0; i < faces.size(); i++ ) { rectangle(a, faces[i], Scalar(255, 0, 0)); } std::cout << "end loop "<< std::endl; }
/** @function detectAndDisplay */ vector<Rect> RegionDetector::doCascade(Mat frame) { //float scale_factor = 1; int w = frame.size().width; int h = frame.size().height; vector<Rect> plates; equalizeHist( frame, frame ); resize(frame, frame, Size(w * this->scale_factor, h * this->scale_factor)); //-- Detect plates timespec startTime; getTime(&startTime); Size minSize(config->minPlateSizeWidthPx * this->scale_factor, config->minPlateSizeHeightPx * this->scale_factor); Size maxSize(w * config->maxPlateWidthPercent * this->scale_factor, h * config->maxPlateHeightPercent * this->scale_factor); plate_cascade.detectMultiScale( frame, plates, 1.1, 3, 0, //0|CV_HAAR_SCALE_IMAGE, minSize, maxSize ); if (config->debugTiming) { timespec endTime; getTime(&endTime); cout << "LBP Time: " << diffclock(startTime, endTime) << "ms." << endl; } for( int i = 0; i < plates.size(); i++ ) { plates[i].x = plates[i].x / scale_factor; plates[i].y = plates[i].y / scale_factor; plates[i].width = plates[i].width / scale_factor; plates[i].height = plates[i].height / scale_factor; } return plates; }
void caasCLR4Tx1::FindIsolatorAngle() { //We cut 4/5 width of isolator out int height = 3 * (isolatorBottomEdge - isolatorTopEdge) / 2; int middle = (isolatorBottomEdge + isolatorTopEdge) / 2; Rect rect = Rect(isolatorRightEdge - 4 * isolatorWidth / 5, middle - height / 2, 4 * isolatorWidth / 5, height); Mat imageIsolator = imageGray(rect); int scale = 4; resize(imageIsolator, imageIsolator, Size(imageIsolator.cols / scale, imageIsolator.rows / scale)); #if _DEBUG imwrite("Isolator.jpg", imageIsolator); #endif //sharpen the image //Unsharping masking: Use a Gaussian smoothing filter and subtract the smoothed version from the original image (in a weighted way so the values of a constant area remain constant). Mat imageBlurred, imageGraySharpened; double GAUSSIAN_RADIUS = 4.0; GaussianBlur(imageIsolator, imageBlurred, Size(0, 0), GAUSSIAN_RADIUS); addWeighted(imageIsolator, 1.5, imageBlurred, -0.5, 0, imageGraySharpened); #if _DEBUG //imageGraySharpened = imageGrayQuarter; imwrite("IsolatorSharpened.jpg", imageGraySharpened); #endif imageGraySharpened = imageIsolator; //Histogram Equalization equalizeHist(imageGraySharpened, imageGraySharpened); #if _DEBUG imwrite("IsolatorEqualized.jpg", imageGraySharpened); #endif //Otsu binarization Mat imageOtsu; threshold(imageGraySharpened, imageOtsu, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU); #if _DEBUG imwrite("IsolatorOtsu.jpg", imageOtsu); #endif //Canny Edge Detection int median = Median(imageGraySharpened); Mat imageCanny; Canny(imageGraySharpened, imageCanny, 0.66 * median, 1.33 * median); #if _DEBUG imwrite("IsolatorCanny.jpg", imageCanny); #endif }
void CSkin::face_detect(const cv::Mat &frame) { char *face_cascade_name = "haarcascade_frontalface_alt.xml"; cv::CascadeClassifier face_cascade; if (!face_cascade.load(face_cascade_name)) { return; } if (frame.cols > 0 && frame.rows > 0) { cv::Mat frame_gray; if (frame.channels() == 3) cvtColor(frame, frame_gray, CV_BGR2GRAY); else frame_gray = frame.clone(); equalizeHist(frame_gray, frame_gray); face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, cv::Size(30, 30)); } }
void FaceDetect::detect(cv::Mat& img, fd_cb_t fd_cb) { int i = 0; double t = 0; std::vector<cv::Rect> faces; const static cv::Scalar colors[] = { CV_RGB(0,0,255), CV_RGB(0,128,255), CV_RGB(0,255,255), CV_RGB(0,255,0), CV_RGB(255,128,0), CV_RGB(255,255,0), CV_RGB(255,0,0), CV_RGB(255,0,255)} ; cv::Mat gray, smallImg( cvRound (img.rows/scale_), cvRound(img.cols/scale_), CV_8UC1 ); cv::cvtColor( img, gray, CV_BGR2GRAY ); cv::resize( gray, smallImg, smallImg.size(), 0, 0, cv::INTER_LINEAR ); equalizeHist( smallImg, smallImg ); t = (double)cvGetTickCount(); cascade_.detectMultiScale( smallImg, faces, 1.1, 2, 0 //|CV_HAAR_FIND_BIGGEST_OBJECT //|CV_HAAR_DO_ROUGH_SEARCH |CV_HAAR_SCALE_IMAGE , cv::Size(30, 30) ); fd_cb_t cb = fd_cb ? fd_cb : fd_cb_; for( std::vector<cv::Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ ) { cv::Mat smallImgROI; cv::Point center; cv::Scalar color = colors[i%8]; int radius; center.x = cvRound((r->x + r->width*0.5)*scale_); center.y = cvRound((r->y + r->height*0.5)*scale_); radius = cvRound((r->width + r->height)*0.25*scale_); if(cb) { cb(center.x, center.y, radius); } } }
vector<DetectedObject> CascadeObjectDetector::detectObjects(Mat image) { Mat gray; if (image.channels() > 1) { cvtColor(image, gray, CV_RGB2GRAY); } else { gray = image; } equalizeHist(gray, gray); detector->process(gray); vector<Rect> found; detector->getObjects(found); vector<DetectedObject> objects; for (int i = 0; i < found.size(); i++) { DetectedObject o; o.boundingBox = found[i]; objects.push_back(o); } return objects; }
//method to detect faces in the image frame and return rectangular ROI if found Rect detect(Mat &frame) { int flag=0; Mat f1(240/2,320/2,frame.type ()); cv::flip(frame,frame,1); cv::resize(frame,f1,f1.size(),0,0,INTER_CUBIC); //cv::flip(f1,f1,1); //ROI of face locations std::vector<Rect> faces; Mat frame_gray; //converting to grayscale cvtColor( f1, frame_gray, CV_BGR2GRAY ); //pre processing frame using histogram equalization equalizeHist( frame_gray, frame_gray ); //-- multiscale detection of faces face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) ); float maxarea=0; Rect rect; for( int i = 0; i < faces.size(); i++ ) { Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 ); int scalex=frame.cols/f1.cols; int scaley=frame.rows/f1.rows; Point ncenter; ncenter.x=center.x*scalex; ncenter.y=center.y*scaley; Point corner; corner.x=faces[i].x; corner.y=faces[i].y; center.x=corner.x*scalex; center.y=corner.y*scaley; if(faces[i].width*faces[i].height>maxarea) { maxarea=faces[i].width*faces[i].height; //scale the center and dimension to original image scale rect=Rect(center.x,center.y,faces[i].width*scalex,faces[i].height*scaley); //rect=Rect(faces[i].x,faces[i].y,faces[i].width,faces[i].height); } } return rect; }
void Detection::getFaces(Mat& a) { std::vector<Rect> faces; Mat frameGray; cvtColor( a, frameGray, CV_BGR2GRAY ); equalizeHist( frameGray, frameGray ); //-- Detect faces face_cascade.detectMultiScale( frameGray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) ); std::cout << "begin loop "<< std::endl; for( int i = 0; i < faces.size(); i++ ) { Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 ); rectangle(a, faces[i], Scalar(255, 0, 0)); //ellipse( a, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 ); std::cout << "iteration " + i << std::endl; } std::cout << "end loop "<< std::endl; }
vector<RotatedRect> grayscale_method(Mat& image, Mat& imgMask) { // std::cout << "SAMPLE_DETECTOR::Starting Grayscale METHOD" << std::endl; cvtColor(image, grayscale_image, CV_BGR2GRAY); Mat eq_gray; equalizeHist(grayscale_image,eq_gray); cv::imwrite("Sample-04-Grayscale.png", grayscale_image); cv::imwrite("Sample-Grayscale-equalized.png", eq_gray); threshold(eq_gray, grayscale_filtered_image, min_grayscale_thresh, max_grayscale_thresh, 0); cv::imwrite("Sample-05-Grayscale-Threshold.png", grayscale_filtered_image); // Erode and Dilate obj_tracker.filter(grayscale_filtered_image, grayscale_erode_size, grayscale_dilate_size); cv::imwrite("Sample-06-Grayscale-Filtered.png", grayscale_filtered_image); // std::cout << "SAMPLE_DETECTOR::Completed Grayscale METHOD" << std::endl; return obj_tracker.track(image,grayscale_filtered_image,imgMask); }
Mat equalizeIntensity(const Mat& inputImage) { if(inputImage.channels() >= 3) { Mat ycrcb; cvtColor(inputImage,ycrcb,CV_BGR2YCrCb); vector<Mat> channels; split(ycrcb,channels); equalizeHist(channels[0], channels[0]); Mat result; merge(channels,ycrcb); cvtColor(ycrcb,result,CV_YCrCb2BGR); return result; } return Mat(); }
Mat eql(Mat img, Mat dst) { if(img.data < 0){ cout << "Not loaded" << endl; } Mat image = img.clone(); int histSize = 256; float range[] = { 0, 256 } ; const float* ranges[] = { range }; MatND hist; calcHist( &image, 1, 0, Mat(), hist, 1, &histSize, ranges, true, false ); // Show the calculated histogram in command window //double total; //total = image.rows * image.cols; equalizeHist(image,dst); }
/******************************************************************** Utils::HistogramEqualize Performs Histogram Equalization on the given image Exceptions: None *********************************************************************/ cv::Mat Utils::HistogramEqualize(const cv::Mat& inputImage) { if ( inputImage.channels() >= 3 ) { cv::Mat ycrcb; cvtColor(inputImage,ycrcb,CV_BGR2YCrCb); std::vector<cv::Mat> channels; split(ycrcb,channels); equalizeHist(channels[0], channels[0]); cv::Mat result; merge(channels,ycrcb); cvtColor(ycrcb,result,CV_YCrCb2BGR); return result; } return cv::Mat(); }
//////////////////////////////////////////////////////////// // Helper Function for Cascade Classifier // Note: Cascade classifier is not currently used // by this application. We left the functions associated // with this method for future reference. //////////////////////////////////////////////////////////// void detectAndDisplay(Mat image, string panel_cascade_name) { CascadeClassifier panel_cascade; if (!panel_cascade.load(panel_cascade_name)){ printf("--(!)Error loading\n"); return; }; std::vector<Rect> detectedPanels; Mat frame_gray; cvtColor(image, frame_gray, CV_BGR2GRAY); equalizeHist(frame_gray, frame_gray); //-- Detect faces panel_cascade.detectMultiScale(frame_gray, detectedPanels, 1.1, 20, 0 | CV_HAAR_SCALE_IMAGE, Size(200, 200)); for (size_t i = 0; i < detectedPanels.size(); i++) { Point topLeft(detectedPanels[i].x, detectedPanels[i].y); Point botRight(detectedPanels[i].x + detectedPanels[i].width, detectedPanels[i].y + detectedPanels[i].height); rectangle(image, topLeft, botRight, Scalar(0, 0, 255), 4); } //-- Show what you got imshow("Classifier Result", image); }