Exemplo n.º 1
0
void OCRer::OCR(Mat img)
{

    cv::waitKey(100);

    _tessapi->SetImage((const unsigned char*) img.data,
                       img.cols,img.rows,
                       img.channels(), img.step);
    _tessapi->Recognize(NULL);

    ResultIterator *ri = _tessapi->GetIterator();
    //return _tessapi->GetUTF8Text();

    
    if (ri != NULL) {

        all_text= "";
        while (ri->Next(RIL_TEXTLINE)) {
        
            all_text += ri->GetUTF8Text(RIL_TEXTLINE);
        }

        delete ri;
        
    }
        
};
Exemplo n.º 2
0
// @param img: grayscaled and in RGB format (e.g. loaded from file).
void OCRer::process_image(Mat img) {
	if (_motion) {
		_n_imgs = 0;
		_motion = false;
	}


	if (_n_imgs == 0) {
		_average_img = img;

	} else {
		// detect motion by comparing with _last_img
		vector<uchar> status;
		vector<float> err;
		calcImageDisplacement(_last_img, img, &status, &err);

		int n = 0;
		int l = status.size();
		for (int i = 0; i < l; i++) {
			bool matched = (bool) status[i];
			if (matched) {
				n++;
			}
		}

		float fn  = (float) n;
		float fnl = (float) _last_n_features;
		float p_motion = fabs( (fn - fnl)  / fnl );          // "percentage" of motion

		_last_n_features = n;
		if (p_motion > MOTION_P_THRESHOLD)
			_motion = true;

		else if (_n_imgs < MAX_N_IMGS) {
			float alpha = 1.0f / (float) (_n_imgs + 1);
			Mat sum = Mat::zeros(img.size(), CV_32F);       // has to be CV_32F or CV_64F

			accumulate(img, sum);
			accumulate(_average_img, sum);
			sum.convertTo(_average_img, _average_img.type(), alpha);
		}
		else {
			return ;
		}
	}
	_last_img = img;
	_n_imgs++;


	_tessapi->SetImage((const unsigned char*) _average_img.data,
	                   _average_img.cols, _average_img.rows,
	                   _average_img.channels(), _average_img.step);
	_tessapi->Recognize(NULL);

	ResultIterator *ri = _tessapi->GetIterator();

	/*
	ChoiceIterator* ci;
	if (ri != NULL) {
		while ((ri->Next(RIL_SYMBOL))) {
			const char* symbol = ri->GetUTF8Text(RIL_SYMBOL);

			if (symbol != 0) {
				float conf = ri->Confidence(RIL_SYMBOL);
				std::cout << "\tnext symbol: " << symbol << "\tconf: " << conf << endl;

				const ResultIterator itr = *ri;
				ci = new ChoiceIterator(itr);

				do {
					std::cout << "\t\t" << ci->GetUTF8Text() << " conf: " << ci->Confidence() << endl;
				} while(ci->Next());

				delete ci;
			}

			delete[] symbol;
		}
	}
	*/

	if (ri != NULL) {
		// int l, t, r, b;
		while (ri->Next(RIL_WORD)) {
			// ri->BoundingBox(RIL_WORD, &l, &t, &r, &b);
			// cout << "rect = " << l << ", " << r << ", " << t << ", " << b << endl;

			char *ocr_text = ri->GetUTF8Text(RIL_WORD);

			if (heuristic(ocr_text)) {
				if (ri->WordIsFromDictionary()) {
					_dict_words.insert(string(ocr_text));
					// cout << "conf = " << ri->Confidence(RIL_WORD) << endl;

				} else {
					_live_words.insert(string(ocr_text));
				}
			}

			delete[] ocr_text;
		}

		delete ri;
	}
}