void sceneTextDetector::set_Channels_Group(Mat image, int mode, int type, string xm, double prob) { //-----------------------------------------------------------------------------CSER /*computeNMChannels(image, channels, mode); int cn = (int)channels.size(); for (int c = 0; c < cn - 1; c++) channels.push_back(255 - channels[c]); for (int c = 0; c<(int)channels.size(); c++) { er_filter1->run(channels[c], regions[c]); er_filter2->run(channels[c], regions[c]); }*/ Mat grey; vector<vector<ERStat> > regions(2); cvtColor(image, grey, COLOR_RGB2GRAY); channels.clear(); channels.push_back(grey); channels.push_back(255 - grey); regions[0].clear(); regions[1].clear(); nm_region_groups.clear(); nm_boxes.clear(); parallel_for_(cv::Range(0, (int)channels.size()), Parallel_extractCSER(channels, regions, er_filters1, er_filters2)); //-----------------------------------------------------------------------------MSER /* vector<vector<Point> > contours; vector<Rect> bboxes; Mat grey; cvtColor(image, grey, COLOR_RGB2GRAY); channels.clear(); channels.push_back(grey); channels.push_back(255 - grey); regions.resize(channels.size()); nm_region_groups.clear(); nm_boxes.clear(); Ptr<MSER> mser = MSER::create(21, (int)(0.00002*grey.cols*grey.rows), (int)(0.05*grey.cols*grey.rows), 1, 0.7); mser->detectRegions(grey, contours, bboxes); if (contours.size() > 0) MSERsToERStats(grey, contours, regions);*/ //----------------------------------------------------------------------------- erGrouping(image, channels, regions, nm_region_groups, nm_boxes, type, xm, prob); }
bool OCRTess::detectAndRecog() { UMat grey = UMat::zeros(this->img.rows + 2, this->img.cols + 2, CV_8UC1); cvtColor(this->img.clone(), grey, COLOR_RGB2GRAY); vector<UMat> channels; channels.clear(); channels.push_back(grey); Mat m = 255 - grey.getMat(ACCESS_READ | ACCESS_WRITE); channels.push_back(m.getUMat(ACCESS_READ)); vector<vector<ERStat>> regions(2); regions[0].clear(); regions[1].clear(); switch (this->REGION) { case REG_CSER: { parallel_for_(Range(0, (int) channels.size()), Parallel_extractCSER(channels, regions, this->erf1, this->erf2)); break; } case REG_MSER: { vector<vector<Point> > contours; vector<Rect> bboxes; Ptr<MSER> mser = MSER::create(21, (int) (0.00002 * grey.cols * grey.rows), (int) (0.05 * grey.cols * grey.rows), 1, 0.7); mser->detectRegions(grey, contours, bboxes); if (contours.size() > 0) MSERsToERStats(grey, contours, regions); break; } default: { break; } } /*Text Recognition (OCR)*/ vector<vector<Vec2i> > nm_region_groups; vector<Rect> nm_boxes; switch (this->GROUP) { case 0: erGrouping(this->img, channels, regions, nm_region_groups, nm_boxes, ERGROUPING_ORIENTATION_HORIZ); break; case 1: default: erGrouping(this->img, channels, regions, nm_region_groups, nm_boxes, ERGROUPING_ORIENTATION_ANY, DIR + TR_GRP, 0.5); break; } if (!nm_boxes.size() || nm_boxes.size() > 1) return false; vector<string> words_detection; float min_confidence1 = 51.f, min_confidence2 = 60.f; vector<UMat> detections; for (int i = 0; i < (int) nm_boxes.size(); i++) { // rectangle(this->out, nm_boxes[i].tl(), nm_boxes[i].br(), Scalar(255, 255, 0), 3); UMat group_img = UMat::zeros(this->img.rows + 2, this->img.cols + 2, CV_8UC1); er_draw(channels, regions, nm_region_groups[i], group_img); group_img = group_img(nm_boxes[i]); copyMakeBorder(group_img.clone(), group_img, 15, 15, 15, 15, BORDER_CONSTANT, Scalar(0)); detections.push_back(group_img); } vector<string> outputs((int) detections.size()); vector<vector<Rect> > boxes((int) detections.size()); vector<vector<string> > words((int) detections.size()); vector<vector<float> > confidences((int) detections.size()); if (!detections.size() || detections.size() > 1) return false; for (int i = 0; i < (int) detections.size(); i = i + this->num) { Range r; if (i + this->num <= (int) detections.size()) r = Range(i, i + this->num); else r = Range(i, (int) detections.size()); parallel_for_(r, Parallel_OCR<OCRTesseract>(detections, outputs, boxes, words, confidences, this->ocrs)); } for (int i = 0; i < (int) detections.size(); i++) { outputs[i].erase(remove(outputs[i].begin(), outputs[i].end(), '\n'), outputs[i].end()); if (outputs[i].size() < 3) { continue; } for (int j = 0; j < (int) boxes[i].size(); j++) { boxes[i][j].x += nm_boxes[i].x - 15; boxes[i][j].y += nm_boxes[i].y - 15; if ((words[i][j].size() < 2) || (confidences[i][j] < min_confidence1) || ((words[i][j].size() == 2) && (words[i][j][0] == words[i][j][1])) || ((words[i][j].size() < 4) && (confidences[i][j] < min_confidence2)) || isRepetitive(words[i][j])) continue; words_detection.push_back(words[i][j]); // rectangle(this->out, boxes[i][j].tl(), boxes[i][j].br(), Scalar(255, 0, 255), 3); // Size word_size = getTextSize(words[i][j], FONT_HERSHEY_SIMPLEX, (double) scale_font, (int) (3 * scale_font), NULL); // rectangle(this->out, boxes[i][j].tl() - Point(3, word_size.height + 3), boxes[i][j].tl() + Point(word_size.width, 0), Scalar(255, 0, 255), -1); // putText(this->out, words[i][j], boxes[i][j].tl() - Point(1, 1), FONT_HERSHEY_SIMPLEX, scale_font, Scalar(255, 255, 255), (int) (3 * scale_font)); } } if (!words_detection.size() || words_detection.size() > 1) return false; return (words_detection[0].compare(WORD) == 0); }