c_cpp Raspberry Pi 2相机ofxCv color_tracking_edit

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp Raspberry Pi 2相机ofxCv color_tracking_edit相关的知识,希望对你有一定的参考价值。

#include "testApp.h"

using namespace ofxCv;
using namespace cv;

void testApp::setup() {
    cam.setup(640, 480,true);
    contourFinder.setMinAreaRadius(10);
	contourFinder.setMaxAreaRadius(150);
	//contourFinder.setInvert(true); // find black instead of white
	trackingColorMode = TRACK_COLOR_RGB;
    
}

void testApp::update() {
    frame = cam.grab();
    if(!frame.empty()) {
		threshold = ofMap(mouseX, 0, ofGetWidth(), 0, 255);
		contourFinder.setThreshold(threshold);
        contourFinder.findContours(frame);
	}
}

void testApp::draw() {
	ofSetColor(255);
    if(!frame.empty()){
        drawMat(frame,0,0);

        ofSetLineWidth(2);
        contourFinder.draw();

        ofNoFill();
        int n = contourFinder.size();
        for(int i = 0; i < n; i++) {
            // smallest rectangle that fits the contour
            ofSetColor(cyanPrint);
            ofPolyline minAreRect = toOf(contourFinder.getMinAreaRect(i));
            // minAreRect.draw();

            // ellipse that best fits the contour
            ofSetColor(magentaPrint);
            cv::RotatedRect ellipse = contourFinder.getFitEllipse(i);
            ofPushMatrix();
            ofVec2f ellipseCenter = toOf(ellipse.center);
            ofVec2f ellipseSize = toOf(ellipse.size);
            ofTranslate(ellipseCenter.x, ellipseCenter.y);
            ofRotate(ellipse.angle);
            // ofEllipse(0, 0, ellipseSize.x, ellipseSize.y);
            ofPopMatrix();

            // minimum area circle that encloses the contour
            ofSetColor(cyanPrint);
            float circleRadius;
            ofVec2f circleCenter = toOf(contourFinder.getMinEnclosingCircle(i, circleRadius));
            // ofCircle(circleCenter, circleRadius);

            // convex hull of the contour
            ofSetColor(yellowPrint);
            ofPolyline convexHull = toOf(contourFinder.getConvexHull(i));
            convexHull.draw();

            // defects of the convex hull
            vector<cv::Vec4i> defects = contourFinder.getConvexityDefects(i);
            for(int j = 0; j < defects.size(); j++) {
                ofLine(defects[j][0], defects[j][1], defects[j][2], defects[j][3]);
            }

            // some different styles of contour centers
            ofVec2f centroid = toOf(contourFinder.getCentroid(i));
            ofVec2f average = toOf(contourFinder.getAverage(i));
            ofVec2f center = toOf(contourFinder.getCenter(i));
            // ofSetColor(cyanPrint);
            // ofCircle(centroid, 1);
            // ofSetColor(magentaPrint);
            // ofCircle(average, 1);
            // ofSetColor(yellowPrint);
            // ofCircle(center, 1);

            // you can also get the area and perimeter using ofPolyline:
            // ofPolyline::getArea() and ofPolyline::getPerimeter()
            double area = contourFinder.getContourArea(i);
            double length = contourFinder.getArcLength(i);

            // balance is useful for detecting when a shape has an "arm" sticking out
            // if balance.length() is small, the shape is more symmetric: like I, O, X...
            // if balance.length() is large, the shape is less symmetric: like L, P, F...
            ofVec2f balance = toOf(contourFinder.getBalance(i));
            // ofPushMatrix();
            // ofTranslate(centroid.x, centroid.y);
            // ofScale(5, 5);
            // ofLine(0, 0, balance.x, balance.y);
            // ofPopMatrix();
        }

        ofSetColor(255);
        ofDrawBitmapString(ofToString((int) ofGetFrameRate()) + " fps", 10, 10);
        drawHighlightString(ofToString((int) threshold) + " threshold", 10, 30);
        drawHighlightString(trackingColorMode == TRACK_COLOR_RGB ? "RGB tracking" : "hue tracking", 10, 50);


        
        // Mouse Cursor !!!
        ofTranslate(mouseX, mouseY);
        ofFill();
        ofSetColor(0);
        ofRect(-3, -3, 64+6, 64+6);
        ofSetColor(targetColor.r, targetColor.g, targetColor.b);
        ofRect(0, 0, 64, 64);
        
        ofSetColor(255,0,0);
        ofDrawLine(-20,0,20,0);
        ofDrawLine(0,-20,0,20);

    }
}

void testApp::mousePressed(int x, int y, int button) {
    if(!frame.empty()){
        Vec3b cvColor = frame.at<Vec3b>(y,x);
        targetColor = ofColor(cvColor[0],cvColor[1],cvColor[2]);
        contourFinder.setTargetColor(targetColor, trackingColorMode);
    }
}

void testApp::keyPressed(int key) {
	if(key == 'h') {
		trackingColorMode = TRACK_COLOR_HS;
	}
	if(key == 'r') {
		trackingColorMode = TRACK_COLOR_RGB;
	}
	contourFinder.setTargetColor(targetColor, trackingColorMode);
}
#include "testApp.h"

int main() {
	ofSetupOpenGL(640, 480, OF_FULLSCREEN);
	ofRunApp(new testApp());
}
#pragma once

#include "ofMain.h"
#include "ofxCv.h"
#include "ofxCvPiCam.h"

class testApp : public ofBaseApp {
public:
	void setup();
	void update();
	void draw();
	void mousePressed(int x, int y, int button);
	void keyPressed(int key);
	
    ofxCvPiCam cam;
    cv::Mat frame;
    ofxCv::ContourFinder contourFinder;
	float threshold;
	ofxCv::TrackingColorMode trackingColorMode;
	ofColor targetColor;
};

以上是关于c_cpp Raspberry Pi 2相机ofxCv color_tracking_edit的主要内容,如果未能解决你的问题,请参考以下文章

Raspberry Pi 3B+:相机 V1.3 不工作

Raspberry Pi NoIR 相机挂起

树莓派 Raspberry Pi 相机模块 Camera Module 3

树莓派 Raspberry Pi 相机模块 Camera Module 3

python 基于Python Bottle的简单应用程序,用于控制Raspberry Pi相机模块

c_cpp 在Raspberry Pi上使用Microchip的TC74 I2C温度传感器的有用代码