#include "project.h" Project::Project(Settings* _settings){ settings=_settings; filename=""; retrmode=CV_RETR_LIST; cannythresh=100; gcode=""; ivcombined=ImageViewer(&combined, "Images"); ivprocessed=ImageViewer(&processed, "Processing"); } Project::Project(Settings* _settings, std::string _filename){ settings=_settings; filename=_filename; retrmode=CV_RETR_LIST; cannythresh=100; gcode=""; ivcombined=ImageViewer(&combined, "Images"); ivprocessed=ImageViewer(&processed, "Processing"); } std::vector* Project::getImages(){ return &images; } int Project::getRetrMode(){ return retrmode; } int Project::getCannyThresh(){ return cannythresh; } std::map* Project::getColoring(){ return &coloring; } std::string* Project::getGcode(){ return &gcode; } void Project::setFileName(std::string _filename){ filename=_filename; } void Project::setRetrMode(int _retrmode){ retrmode=_retrmode; } void Project::setCannyThresh(int _cannythresh){ cannythresh=_cannythresh; } bool Project::newProj(){ //create files, save def, etc FILE* f=fopen(filename.c_str(), "w"); if(!f){ fprintf(stderr, "Error> Creating file for project. At: Project::newProj() (file: %s, line: %i)\n", __FILE__, __LINE__); return false; } fprintf(f,""); fclose(f); return true; } void Project::loadProj(){ //load stuff, deserialize } void Project::save(){ //serialize, save to file } void Project::close(){ //close project, free up resources; cv::destroyAllWindows(); } bool Project::combine(){ //get col and row count int colmax=0, rowmax=0; for(std::vector::iterator it=images.begin(); itgetMat(); //get resized mat cv::Mat resz; cv::resize(cur, resz, cv::Size(cur.cols*it->getScale(), cur.rows*it->getScale())); //get cropped mat cv::Mat crop=resz(cv::Rect(it->getCrop1().getX(), it->getCrop1().getY(), (it->getCrop2().getX()-it->getCrop1().getX())*it->getScale(), (it->getCrop2().getY()-it->getCrop1().getY())*it->getScale())); //crop debug if(settings->isDebug()){ ImageViewer* iv=new ImageViewer(crop, "DEBUG:combine> "+it->getFilename()); iv->show(); } if(it->getPosition().getX()+crop.cols > colmax){ colmax=it->getPosition().getX()+crop.cols; } if(it->getPosition().getY()+crop.rows > rowmax){ rowmax=it->getPosition().getY()+crop.rows; } //cleanup resz.release(); crop.release(); } if(settings->isDebug()){ printf("DEBUG:combine> colmax=%i, rowmax=%i\n", colmax, rowmax); } //create new mat combined=cv::Mat::zeros(rowmax, colmax, CV_8UC3); //add layers for(std::vector::iterator it=images.begin(); itgetMat(); //get resized mat cv::Mat resz; cv::resize(cur, resz, cv::Size(cur.cols*it->getScale(), cur.rows*it->getScale())); //get cropped mat cv::Mat crop=resz(cv::Rect(it->getCrop1().getX()*it->getScale(), it->getCrop1().getY()*it->getScale(), (it->getCrop2().getX()-it->getCrop1().getX())*it->getScale(), (it->getCrop2().getY()-it->getCrop1().getY())*it->getScale())); //add layer crop.copyTo(combined(cv::Rect(it->getPosition().getX(), it->getPosition().getY(), crop.cols, crop.rows))); //cleanup resz.release(); crop.release(); } //show results ivcombined.refresh(); //everything alright return true; } bool Project::process(){ if(combined.empty()){ return false; } //convert mat to gray cv::Mat graymat; cv::cvtColor(combined, graymat, CV_BGR2GRAY); if(settings->isDebug()){ ImageViewer* iv=new ImageViewer(graymat, "DEBUG:process> cvtColor"); iv->show(); } //blur edges cv::blur(graymat, graymat, cv::Size(3, 3)); if(settings->isDebug()){ ImageViewer* iv=new ImageViewer(graymat, "DEBUG:process> blur"); iv->show(); } //canny edge detection cv::Mat canny; cv::Canny(graymat, canny, cannythresh, cannythresh*2, 3); if(settings->isDebug()){ ImageViewer* iv=new ImageViewer(canny, "DEBUG:process> Canny"); iv->show(); } //find contours cv::findContours(canny, contours, hierarchy, retrmode, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0)); //build color map if not ready if(coloring.size()!=contours.size()){ coloring.clear(); for(int i=0; i maxX){ maxX=contours[i][0].x/10.0; } if(contours[i][0].y/10.0 > maxY){ maxY=contours[i][0].y/10.0; } //move pen down gcode+="M300 S10\n"; lines+=4; //iterate through points for(int o=1; o maxX){ maxX=contours[i][o].x/10.0; } if(contours[i][o].y/10.0 > maxY){ maxY=contours[i][o].y/10.0; } lines++; } //lift up pen gcode+="M300 S50\n"; lines+=1; } //end gcode+="T1000 ;deselect tool\n"; gcode+="M18 ;disable steppers\n"; gcode+="M2 ;program end\n"; lines+=3; //print "final thoughts" lines+=7; gcode+="\n\n\n;Line count: "+std::to_string(lines)+"\n;Max X: "+std::to_string(maxX)+"\n;Max Y: "+std::to_string(maxY)+"\n;ENJOY!"; } bool Project::exportGcode(std::string file){ FILE* f=fopen(file.c_str(), "w"); if(!f){ fprintf(stderr, "Error> Can't create file for gcode export. At: Project::exportGcode(std::string file) (file: %s, line: %i)\n", __FILE__, __LINE__); return false; } fprintf(f, "%s", gcode.c_str()); fclose(f); return true; }