// cropper.cpp // SlamView // // Created by Mosong Cheng on 4/30/14. // Copyright (c) 2014 Mosong Cheng. All rights reserved. // #include "cropper.h" using namespace std; using namespace cv; /** find the max ROI in which all pixels are not blank, and ** then crop the image using ROI **/ Mat Cropper::crop(const cv::Mat &img){ if(img.rows==0 || img.cols==0)return img; Mat f; crop(img, f, finalROI); return f; } void Cropper::crop(const cv::Mat &src, cv::Mat &dst, cv::Rect &dstBound) { if (src.rows==0 || src.cols==0){ dst = src.clone(); dstBound = cv::Rect(0,0,0,0); return; } // -- Step 0: get mask getMask(src); rect = cv::Rect(0, 0, src.cols, src.rows); // -- Step 1: rough frame framing(); Mat f = Mat(src, rect).clone(); finalROI = rect; rect = cv::Rect(0, 0, f.cols, f.rows); //imwrite(SAT::strCombined(debugPath, "zbw20.jpg"), mask*200); // -- Step 3: start from the center of the image cv::Rect roi; Point2i pt(rect.width/2, rect.height/2); roi = cv::Rect(pt.x, pt.y, 1, 1); expandROI(roi); cout<<"roi = "<=1){ p1 = r.tl() + stp; rp = cv::Rect(p1, p2); if(!rectHasBlankPixels(rp)){ r = rp; canExpand = true; break; } stp.x /= 2; stp.y /=2; } stp = (rect.br() - r.br()); stp.x /=2; stp.y /=2; p1 = r.tl(); while(abs(stp.x) + abs(stp.y)>=1){ p2 = r.br() + stp; rp = cv::Rect(p1, p2); if(!rectHasBlankPixels(rp)){ r = rp; canExpand = true; break; } stp.x /=2; stp.y/=2; } } } /** determine if a line contains any "blank" piexls. * This line is either horizontal or vertical **/ bool Cropper::lineHasBlankPixels(const Point2i &st, const Point2i &ed) { bool verticalLine = (st.x == ed.x); bool horizontalLine = (st.y == ed.y); //cout<<"vertical = "<(pt)>0){ return true; } } return false; } // -- horizontalLine if (ed.x < st.x) kstep = -1; for(pt.x=st.x; pt.x<=ed.x; pt.x += kstep){ if (mask.at(pt)>0){ return true; } } return false; } /** search until no more blank line **/ void Cropper::searchContentLine(Point2i &p1, Point2i &p2, Point2i &delta) { Point2i p3 = p1, p4 = p2; if(lineHasContent(p1, p2))return; // p1-p2 is the content line int stp = kstep; while(stp>=1){ p3 = p1 + delta*stp; p4 = p2 + delta*stp; if(!p3.inside(rect)){ stp /= 2; continue; } if(lineHasContent(p3, p4)){ stp /= 2; continue; } p1 = p3; p2 = p4; } p1 = p1 + delta; p2 = p2 + delta; }