void run(){ listener->waitForNewFrame(*frames); libfreenect2::Frame *rgb = (*frames)[libfreenect2::Frame::Color]; libfreenect2::Frame *ir = (*frames)[libfreenect2::Frame::Ir]; libfreenect2::Frame *depth = (*frames)[libfreenect2::Frame::Depth]; static Img8u colorImage(Size(rgb->width,rgb->height),3); if(depth){ Img32f depthImage(Size(depth->width,depth->height),formatMatrix, std::vector<float*>(1, (float*)depth->data)); gui["hdepth"] = &depthImage; }else{ throw ICLException("error detected in libfreenect2.so: please ensure to deactivate" " visualization in ....h by setting debug_on to false"); } Img32f irImage(Size(ir->width,ir->height),formatMatrix, std::vector<float*>(1,(float*)ir->data)); interleavedToPlanar(rgb->data, &colorImage); colorImage.swapChannels(0,2); //gui["hdepth"] = &depthImage; gui["hcolor"] = &colorImage; gui["hir"] = &irImage; listener->release(*frames); }
const Img8u &thresh(const Img8u &input, icl8u t){ static Img8u result; result.setChannels(1); result.setSize(input.getSize()); int t3 = 3*t; for(int x=0;x<input.getWidth();++x){ for(int y=0;y<input.getHeight();++y){ result(x,y,0) = 255*((input(x,y,0)+input(x,y,1)+input(x,y,2))>t3); } } return result; }
TemplateTracker::Result TemplateTracker::track(const Img8u &image, const Result *initialResult, std::vector<Result> *allResults){ Result last = initialResult ? *initialResult : data->lastResult; if(last.pos == Point32f(-1,-1)){ last.pos = Point(image.getWidth()/2,image.getHeight()/2); } const double angle = last.angle; const int X = last.pos.x; const int Y = last.pos.y; const int lutSize = (int)data->lut.size(); const int angleIndex = angle / (2*M_PI) * (lutSize-1); const int ROI = getPropertyValue("tracking.position range"); const float rotationRange = getPropertyValue("tracking.rotation range"); const int step1 = getPropertyValue("tracking.coarse steps"); //const int step2 = getPropertyValue("tracking.fine steps"); //const float angleStepSize = 360./lutSize; const Rect roi(X - ROI/2, Y-ROI/2, ROI, ROI); SmartPtr<const Img8u> roiImageTmp = image.shallowCopy(roi); SmartPtr<Img8u> roiImage = roiImageTmp->deepCopyROI(); const int stepRadius = lutSize * rotationRange/720; Result bestResult = last; for(int i = -stepRadius; i <= stepRadius; i+= step1){ int curIndex = angleIndex + i; while(curIndex >= lutSize) curIndex -= lutSize; while(curIndex < 0) curIndex += lutSize; data->prox->apply(roiImage.get(),data->lut.at(curIndex).get(),&data->buf); Point maxPos; float maxValue = data->buf->getMax(0,&maxPos); Result curr(maxPos+last.pos, (float(curIndex)/lutSize) * 2 * M_PI, maxValue, data->lut.at(curIndex).get()); if(curr.proximityValue > bestResult.proximityValue){ bestResult = curr; } if(allResults) allResults->push_back(curr); } data->lastResult = bestResult; return bestResult; }
int main(int n, char **a){ pa_explain("-o","output filename (should be some format, that supports alpha channel such as png)\n" "or, if no output is given, the image is just show"); pa_init(n,a,"-icon-name|-i(iconname=empty) -output|-o(2) -image-file-to-c++-array|-ita(input-file-name)"); if(pa("-ita")){ Img8u image = load<icl8u>(pa("-ita")); const int w=image.getWidth(), h = image.getHeight(), c = image.getChannels(); std::cout << "static icl8u data_XYZ["<<w<<"]["<<h<<"]["<<c<<"]={" << std::endl; for(int y=0;y<h;++y){ std::cout << "{"; for(int x=0;x<w;++x){ std::cout << "{"; for(int i=0;i<c;++i){ std::cout << (int)image(x,y,i) << (i<c-1?",":"}"); } std::cout << (x<w-1?",":"}"); } std::cout << (y<h-1?",":"};") << std::endl; } std::cout << "//#include <ICLCore/Img.h>" << std::endl; std::cout << "//#include <ICLCC/CCFunctions.h>" << std::endl; std::cout << "const Img8u& load_data_XYZ(){" << std::endl; std::cout << " static SmartPtr<Img8u> image;" << std::endl; std::cout << " if(!image){" << std::endl; std::cout << " image = new Img8u(Size(" << w << "," << h << ")," << c << ");" << std::endl; std::cout << " interleavedToPlanar((const icl8u*)data_XYZ, image.get());" << std::endl; std::cout << " }" << std::endl; std::cout << " return *image;" << std::endl; std::cout << "}" << std::endl; }else{ const Img8u &image = IconFactory::create_image(pa("-i")); if(pa("-o")){ GenericImageOutput out(pa("-o")); out.send(&image); }else{ show(image); } } }
void TemplateTracker::setTemplateImage(const Img8u &templateImage, float rotationStepSizeDegree){ Img8u test = templateImage; test.scale(test.getSize()*4); RotateOp rot; ICLASSERT_RETURN(rotationStepSizeDegree > 0.001); const int w = templateImage.getWidth(); const int h = templateImage.getHeight(); data->lut.clear(); for(float a=0;a<=360;a+=rotationStepSizeDegree){ rot.setAngle(a); const Img8u *r = rot.apply(&templateImage)->as8u(); const int rw = r->getWidth(); const int rh = r->getHeight(); Rect center((rw-w)/2, (rh-h)/2, w,h); SmartPtr<const Img8u> roiimage = r->shallowCopy(center); Img8u *tmp = new Img8u(test.getSize()/4,1); // data->lut.push_back(roiimage->deepCopyROI()); roiimage->scaledCopyROI(tmp,interpolateLIN); data->lut.push_back(tmp); } }
void run(){ bool c_arg = pa("-c"); static FPSLimiter fpsLimit(30); fpsLimit.wait(); const ImgBase *image = grabber.grab(); DrawHandle draw = gui["draw"]; ImageHandle cropped = gui["cropped"]; draw = image; static RotateOp rot; if(c_arg){ rot.setAngle(0); }else{ rot.setAngle(parse<int>(gui["rot"])); } const ImgBase *cro = 0; if(c_arg || gui["rect"].as<bool>()){ static Img8u roi; std::vector<utils::Rect> rs = mouse_2->getRects(); ICLASSERT_THROW(rs.size() == 1, ICLException("expected exactly one rectangle")); lastRect = rs[0]; mouse_2->visualize(**draw); SmartPtr<const ImgBase> tmp = image->shallowCopy(rs[0] & image->getImageRect()); roi.setChannels(tmp->getChannels()); roi.setFormat(tmp->getFormat()); roi.setSize(tmp->getROISize()); tmp->convertROI(&roi); cro = rot.apply(&roi); draw->color(0,255,0,255); draw->text(str(rs[0]), rs[0].x, rs[0].y); }else{ draw->draw(mouse_1->vis()); Size32f s(gui["s1"],gui["s2"]); Point32f ps[4] = { mouse_1->ps[0], mouse_1->ps[1], mouse_1->ps[2], mouse_1->ps[3] }; switch(image->getDepth()){ #define ICL_INSTANTIATE_DEPTH(D) \ case depth##D:{ \ static ImageRectification<icl##D> ir; \ try{ \ cro = rot.apply(&ir.apply(ps,*image->as##D(),s)); \ }catch(...){} \ break; \ } ICL_INSTANTIATE_ALL_DEPTHS; #undef ICL_INSTANTIATE_DEPTH } } if(cro){ cropped = cro; currMutex.lock(); cro->convert(&curr); currMutex.unlock(); } gui["draw"].render(); gui["fps"].render(); }