Created
October 2, 2014 14:54
-
-
Save elliotwoods/e5cf91c54625090f92c9 to your computer and use it in GitHub Desktop.
findCirclesGrid
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 "ofApp.h" | |
using namespace ofxCv; | |
using namespace cv; | |
//-------------------------------------------------------------- | |
void ofApp::setup(){ | |
this->image.loadImage("/Users/elliot/Desktop/5x7.jpg"); | |
this->paramThreshold = 100; | |
this->paramMinArea = 10 * 10; | |
this->paramMaxArea = 100 * 100; | |
this->check(); | |
} | |
//-------------------------------------------------------------- | |
void ofApp::update(){ | |
if (this->needsCheck) { | |
this->check(); | |
} | |
} | |
//-------------------------------------------------------------- | |
void ofApp::check(){ | |
Mat cvImage; | |
cvtColor(toCv(this->image.getPixelsRef()), cvImage, CV_RGB2GRAY); | |
int blockSize = sqrt(this->paramMaxArea) * 3.0f; | |
blockSize = (blockSize / 2) * 2 + 1; | |
if (blockSize <= 1) { | |
blockSize = 3; | |
} | |
adaptiveThreshold(cvImage, cvImage, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, blockSize, 2); | |
imitate(this->thresholdedImage, cvImage); | |
memcpy(this->thresholdedImage.getPixels(), cvImage.data, this->thresholdedImage.getPixelsRef().size()); | |
this->thresholdedImage.update(); | |
SimpleBlobDetector::Params params; | |
params.maxArea = paramMaxArea; | |
params.minArea = paramMinArea; | |
Ptr<FeatureDetector> blobDetector = new SimpleBlobDetector(params); | |
auto success = findCirclesGrid(cvImage, cv::Size(5, 7), this->centers, CALIB_CB_ASYMMETRIC_GRID | CALIB_CB_CLUSTERING, blobDetector); | |
cout << (success ? "success" : "fail") << endl; | |
if (!success) { | |
blobDetector->detect(cvImage, this->blobs); | |
} | |
this->needsCheck = false; | |
} | |
//-------------------------------------------------------------- | |
void ofApp::draw(){ | |
ofPushMatrix(); | |
float scale = ofGetWidth() / this->image.getWidth(); | |
ofScale(scale, scale); | |
if (this->showThresholded) { | |
this->thresholdedImage.draw(0, 0); | |
} else { | |
this->image.draw(0, 0); | |
} | |
ofPushStyle(); | |
ofSetColor(255, 100, 100); | |
ofPolyline line; | |
for(auto center : this->centers) { | |
ofCircle(toOf(center), 5); | |
line.addVertex(toOf(center)); | |
} | |
line.draw(); | |
if (!this->centers.empty()) { | |
ofCircle(toOf(this->centers.front()), 10); | |
} | |
ofNoFill(); | |
ofSetLineWidth(2.0f); | |
for(auto blob : this->blobs) { | |
ofCircle(toOf(blob.pt), blob.size); | |
} | |
ofPopStyle(); | |
ofPopMatrix(); | |
ofPushStyle(); | |
ofSetColor(100, 100, 255); | |
ofNoFill(); | |
ofSetLineWidth(1.0f); | |
ofCircle(ofGetMouseX(), ofGetMouseY(), sqrt(this->paramMinArea) / PI * scale); | |
ofCircle(ofGetMouseX(), ofGetMouseY(), sqrt(this->paramMaxArea) / PI * scale); | |
ofPopStyle(); | |
} | |
//-------------------------------------------------------------- | |
void ofApp::keyPressed(int key){ | |
switch(key) { | |
case OF_KEY_UP: | |
this->paramMinArea *= 1.1; | |
break; | |
case OF_KEY_DOWN: | |
this->paramMinArea /= 1.1; | |
break; | |
case OF_KEY_LEFT: | |
this->paramMaxArea /= 1.1; | |
break; | |
case OF_KEY_RIGHT: | |
this->paramMaxArea *= 1.1; | |
break; | |
case '=': | |
this->paramThreshold++; | |
break; | |
case '-': | |
this->paramThreshold--; | |
break; | |
case ' ': | |
this->showThresholded ^= true; | |
break; | |
} | |
cout << "minArea = " << this->paramMinArea << endl; | |
cout << "maxArea = " << this->paramMaxArea << endl; | |
cout << "threshold = " << this->paramThreshold << endl; | |
this->needsCheck = true; | |
} | |
//-------------------------------------------------------------- | |
void ofApp::keyReleased(int key){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::mouseMoved(int x, int y ){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::mouseDragged(int x, int y, int button){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::mousePressed(int x, int y, int button){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::mouseReleased(int x, int y, int button){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::windowResized(int w, int h){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::gotMessage(ofMessage msg){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::dragEvent(ofDragInfo dragInfo){ | |
} |
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
#pragma once | |
#include "ofMain.h" | |
#include "ofxCvMin.h" | |
class ofApp : public ofBaseApp{ | |
public: | |
void setup(); | |
void update(); | |
void check(); | |
void draw(); | |
void keyPressed(int key); | |
void keyReleased(int key); | |
void mouseMoved(int x, int y ); | |
void mouseDragged(int x, int y, int button); | |
void mousePressed(int x, int y, int button); | |
void mouseReleased(int x, int y, int button); | |
void windowResized(int w, int h); | |
void dragEvent(ofDragInfo dragInfo); | |
void gotMessage(ofMessage msg); | |
ofImage image; | |
ofImage thresholdedImage; | |
vector<cv::Point2f> centers; | |
vector<cv::KeyPoint> blobs; | |
float paramMinArea; | |
float paramMaxArea; | |
float paramThreshold; | |
bool needsCheck; | |
bool showThresholded; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment