#include "PolyToQImage.h" #include using namespace vcg; using namespace std; void PolyDumper::rectSetToPolySet(vector< Box2f > &rectVec, vector< vector > &polyVec) { polyVec.clear(); for(size_t i=0;i > &polyVec, vector &trVec, PolyDumperParam &pp) { vector< vector< vector > > polyVecVec(polyVec.size()); for(size_t i=0;i > &polyVec, vector &trVec, PolyDumperParam &pp) { vector< vector< vector > > polyVecVec(polyVec.size()); for(size_t i=0;i > > &polyVecVec, vector &trVec, PolyDumperParam &pp) { vector labelVec(polyVecVec.size()); dumpPolySetPNG(imageName,polyVecVec,trVec,labelVec,pp); } void PolyDumper::dumpPolySetSVG(const char * imageName, vector< vector< vector > > &polyVecVec, vector &trVec, PolyDumperParam &pp) { vector labelVec(polyVecVec.size()); dumpPolySetSVG(imageName,polyVecVec,trVec,labelVec,pp); } ///this class draw a black mask fora given polygon, cenetered and scaled to fit with ///the image size, it return the transformation to tranform back the polygon to 2D space void PolyDumper::DrawPolygonMask(const vector< vector > &polyVec, QImage &img, Similarity2f &ret, const Similarity2f &trans) { ///RASTERIZE THE POLYGON QPainter painter; painter.begin(&img); QBrush bb; bb.setStyle(Qt::SolidPattern); int resolution=img.width(); QPen qp; qp.setWidthF(0); ///find the BB vcg::Box2f bbox; for(size_t i=0;i ppQ; for(size_t j=0;j > &polyVec, const Similarity2f &tra1, int &radius, int resolution) { ///INITIALIZE THE IMAGE QImage img(resolution,resolution,QImage::Format_RGB32); img.fill(vcg::ColorConverter::ToQColor(vcg::Color4b(255,255,255,255)).rgb()); Similarity2f tra0; ///DRAW THE MASK DrawPolygonMask(polyVec,img,tra0,tra1); //img = img.mirrored(false,true); ///THEN FIND THE CENTROID float Maxradius=-1; vcg::Point2i incenter=vcg::Point2i(0,0); int sizeY=img.size().height(); int sizeX=img.size().width(); ///THEN GO OVER ALL VERTICES for (int x=0;xMaxradius) { Maxradius=curr_radius; incenter=vcg::Point2i(x,y); } } vcg::Point2f incenterf; incenterf.Import(incenter); ///FIRST TRASNFORMATION //QPainter painter; //painter.begin(&img); ////painter.fillRect(incenter.V(0)-Maxradius,incenter.V(1)-Maxradius,Maxradius*2,Maxradius*2,Qt::red); //painter.fillRect(incenter.V(0)-1,incenter.V(1)-1,2,2,Qt::red); ////painter.drawPoint(incenter.V(0),incenter.V(1)); //painter.end(); /*static int num=0; num++; char path[100]; sprintf(path,"mask%d.png",num); img.save(path);*/ incenterf.X()-=tra0.tra[0]; incenterf.Y()-=tra0.tra[1]; incenterf*=1.0/tra0.sca; incenterf*=tra1.sca; ///SECOND TRANSFORMATION assert(Maxradius>0); radius=Maxradius; return incenterf; } ///write a polygon on a PNG file, format of the polygon is vector of vector of contours...nested contours are holes ///takes the name of the image in input, the set of polygons, the set of per polygons transformation, ///the label to be written and the global parameter for drawing style void PolyDumper::dumpPolySetPNG(const char * imageName, vector< vector< vector > > &polyVecVec, vector &trVec, vector &labelVec, PolyDumperParam &pp) { ///SET THE FONT assert(polyVecVec.size() == trVec.size()); int fontSize=ceil(std::max(pp.width,pp.height)/100.0); QFont qf("courier",fontSize); ///SET THE DRAWING SIZE QImage img(pp.width,pp.height,QImage::Format_RGB32); img.fill(vcg::ColorConverter::ToQColor( pp.backgroundColor).rgb()); ///SETUP OF DRAWING PROCEDURE QPainter painter; painter.begin(&img); QBrush bb; if (pp.fill) bb.setStyle(Qt::SolidPattern); QPen qp; qp.setWidthF(0); for(size_t i=0;i ppQ; for(size_t j=0;j > > &polyVecVec, vector &trVec, vector &labelVec, PolyDumperParam &pp) { assert(polyVecVec.size() == trVec.size()); ///SET THE FONT int fontSize; if(pp.fontSize==0) fontSize=ceil(std::max(pp.width,pp.height)/100.0); else fontSize=pp.fontSize; QFont qf("courier",fontSize); QSvgGenerator svg; svg.setFileName(imageName); ///SET THE DRAWING SIZE svg.setSize(QSize(pp.width,pp.height)); svg.setViewBox(QRect(0, 0, pp.width,pp.height)); svg.setResolution(int(pp.dpi));// ///SETUP OF DRAWING PROCEDURE QPainter painter; painter.begin(&svg); QBrush bb; if (pp.fill) bb.setStyle(Qt::SolidPattern); QPen qp; ///SET THE GLOBAL SCALING FACTOR float Scalesvg=1.f/(float)trVec[0].sca; qp.setWidthF(Scalesvg); for(size_t i=0;i ppQ; for(size_t j=0;j