void ofApp::show_merge(int play_cnt) { memcpy(tmp, recorded + image_size * play_cnt, image_size); memcpy(merged, show, image_size); bgs(tmp); image_merge(merged, tmp, 0); img.setFromPixels(merged, width, height, OF_IMAGE_COLOR); img.draw(width, height); }
void activations::getImage(ofImage & img) { ofPixels pix; pix.allocate(rows, cols, OF_PIXELS_GRAY); for (int i=0; i<rows*cols; i++) { pix[i] = ofMap(acts[i], min, max, 0, 255); } img.setFromPixels(pix); }
void testApp::update() { if(wrapper.isFrameNew()) { timer.tick(); } ofSetWindowTitle("FPS:" + ofToString(ofGetFrameRate())); cameraImage.setFromPixels(wrapper.pixels, wrapper.cameraWidth, wrapper.cameraHeight, OF_IMAGE_COLOR); }
//-------------------------------------------------------------- void testApp::update() { subs.update(); if (subs.isFrameNew()) { image.setFromPixels(subs.getPixelsRef()); } ofSetWindowTitle(ofToString(subs.getFps(), 2)); }
void makeThumb(string vidPath, string thumb){ ofVideoPlayer tmp; tmp.loadMovie(vidPath); tmp.play(); tmp.setPosition(0.3); ofImage img; img.setFromPixels( tmp.getPixelsRef() ); img.resize(120, 120.0f * (img.getHeight() / img.getWidth()) ); img.saveImage(thumb); }
//-------------------------------------------------------------- void testApp::setup(){ ofEnableAlphaBlending(); //load image file string path = ofToDataPath("logo.png"); of_image.loadImage( path ); ci_surface = ci::loadImage( path ); //convert images ci_texture = ci::gl::Texture( ofxCi::toCi(of_image), GL_RGBA, of_image.getWidth(), of_image.getHeight() ); of_image.setFromPixels(ofxCi::toOf(ci_surface), ci_surface.getWidth(), ci_surface.getHeight(), OF_IMAGE_COLOR_ALPHA); }
//-------------------------------------------------------------- void ofApp::update(){ //Creating image int w = 512; //Image width int h = 512; //Image height //Allocate array for filling pixels data unsigned char *data = new unsigned char[w * h * 4]; //Fill array for each pixel (x,y) for (int y=0; y<h; y++) { for (int x=0; x<w; x++) { //Compute preliminary values, //needed for our pixel color calculation: //1. Time from application start float time = ofGetElapsedTimef(); //2. Level of hyperbola value of x and y with //center in w/2, h/2 float v = ( x - w/2 ) * ( y - h/2 ); //3. Combining v with time for motion effect float u= v * 0.00025 + time; //Here 0.00025 was chosen empirically //4. Compute color components as periodical //functions of u, and stretched to [0..255] int red = ofMap( sin( u ), -1, 1, 0, 255 ); int green = ofMap( sin( u * 2 ), -1, 1, 0, 255 ); int blue = 255 - green; int alpha = 255; //Just constant for simplicity //Fill array components for pixel (x, y): int index = 4 * ( x + w * y ); data[ index ] = red; data[ index + 1 ] = green; data[ index + 2 ] = blue; data[ index + 3 ] = alpha; } } //Load array to image image.setFromPixels( data, w, h, OF_IMAGE_COLOR_ALPHA ); //Array is not needed anymore, so clear memory delete[] data; }
//-------------------------------------------------------------- void testApp::update(){ video.update(); if ( bVideoSetup && video.isFrameNew() ){ static unsigned long size; static ofImage currentImage; currentImage.setFromPixels(video.getPixelsRef()); // compress video into image via turbojpg // the second param == quality. play with this to get a better framerate // you can also try resizing your image! //currentImage.resize(160, 120); unsigned char * compressed = turbo.compress(¤tImage,50,&size); server.sendBinary(compressed, size); free(compressed); } }
void ofxLibdc::getOneShot(ofImage& img) { setTransmit(false); flush(); dc1394_video_set_one_shot(camera, DC1394_ON); dc1394video_frame_t *frame; dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame); img.allocate(width, height, imageType); if(imageType == OF_IMAGE_GRAYSCALE) { memcpy(img.getPixels(), frame->image, width * height); } else if(imageType == OF_IMAGE_COLOR) { // this shouldn't be reallocated every frame! dc1394video_frame_t* rgbFrame = (dc1394video_frame_t*) calloc(1, sizeof(dc1394video_frame_t)); rgbFrame->color_coding = DC1394_COLOR_CODING_RGB8; dc1394_convert_frames(frame, rgbFrame); memcpy(img.getPixels(), rgbFrame->image, 3 * width * height); free(rgbFrame); } img.setFromPixels(frame->image, width, height, imageType); dc1394_capture_enqueue(camera, frame); }
//-------------------------------------------------------------- void ofApp::draw(){ img.setFromPixels(show, width, height, OF_IMAGE_COLOR); img.draw(width * 2, 0); // save_image(img); if (rec_flag) { record(); } if (play_cnt++ >= play_margin || play_cnt >= record_size) { play_cnt = 0; play_start = get_start_point(); } show_cut(play_start + play_cnt); show_bgs(); show_back(); show_merge(play_start + play_cnt); // text ofDrawBitmapString("play_cnt:" + ofToString(play_cnt), 0, 10); ofDrawBitmapString("record_cnt:" + ofToString(rec_cnt), 0, 20); ofDrawBitmapString("R", trackers[0].x, trackers[0].y); ofDrawBitmapString("G", trackers[1].x, trackers[1].y); ofDrawBitmapString("B", trackers[2].x, trackers[2].y); }
void loadImageRaw(string path, int w, int h, ofImage &img) { ofFile file(path); img.setFromPixels((unsigned char*)file.readToBuffer().getData(), w, h, OF_IMAGE_COLOR); }
void ofApp::show_back() { img.setFromPixels(back, width, height, OF_IMAGE_COLOR); img.draw(0, height); }
void ofApp::show_bgs() { memcpy(tmp, show, image_size); bgs(tmp); img.setFromPixels(tmp, width, height, OF_IMAGE_COLOR); img.draw(0, 0); }
void ofApp::show_cut(int play_cnt) { memcpy(tmp, recorded + image_size * play_cnt, image_size); bgs(tmp); img.setFromPixels(tmp, width, height, OF_IMAGE_COLOR); img.draw(width, 0); }
void ofxCorrectPerspective::Rectify_Image(ofImage & my_image,double focal_length,double sensor_width, double & alpha, double & beta){ resize_image=1; //To Enable or Disable Resize //Rescale Image to be Max in 1000px if (resize_image && max(my_image.width,my_image.height)>1000) { float s=float(1000)/max(my_image.width,my_image.height); my_image.resize(floor(my_image.width*s),floor(my_image.height*s)); } float f=focal_length*(float)max(my_image.width,my_image.height)/sensor_width; K.set(f,0.0,0.0,0.0,f,0.0,0.0,0.0,1.0); center.set(double(my_image.width)/2.0,double(my_image.height)/2.0); if (talk) cout << K; my_img_gray=my_image; my_img_gray.setImageType(OF_IMAGE_GRAYSCALE); //Converting Image to Image Double// image_double dub_image; ntuple_list lsd_out; unsigned int w=my_img_gray.width; unsigned int h=my_img_gray.height; unsigned char * imgP=my_img_gray.getPixels(); // LSD parameters start double scale = 0.8; // Scale the image by Gaussian filter to 'scale'. double sigma_scale = 0.6; // Sigma for Gaussian filter is computed as sigma = sigma_scale/scale. double quant = 2.0; // Bound to the quantization error on the gradient norm. double ang_th = 22.5; // Gradient angle tolerance in degrees. double eps = 0.0; // Detection threshold, -log10(NFA). double density_th = 0.7; // Minimal density of region points in rectangle. int n_bins = 1024; // Number of bins in pseudo-ordering of gradient modulus. double max_grad = 255.0; // Gradient modulus in the highest bin. The default value corresponds to the highest // gradient modulus on images with gray levels in [0,255]. // LSD parameters end bool verbose=0; dub_image = new_image_double(w,h); double px=0; cout << "\n--------\nInput data being written to image buffer \n"; for(int j=0;j<(w*h);j++){ px=imgP[j]; dub_image->data[j] = px; if (verbose){ cout << " " << dub_image->data[j]; } } // Call LSD // lsd_out = LineSegmentDetection( dub_image, scale, sigma_scale, quant, ang_th, eps, density_th, n_bins, max_grad, NULL ); cout << "LSD has done it's thing!\n"; if (talk) cout << "Number of Lines: "<< lsd_out->size << "Number of Dimensions: " << lsd_out->dim << "\n"; if (verbose) { cout << "LSD Values: " << lsd_out->values[0] << " " << lsd_out->values[1] <<" " << lsd_out->values[2] <<" " << lsd_out->values[3] <<" " << lsd_out->values[4] <<" " << lsd_out->values[5] << "\n"; } //Sorting in (Value, Index) pairs //http://stackoverflow.com/questions/1577475/c-sorting-and-keeping-track-of-indexes std::vector<mypair> line_lengths; double sqd_distance; mesh.setMode(OF_PRIMITIVE_LINES); mesh.enableColors(); ofVec3f first(0.0,0.0,0.0); ofVec3f second(0.0,0.0,0.0); double x1,x2,y1,y2; for(int j=0;j<(lsd_out->size*lsd_out->dim);j=j+5){ x1=lsd_out->values[j]; y1=lsd_out->values[j+1]; x2=lsd_out->values[j+2]; y2=lsd_out->values[j+3]; sqd_distance=(x2-x1)*(x2-x1) + (y2-y1)*(y2-y1); line_lengths.push_back(make_pair(sqd_distance,j)); //To Draw as Primitive Lines// /*first.set(x2,y2,0.0); second.set(x1,y1,0.0); mesh.addVertex(first); mesh.addColor(ofFloatColor(1.0, 0.0, 0.0)); mesh.addVertex(second); mesh.addColor(ofFloatColor(1.0, 0.0, 0.0));*/ //Lines Added, will be drawn// } sort(line_lengths.begin(),line_lengths.end()); reverse(line_lengths.begin(), line_lengths.end()); if (talk) cout << line_lengths[0].first << " " << line_lengths[0].second << " " << line_lengths[1].first << " " << line_lengths[1].second << "\n"; unsigned int maxlines=700; unsigned int no_of_lines=min(lsd_out->size,maxlines); //Store these Lines pairs in a Matrix, in descending order of Distance cout << "Number of Lines: " << no_of_lines << "\n"; L.resize(4); for (int i = 0; i < 4; ++i){ L[i].resize(no_of_lines); } for (int j=0; j<no_of_lines; j++){ L[0][j] = lsd_out->values[line_lengths[j].second]; L[1][j] = lsd_out->values[(line_lengths[j].second)+1]; L[2][j] = lsd_out->values[(line_lengths[j].second)+2]; L[3][j] = lsd_out->values[(line_lengths[j].second)+3]; } //LINE EXTENSION double extension_fac=.15; //For one side of the line double line_length; double line_gradient; double rise_angle; double delta_x; double delta_y; for (int j=0; j<no_of_lines; j++){ x1=L[0][j]; y1=L[1][j]; x2=L[2][j]; y2=L[3][j]; line_length=sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)); line_gradient=(y2-y1)/(x2-x1); rise_angle=atan(abs(line_gradient)); delta_x=cos(rise_angle)*(line_length*extension_fac); delta_y=sin(rise_angle)*(line_length*extension_fac); if (line_gradient<0) { if (x1>x2) { L[0][j]+=delta_x; L[1][j]-=delta_y; L[2][j]-=delta_x; L[3][j]+=delta_y; } else { L[2][j]+=delta_x; L[3][j]-=delta_y; L[0][j]-=delta_x; L[1][j]+=delta_y; } } else { if (x1>x2) { L[0][j]+=delta_x; L[1][j]+=delta_y; L[2][j]-=delta_x; L[3][j]-=delta_y; } else { L[2][j]+=delta_x; L[3][j]+=delta_y; L[0][j]-=delta_x; L[1][j]-=delta_y; } } x1=L[0][j]; y1=L[1][j]; x2=L[2][j]; y2=L[3][j]; L[2][j]-=center.x; //Move Origin to the Principle Point L[3][j]-=center.y; L[0][j]-=center.x; L[1][j]-=center.y; } //Finding ADJACENT Lines// bool adjflag=1; std::vector<int> ar; //To hold Adjacent Row Values std::vector<int> ac; //To hold Adjacent Column Values if (adjflag) { double athreshadj=10; std::vector<std::vector<bool> > adj; //Line x Line Inf Matrix Initialization, adj //Not Used at the Moment. adj.resize(no_of_lines); //Height for (int i = 0; i < no_of_lines; ++i){ adj[i].resize(no_of_lines); for (int j = 0; j < no_of_lines; ++j){ //adj[i][j]=1.0/0.0; adj[i][j]=0; } } ofVec2f v1,v2,x; athreshadj=abs(cos((athreshadj*PI)/180.0)); for (int i = 0; i < no_of_lines; ++i){ for (int j = i+1; j < no_of_lines; ++j){ //Everyline in-front v1.set(L[0][i]-L[2][i],L[1][i]-L[3][i]); v2.set(L[0][j]-L[2][j],L[1][j]-L[3][j]); v1.normalize(); v2.normalize(); if (abs(v1.dot(v2))<athreshadj) //acos(v1.dot(v2)) //So Angle is greater! { x=solveLinearSys(L[0][i]-L[2][i],-L[0][j]+L[2][j],L[1][i]-L[3][i],-L[1][j]+L[3][j],-L[2][i]+L[2][j],-L[3][i]+L[3][j]); if (not isinf(x.x) and not isinf(x.y)) { adj[i][j]=(x.x>=-DBL_EPSILON) && (x.x<=1+DBL_EPSILON) && (x.y>=-DBL_EPSILON) && (x.y<=1+DBL_EPSILON); adj[j][i]=adj[i][j] || adj[j][i]; if (adj[i][j]) { ar.push_back(i); ac.push_back(j); } } } } } } else { for (int i = 0; i < no_of_lines; ++i){ // nC2 Pairs for (int j = i+1; j < no_of_lines; ++j){ ar.push_back(i); ac.push_back(j); } } } cout << "No. of Pairs: "<< ac.size() << "\n"; //Adjacent Matrix FOUND// //Convert Line Segments to vector format for Rectification// std::vector<std::vector<double> > L_vec; //Line's Vector Form L_vec.resize(3); //Height for (int i = 0; i < 3; ++i){ L_vec[i].resize(no_of_lines); } Mat A(3,3,CV_32F); Mat s, u, vt; A.at<float>(0,2)=1; A.at<float>(1,2)=1; A.at<float>(2,0)=0; A.at<float>(2,1)=0; A.at<float>(2,2)=0; for (int i = 0; i < no_of_lines; ++i){ A.at<float>(0,0)=L[0][i]; A.at<float>(0,1)=L[1][i]; A.at<float>(1,0)=L[2][i]; A.at<float>(1,1)=L[3][i]; SVD::compute(A, s, u, vt); //YY=U*S*V' vt=vt.t(); vt.col(0)=vt.col(0)*-1; L_vec[0][i]=vt.at<float>(0,2); L_vec[1][i]=vt.at<float>(1,2); L_vec[2][i]=vt.at<float>(2,2); } //RANSAC// //int PairsC2=(ac.size()-1)*ac.size()/2; unsigned int maxTrials=400; //min(PairsC2*5,200); cout << "RANSAC: No. of Trials = " << maxTrials << endl; unsigned int trialcount=0; unsigned int r1, r2, r3, r4; unsigned int r_ind1, r_ind2; column_vector modelX; std::vector<int> arIn; //To hold Adjacent Row Values std::vector<int> acIn; //To hold Adjacent Column Values double thresh=0.001; std::vector<int> Best_arIn; //Best Adjacent Row Values std::vector<int> Best_acIn; //Best Adjacent Column Values unsigned int Bestscore=0; //Number of Inliers unsigned int score=0; column_vector Best_modelX; while (trialcount<maxTrials) { arIn.resize(0); acIn.resize(0); r_ind1=floor(ofRandom(ac.size())); r_ind2=floor(ofRandom(ac.size())); r1=ar[r_ind1]; r2=ar[r_ind2]; r3=ac[r_ind1]; r4=ac[r_ind2]; modelX=fitFunc4Lines(L_vec, r1, r2, r3, r4, f); //Fitting Function distFunc(L_vec,modelX,f,thresh,arIn,acIn); score=arIn.size(); //score=distFunc2(L_vec,modelX,f,thresh,arIn,acIn, ar, ac); //Distance Function if (Bestscore<score) { Bestscore=score; Best_arIn=arIn; Best_acIn=acIn; Best_modelX=modelX; if (talk) cout << "No. of Inliers: "<< Bestscore<<endl; } trialcount++; } cout << "RANSAC Done." << endl; cout << "No. of Inliers: "<< Bestscore<<endl; column_vector solution=Best_modelX; if (talk) cout << "cost_function solution:\n" << Best_modelX << endl; cout << "cost_function solution:\n" << solution << endl; alpha=solution(0); beta=solution(1); //APPLYING HOMOGRAPHY for this SOLUTION// ofMatrix4x4 R=ofMatrix4x4::newRotationMatrix(solution(0)*180.0/PI, ofVec3f(-1, 0, 0), solution(1)*180.0/PI, ofVec3f(0, -1, 0), 0, ofVec3f(0, 0, -1)); double m[3][3] = {{R(0,0), R(0,1), R(0,2)}, {R(1,0), R(1,1), R(1,2)}, {R(2,0), R(2,1), R(2,2)}}; cv::Mat R_mat = cv::Mat(3, 3, CV_64F, m); cv::Mat K_mat = (cv::Mat_<double>(3,3)<< f,0.0,0.0,0.0,f,0.0,0.0,0.0,1.0); cv::Mat K_c= K_mat.clone(); K_c=K_c.inv(); cv::Mat C = (cv::Mat_<double>(3,3)<< 1,0,-center.x,0,1,-center.y,0,0,1); cv::Mat H=K_mat*R_mat*K_c*C; //Calclating Resultant Translation and Scale std::vector<Point2f> Ref_c; std::vector<Point2f> Ref_c_out; Ref_c.resize(4); Ref_c_out.resize(4); Ref_c[0].x=0; Ref_c[0].y=0; Ref_c[1].x=double(my_image.width); Ref_c[1].y=0; Ref_c[2].x=double(my_image.width); Ref_c[2].y=double(my_image.height); Ref_c[3].x=0; Ref_c[3].y=double(my_image.height); perspectiveTransform(Ref_c, Ref_c_out, H); if (talk) cout << "Ref Out New: " << Ref_c_out << endl; //Scalling: double scale_fac=abs((max(Ref_c_out[1].x,Ref_c_out[2].x)-min(Ref_c_out[0].x,Ref_c_out[3].x))/my_image.width); //Based on Length if (talk) cout << "Scale Factor: " << scale_fac << endl; Ref_c_out[0].x=Ref_c_out[0].x/scale_fac; Ref_c_out[0].y=Ref_c_out[0].y/scale_fac; Ref_c_out[1].x=Ref_c_out[1].x/scale_fac; Ref_c_out[1].y=Ref_c_out[1].y/scale_fac; Ref_c_out[2].x=Ref_c_out[2].x/scale_fac; Ref_c_out[2].y=Ref_c_out[2].y/scale_fac; Ref_c_out[3].x=Ref_c_out[3].x/scale_fac; Ref_c_out[3].y=Ref_c_out[3].y/scale_fac; Ref_c_out[1].x=Ref_c_out[1].x-Ref_c_out[0].x; Ref_c_out[1].y=Ref_c_out[1].y-Ref_c_out[0].y; Ref_c_out[2].x=Ref_c_out[2].x-Ref_c_out[0].x; Ref_c_out[2].y=Ref_c_out[2].y-Ref_c_out[0].y; Ref_c_out[3].x=Ref_c_out[3].x-Ref_c_out[0].x; Ref_c_out[3].y=Ref_c_out[3].y-Ref_c_out[0].y; Ref_c_out[0].x=Ref_c_out[0].x-Ref_c_out[0].x; Ref_c_out[0].y=Ref_c_out[0].y-Ref_c_out[0].y; if (talk) (cout << "Ref Out New: " << Ref_c_out << endl); H = getPerspectiveTransform( Ref_c, Ref_c_out ); //For the Translated/Scalled Image //Applying Homography// Mat src_img(cv:: Size (my_image.width, my_image.height),CV_8UC3,my_image.getPixels()); //OF to OpenCV cv::Mat dst_img; dst_img.create(src_img.size(), src_img.type()); cv::warpPerspective(src_img, dst_img, H, src_img.size(), cv::INTER_LINEAR); //OpenCV to OF my_image.setFromPixels((unsigned char *) IplImage(dst_img). imageData,dst_img.size().width, dst_img.size().height,OF_IMAGE_COLOR); cout << endl <<"Press any key to Save Output Image." << endl; }