Created
July 5, 2015 02:04
-
-
Save melvincabatuan/0ed661fd01781c874c0a to your computer and use it in GitHub Desktop.
BananaDectect.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "opencv2/objdetect.hpp" | |
#include "opencv2/videoio.hpp" | |
#include "opencv2/highgui.hpp" | |
#include "opencv2/imgproc.hpp" | |
#include <iostream> | |
#include <stdio.h> | |
using namespace std; | |
using namespace cv; | |
/** Function Headers */ | |
void detectAndDisplay(Mat frame); | |
/** Global variables */ | |
String banana_cascade_name = "banana_classifier.xml"; | |
CascadeClassifier banana_cascade; | |
String window_name = "Banana detection"; | |
/** @function main */ | |
int main(int argc, char *argv[]) | |
{ | |
Mat frame; | |
VideoCapture capture(argv[1]); | |
if (!capture.isOpened()) // Check whether the file was opened | |
return -1; | |
int fps = (int)capture.get(CV_CAP_PROP_FPS); | |
namedWindow(window_name); // Creates a window | |
// Load the cascades | |
if (!banana_cascade.load(banana_cascade_name)){ | |
printf("--(!)Error loading face cascade\n"); return -1; | |
}; | |
while (1) | |
{ | |
if (!capture.read(frame)) | |
break; | |
detectAndDisplay(frame); | |
if (waitKey(1000/fps) >= 0) | |
break; | |
} | |
return 0; | |
} | |
/** @function detectAndDisplay */ | |
void detectAndDisplay(Mat frame) | |
{ | |
std::vector<Rect> bananas; | |
Mat frame_gray; | |
//Conversion of frame to grayscale | |
cvtColor(frame, frame_gray, COLOR_BGR2GRAY); | |
//Contrast enhance(Spread out intensity distribution) | |
equalizeHist(frame_gray, frame_gray); | |
//-- Detect Banana | |
//banana_cascade.detectMultiScale(frame_gray, bananas, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30)); | |
banana_cascade.detectMultiScale(frame_gray, bananas, 1.05, 12, 0 |CASCADE_FIND_BIGGEST_OBJECT , Size(100, 100)); | |
cout << "bananas.size() = "<< bananas.size() << endl; | |
for (size_t i = 0; i < bananas.size(); i++) | |
{ | |
Point center(bananas[i].x + bananas[i].width / 2, bananas[i].y + bananas[i].height / 2); | |
ellipse(frame, center, Size(bananas[i].width / 2, bananas[i].height / 2), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0); | |
} | |
//-- Show what you got | |
imshow(window_name, frame); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment