小白再来问c++
本帖最后由 吾爱破jie 于 2019-11-26 20:55 编辑/*ofAPP*/
#include "ofApp.h"
int pointx;
int pointy;
void ofApp::skin(Mat src)
{
src.convertTo(src, CV_8UC3, 255);
Mat yuv, dst;
cvtColor(src, yuv, CV_BGR2YCrCb);
Mat dstTemp1(src.rows, src.cols, CV_8UC1);
Mat dstTemp2(src.rows, src.cols, CV_8UC1);
// 对YUV空间进行量化,得到2值图像,亮的部分为手的形状
inRange(yuv, Scalar(0, 133, 0), Scalar(256, 173, 256), dstTemp1);
inRange(yuv, Scalar(0, 0, 77), Scalar(256, 256, 127), dstTemp2);
bitwise_and(dstTemp1, dstTemp2, mask);
dst.setTo(Scalar::all(0));
bitwise_and(mask, mask0, mask);
src.copyTo(dst, mask);
vector< vector<Point> > contours; // 轮廓
vector< vector<Point> > filterContours; // 筛选后的轮廓
vector< Vec4i > hierarchy; // 轮廓的结构信息
vector< Point > hull; // 凸包络的点集
contours.clear();
hierarchy.clear();
filterContours.clear();
// 得到手的轮廓
findContours(mask, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
// 去除伪轮廓
for (size_t i = 0; i < contours.size(); i++)
{
//approxPolyDP(Mat(contours), Mat(approxContours), arcLength(Mat(contours), true)*0.02, true);
if (fabs(contourArea(Mat(contours))) > 1000 && fabs(arcLength(Mat(contours), true)) < 2000) //判断手进入区域的阈值
{
filterContours.push_back(contours);
}
}
//cout << contours.x << " " << contours.y << endl;
//SetCursorPos(contours.x, contours.y);
pointx = 400;
pointy = 455;
// 画轮廓
drawContours(src, filterContours, -1, Scalar(0, 0, 255), 2); //8, hierarchy);
//drawContours(src, filterContours, 0, Scalar(255), CV_FILLED);
imshow("traclking", src);
}
/*ofApp.h*/
#pragma once
#include "ofMain.h"
#include "demoParticle.h"
using namespace cv;
using namespace std;
class ofApp : public ofBaseApp{
public:
void skin(Mat src);
};
/*demoParticle.cpp*/
#include "demoParticle.h"
#include "ofAppBaseWindow.h"
#include "ofApp.h"
void demoParticle::update(){
//extern int pointx;
//cout << pointx << endl;
//1 - APPLY THE FORCES BASED ON WHICH MODE WE ARE IN
if( mode == PARTICLE_MODE_ATTRACT ){
ofPoint attractPt(pointx, pointy);
frc = attractPt-pos; // we get the attraction force/vector by looking at the mouse pos relative to our pos
frc.normalize(); //by normalizing we disregard how close the particle is to the attraction point
vel *= drag; //apply drag
vel += frc * 0.6; //apply force
}
else if( mode == PARTICLE_MODE_REPEL ){
ofPoint attractPt(pointx, pointy);
frc = attractPt-pos;
//let get the distance and only repel points close to the mouse
float dist = frc.length();
frc.normalize();
vel *= drag;
if( dist < 150 ){
vel += -frc * 0.6; //notice the frc is negative
}else{
//if the particles are not close to us, lets add a little bit of random movement using noise. this is where uniqueVal comes in handy.
frc.x = ofSignedNoise(uniqueVal, pos.y * 0.01, ofGetElapsedTimef()*0.2);
frc.y = ofSignedNoise(uniqueVal, pos.x * 0.01, ofGetElapsedTimef()*0.2);
vel += frc * 0.04;
}
}
else if( mode == PARTICLE_MODE_NOISE ){
//lets simulate falling snow
//the fake wind is meant to add a shift to the particles based on where in x they are
//we add pos.y as an arg so to prevent obvious vertical banding around x values - try removing the pos.y * 0.006 to see the banding
float fakeWindX = ofSignedNoise(pos.x * 0.003, pos.y * 0.006, ofGetElapsedTimef() * 0.6);
frc.x = fakeWindX * 0.25 + ofSignedNoise(uniqueVal, pos.y * 0.04) * 0.6;
frc.y = ofSignedNoise(uniqueVal, pos.x * 0.006, ofGetElapsedTimef()*0.2) * 0.09 + 0.18;
vel *= drag;
vel += frc * 0.4;
//we do this so as to skip the bounds check for the bottom and make the particles go back to the top of the screen
if( pos.y + vel.y > ofGetHeight() ){
pos.y -= ofGetHeight();
}
}
else if( mode == PARTICLE_MODE_NEAREST_POINTS ){
if( attractPoints ){
//1 - find closest attractPoint
ofPoint closestPt;
int closest = -1;
float closestDist = 9999999;
for(unsigned int i = 0; i < attractPoints->size(); i++){
float lenSq = ( attractPoints->at(i)-pos ).lengthSquared();
if( lenSq < closestDist ){
closestDist = lenSq;
closest = i;
}
}
//2 - if we have a closest point - lets calcuate the force towards it
if( closest != -1 ){
closestPt = attractPoints->at(closest);
float dist = sqrt(closestDist);
//in this case we don't normalize as we want to have the force proportional to distance
frc = closestPt - pos;
vel *= drag;
//lets also limit our attraction to a certain distance and don't apply if 'f' key is pressed
if( dist < 300 && dist > 40 && !ofGetKeyPressed('f') ){
vel += frc * 0.003;
}else{
//if the particles are not close to us, lets add a little bit of random movement using noise. this is where uniqueVal comes in handy.
frc.x = ofSignedNoise(uniqueVal, pos.y * 0.01, ofGetElapsedTimef()*0.2);
frc.y = ofSignedNoise(uniqueVal, pos.x * 0.01, ofGetElapsedTimef()*0.2);
vel += frc * 0.4;
}
}
}
}
//2 - UPDATE OUR POSITION
pos += vel;
//3 - (optional) LIMIT THE PARTICLES TO STAY ON SCREEN
//we could also pass in bounds to check - or alternatively do this at the ofApp level
if( pos.x > ofGetWidth() ){
pos.x = ofGetWidth();
vel.x *= -1.0;
}else if( pos.x < 0 ){
pos.x = 0;
vel.x *= -1.0;
}
if( pos.y > ofGetHeight() ){
pos.y = ofGetHeight();
vel.y *= -1.0;
}
else if( pos.y < 0 ){
pos.y = 0;
vel.y *= -1.0;
}
}
/*demoParticle.h*/
#pragma once
#include "ofMain.h"
extern int pointx;
extern int pointy;
enum particleMode{
PARTICLE_MODE_ATTRACT = 0,
PARTICLE_MODE_REPEL,
PARTICLE_MODE_NEAREST_POINTS,
PARTICLE_MODE_NOISE
};
class demoParticle{
public:
demoParticle();
void update();
};
如图一图二
我在ofApp.cpp中定义了并复制了两个全局变量
然后图三我声明了全局变量
图四中我调用这两个变量,结果为零,却不是我赋值的那两个数,怎么回事 你连JAVA, PYTHON这些都没学过吧....
从你发出来的图片内,我没看到你调用ofApp::skin这个函数,你的全局变量都没进行赋值
这是最后一次回复你的问题........
其实单步调试也可以发现问题的所在....
你与明日 发表于 2019-11-26 19:18
你连JAVA, PYTHON这些都没学过吧....
从你发出来的图片内,我没看到你调用ofApp::skin这个函数,你的全 ...
代码太多了大佬,我只把重要关键函数发出来了,几千行一个cpp 实在不行,就自己调试找下问题嘛,先在赋值那个地方打个断点,看有没有进函数给赋值,然后在使用全局变量的函数那块也打个断点,看会不会进函数以及值的变化,如果说变量被赋值后面又成0,那你就先找到它时候被赋值啥时候成的0,找到这俩节点,你自己就能改了,这个问题不难解决,你可以先自己试一下 高深莫测
页:
[1]