Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wonderful / Tamaki ROY https://vimeo.com/70712087
#include "testApp.h"
#include "ofxUI.h"
#include "ofxOpenCv.h"
#define TITLE "sketch wonderful ver.000"
ofVideoPlayer video;
ofxCvColorImage ofxSrcVid,ofxDstVid; // 画像取得用、描画用
IplImage *srcVid, *dstVid, *markersVid;
ofImage capimg;
int w3,h3, sw,sh;
int level; // セグメント化のためのピラミッド最大レベル
int size; // bit数レベル
int space;
float threshold1, threshold2;
// GUI
ofxUICanvas *gui;
void guiEvent(ofxUIEventArgs &e);
ofColor bgColor;
void testApp::setup()
{
// window setting
ofSetFrameRate(60);
ofSetCircleResolution(60);
ofEnableSmoothing();
sw = 1280;
sh = 720;
w3 = 320; // 1280
h3 = 180; // 720
// video
video.loadMovie("wonderful_green.mp4");
video.play();
ofxSrcVid.allocate(w3, h3);
ofxDstVid.allocate(w3, h3);
srcVid = ofxSrcVid.getCvImage();
dstVid = cvCloneImage(srcVid);
capimg.allocate( w3, h3, OF_IMAGE_COLOR );
guiset();
}
void testApp::update()
{
ofSetWindowTitle("fps "+ofToString(int(ofGetFrameRate())));
// video
video.update();
ofxSrcVid.setFromPixels(video.getPixels(), 1920, 1080 );
ofxSrcVid.resize(w3, h3);
srcVid = ofxSrcVid.getCvImage();
CvSeq *comp3 = 0;
CvMemStorage *storage3 = 0;
storage3 = cvCreateMemStorage (0);
cvPyrSegmentation(srcVid, dstVid, storage3, &comp3, 2, threshold1, threshold2); // level=2,4
ofxDstVid = dstVid;
unsigned char * pixels = ofxDstVid.getPixels(); //キャプチャ用の変数
unsigned char pixs4[w3*h3*3];
for(int y = 0; y < h3; y++){
for(int x = 0; x < w3; x++){
//それぞれのピクセルのr, g, bを抽出
pixs4[ y*3*w3 + x*3 ] = pixels[ y*3*w3 + x*3 ];
pixs4[ y*3*w3 + x*3+1 ] = pixels[ y*3*w3 + x*3+1 ];
pixs4[ y*3*w3 + x*3+2 ] = pixels[ y*3*w3 + x*3+2 ];
}
}
capimg.setFromPixels( pixs4, w3, h3, OF_IMAGE_COLOR );
}
void testApp::draw()
{
ofSetColor(255);
glEnable(GL_DEPTH_TEST);
ofBackground(bgColor);
ofSetColor(255);
unsigned char * pixels = capimg.getPixels(); //キャプチャ用の変数
for(int y = 0; y < h3; y+=space){
for(int x = 0; x < w3; x+=space){
int r2 = pixels[y * 3 * w3 + x * 3];
int g2 = pixels[y * 3 * w3 + x * 3 + 1];
int b2 = pixels[y * 3 * w3 + x * 3 + 2];
ofSetColor(r2, g2, b2);
// ofCircle( x*(sw/w3)+size*0.5, y*(sh/h3)+size*0.5, size*0.5); // ●
// ofRect( x*(sw/w3), y*(sh/h3), size, size); // ■
ofBox(x*(sw/w3), y*(sh/h3), (r2+g2+b2)/255*size*space, size);
}
}
}
void testApp::keyPressed(int key){}
void testApp::keyReleased(int key){}
void testApp::mouseMoved(int x, int y ){}
void testApp::mouseDragged(int x, int y, int button){}
void testApp::mousePressed(int x, int y, int button){}
void testApp::mouseReleased(int x, int y, int button){}
void testApp::windowResized(int w, int h){}
void testApp::gotMessage(ofMessage msg){}
void testApp::dragEvent(ofDragInfo dragInfo){}
void testApp::exit(){
delete gui;
}
// GUI
void testApp::guiset()
{
float xInit = OFX_UI_GLOBAL_WIDGET_SPACING;
float barW = 310;
float barW3 = 101;
float barH = 12;
float length = 320-xInit;
gui = new ofxUICanvas(0,0,length+xInit*2.0,ofGetHeight());
ofAddListener(gui->newGUIEvent, this, &testApp::guiEvent);
gui->addWidgetDown(new ofxUILabel( string(TITLE), OFX_UI_FONT_MEDIUM));
gui->addWidgetDown(new ofxUIFPS(OFX_UI_FONT_SMALL));
gui->addSpacer(length, 1);
gui->addToggle("FullScreen", false, 16, 16);
gui->setWidgetPosition(OFX_UI_WIDGET_POSITION_RIGHT);
gui->addButton("Save", false, 16, 16);
gui->setWidgetPosition(OFX_UI_WIDGET_POSITION_DOWN);
gui->addSpacer(length, 1);
gui->addSlider("R", 0.0, 255.0, bgColor.r, barW3, barH);
gui->setWidgetPosition(OFX_UI_WIDGET_POSITION_RIGHT);
gui->addSlider("G", 0.0, 255.0, bgColor.g, barW3, barH);
gui->addSlider("B", 0.0, 255.0, bgColor.b, barW3, barH);
gui->setWidgetPosition(OFX_UI_WIDGET_POSITION_DOWN);
gui->addSpacer(length, 1);
gui->addSlider("threshold1", 1.0, 255.0, threshold1, barW3, barH);
gui->setWidgetPosition(OFX_UI_WIDGET_POSITION_RIGHT);
gui->addSlider("threshold2", 1.0, 255.0, threshold2, barW3, barH);
gui->setWidgetPosition(OFX_UI_WIDGET_POSITION_DOWN);
gui->addSlider("space", 2, 15, space, barW3, barH);
gui->setWidgetPosition(OFX_UI_WIDGET_POSITION_RIGHT);
gui->addSlider("size", 1, 50, size, barW3, barH);
gui->setWidgetPosition(OFX_UI_WIDGET_POSITION_DOWN);
gui->autoSizeToFitWidgets();
gui->loadSettings("GUI/guiSettings.xml");
}
void testApp::guiEvent(ofxUIEventArgs &e)
{
string name = e.widget->getName();
if(name == "FullScreen"){
ofxUIToggle *toggle = (ofxUIToggle *) e.widget;
ofSetFullscreen(toggle->getValue());
}else if(name == "Save"){
ofxUIButton *btn = (ofxUIButton *) e.widget;
if(btn->getValue()){
gui->saveSettings("GUI/guiSettings.xml");
ofLog(OF_LOG_NOTICE, "=== Save GUI Settings.");
}
}else if(name == "size"){
ofxUISlider *rslider = (ofxUISlider *) e.widget;
size = int(rslider->getScaledValue());
}else if(name == "space"){
ofxUISlider *rslider = (ofxUISlider *) e.widget;
space = int(rslider->getScaledValue());
}else if(name == "level"){
ofxUISlider *rslider = (ofxUISlider *) e.widget;
level = int(rslider->getScaledValue());
}else if(name == "threshold1"){
ofxUISlider *rslider = (ofxUISlider *) e.widget;
threshold1 = rslider->getScaledValue();
}else if(name == "threshold2"){
ofxUISlider *rslider = (ofxUISlider *) e.widget;
threshold2 = rslider->getScaledValue();
}else if(name == "R"){
ofxUISlider *rslider = (ofxUISlider *) e.widget;
bgColor.r = rslider->getScaledValue();
}else if(name == "G"){
ofxUISlider *rslider = (ofxUISlider *) e.widget;
bgColor.g = rslider->getScaledValue();
}else if(name == "B"){
ofxUISlider *rslider = (ofxUISlider *) e.widget;
bgColor.b = rslider->getScaledValue();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.