@@ -97,22 +97,34 @@ void DkPageSegmentation::compute() {
9797 if (scale > 0 .8f || scale <= 0 .0f )
9898 scale = 1 .0f ;
9999
100- findRectangles (mImg , mRects );
100+ findRectanglesLab (mImg , mRects );
101101
102102
103103 std::cout << " [DkPageSegmentation] " << mRects .size () << " rectangles found resize factor: " << scale << std::endl;
104104}
105105
106- void DkPageSegmentation::findRectangles (const cv::Mat& img, std::vector<DkPolyRect>& rects) const {
106+ void DkPageSegmentation::findRectanglesLab (const cv::Mat & img, std::vector<DkPolyRect>& rects) const {
107+
107108
108109 cv::Mat imgLab;
109110 cv::cvtColor (img, imgLab, CV_RGB2Lab); // luminance channel is better than grayscale
110111
111- int ch[] = {0 , 0 };
112- cv::Mat imgL (img.size (), CV_8UC1);
113- mixChannels (&imgLab, 1 , &imgL, 1 , ch, 1 );
114- cv::normalize (imgL, imgL, 255 , 0 , cv::NORM_MINMAX);
115- imgLab.release (); // early release
112+ for (int idx = 0 ; idx < img.channels (); idx++) {
113+
114+ int ch[] = {idx, 0 };
115+ cv::Mat imgL (img.size (), CV_8UC1);
116+ cv::mixChannels (&imgLab, 1 , &imgL, 1 , ch, 1 );
117+ std::vector<DkPolyRect> cr;
118+ findRectangles (imgL, rects);
119+ std::cout << rects.size () << " after channel " << idx << std::endl;
120+ }
121+
122+ }
123+
124+ void DkPageSegmentation::findRectangles (const cv::Mat& img, std::vector<DkPolyRect>& rects) const {
125+
126+ cv::Mat imgL;
127+ cv::normalize (img, imgL, 255 , 0 , cv::NORM_MINMAX);
116128
117129 // downscale
118130 if (scale != 1 .0f )
@@ -124,6 +136,7 @@ void DkPageSegmentation::findRectangles(const cv::Mat& img, std::vector<DkPolyRe
124136 // std::cout << "thresh step: " << threshStep << std::endl;
125137
126138 cv::Mat gray;
139+ std::vector<DkPolyRect> rectsL;
127140
128141 // try several threshold levels
129142 for (int thr = 0 ; thr < 255 ; thr += threshStep) {
@@ -166,7 +179,7 @@ void DkPageSegmentation::findRectangles(const cv::Mat& img, std::vector<DkPolyRe
166179 // cv::Mat pImg = imgL.clone();
167180 // cv::cvtColor(pImg, pImg, CV_GRAY2BGR);
168181 // DEBUG ------------------------
169-
182+
170183 // test each contour
171184 for (size_t i = 0 ; i < contours.size (); i++) {
172185 // approxicv::Mate contour with accuracy proportional
@@ -200,7 +213,7 @@ void DkPageSegmentation::findRectangles(const cv::Mat& img, std::vector<DkPolyRe
200213 (!maxSide || cr.maxSide () < maxSide*scale) &&
201214 cr.getMaxCosine () < 0.3 ) {
202215
203- rects .push_back (cr);
216+ rectsL .push_back (cr);
204217 }
205218 }
206219 }
@@ -210,25 +223,23 @@ void DkPageSegmentation::findRectangles(const cv::Mat& img, std::vector<DkPolyRe
210223 // DEBUG ------------------------
211224 }
212225
213- for (size_t idx = 0 ; idx < rects .size (); idx++)
214- rects [idx].scale (1 .0f /scale);
226+ for (size_t idx = 0 ; idx < rectsL .size (); idx++)
227+ rectsL [idx].scale (1 .0f /scale);
215228
216229
217230 // filter rectangles which are found because of the image border
218- std::vector<DkPolyRect> noLargeRects;
219- for (const DkPolyRect& p : rects) {
231+ for (const DkPolyRect& p : rectsL) {
220232
221233 DkBox b = p.getBBox ();
222234
223235 if (b.size ().height < img.rows *maxSideFactor &&
224236 b.size ().width < img.cols *maxSideFactor) {
225- noLargeRects .push_back (p);
237+ rects .push_back (p);
226238 }
227239 }
228240
229241 // cv::normalize(dbgImg, dbgImg, 255, 0, cv::NORM_MINMAX);
230242
231- rects = noLargeRects;
232243}
233244
234245// QImage DkPageSegmentation::cropToRect(const QImage & img, const nmc::DkRotatingRect & rect, const QColor & bgCol) const {
0 commit comments