123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- #include "StdAfx.h"
- #include "..\Schema\schema_struct.h"
- #include <opencv2\opencv.hpp>
- #include "LocateTester.h"
- #include "..\Identifier\schema_struct.h"
- #include "common.h"
- using namespace cv;
- using namespace schema;
- CLocateTester::CLocateTester(void) :m_isReady(true), m_isResultOk(false), _schema(new ISCH_Schema())
- {
- }
- CLocateTester::~CLocateTester(void)
- {
- }
- /*
- 初始化模板
- */
- void CLocateTester::SetPageDefault(ISCH_SCHEMA_PAGE& page)
- {
- //page.group_spacer =';';
- page.hei_du_ling_min_du =4;
- page.height =0;
- page.index =0;
- page.option_spacer =',';
- //page.unselect_char='*';
- page.width=0;
- }
- void CLocateTester::loadSchema(const schema::SCHEMA& schema, const std::vector<std::string>& img_paths, std::vector<ISCH_SCHEMA_PAGE>& _schema)
- {
- const std::vector<schema::SCHEMA_QUESTION> &questions =schema.questions;
- _schema.resize(schema.pages.size());
- for (int i=0;i<schema.pages.size();i++)
- {
- const SCHEMA_PAGE& schema_page=schema.pages[i];
- /************************************************************************/
- /* 整理模板到结果中 */
- /************************************************************************/
- ISCH_SCHEMA_PAGE* page = &_schema[i];
- SetPageDefault(*page);
- page->index = i;
- page->width =schema_page.width;
- page->height =schema_page.height;
- page->locatePoints.resize(schema_page.locatePoints.size());
- for (int idx_i = 0; idx_i < schema_page.locatePoints.size(); idx_i++)
- {
- identify::schema::ISCH_SCHEMA_LOCATE_POINT& dst = page->locatePoints[idx_i];
- const schema::SCHEMA_LOCATE_POINT& src = schema_page.locatePoints[idx_i];
- dst.centerx = src.centerx;
- dst.centery = src.centery;
- dst.width = src.width;
- dst.height = src.height;
- dst.nID = src.nID;
- }
- page->locateCrosses.resize(schema_page.locateCrosses.size());
- for (int idx_i = 0; idx_i < schema_page.locateCrosses.size(); idx_i++)
- {
- identify::schema::ISCH_SCHEMA_LOACTE_CROSS& dst = page->locateCrosses[idx_i];
- const schema::SCHEMA_LOACTE_CROSS& src = schema_page.locateCrosses[idx_i];
- dst.centerx = src.centerx;
- dst.centery = src.centery;
- dst.width = src.width;
- dst.height = src.height;
- dst.nID = src.nID;
- dst.angle = src.angle;
- dst.sign = src.sign;
- }
- page->codes.resize(schema_page.codes.size());
- for (int idx_i = 0; idx_i < schema_page.codes.size(); idx_i++)
- {
- identify::schema::ISCH_SCHEMA_CODE& dst = page->codes[idx_i];
- const schema::SCHEMA_CODE& src = schema_page.codes[idx_i];
- dst.centerx = src.centerx;
- dst.centery = src.centery;
- dst.width = src.width;
- dst.height = src.height;
- dst.nID = src.nID;
- dst.bDirection = (identify::schema::ISCH_DIRECTION)(int)src.bDirection;
- }
- page->cutAreas.resize(schema_page.clips.size());
- for (int idx_i = 0; idx_i < schema_page.clips.size(); idx_i++)
- {
- identify::schema::ISCH_SCHEMA_CLIP& dst = page->cutAreas[idx_i];
- const schema::SCHEMA_CLIP& src = schema_page.clips[idx_i];
- dst.centerx = src.centerx;
- dst.centery = src.centery;
- dst.width = src.width;
- dst.height = src.height;
- dst.nID = src.nID;
- dst.area_name = src.area_name;
- dst.markUnit = src.markUnit;
- dst.markUnitPart = src.markUnitPart;
- }
- Mat img = imread(img_paths[i],CV_LOAD_IMAGE_GRAYSCALE);
- double angle =schema_page.angle;
- double a = fmod(fmod(angle,360)+360,360);
- int nWidth,nHeight;
- if((a>45&&a<=135)||(a>45+180&&a<=135+180)){
- nHeight = img.cols;
- nWidth = img.rows;
- }else{
- nWidth = img.cols;
- nHeight = img.rows;
- }
- page->width = nWidth;
- page->height = nHeight;
- double sina = sin(CV_PI*angle/180);
- double cosa = cos(CV_PI*angle/180);
- CvPoint2D32f center0 = cvPoint2D32f(img.cols/2.0,img.rows/2.0);
- CvPoint2D32f center1 = cvPoint2D32f(nWidth/2.0,nHeight/2.0);
- float offsetx = center1.x - (cosa*center0.x+sina*center0.y);
- float offsety = center1.y - (-sina*center0.x+cosa*center0.y);
- float data[6]={cosa,sina,offsetx,-sina,cosa,offsety};
- Mat m(2,3,CV_32F,data);
- Mat img_rotated;
- warpAffine(img,img_rotated,m,cvSize(nWidth,nHeight));
- img.release();
- IplImage * img_smoothd = &IplImage(img_rotated);
- cvSmooth(img_smoothd, img_smoothd, CV_GAUSSIAN, 5);
- caculateLocateArea(img_rotated,schema_page.locateAreas,page->locateAreas);
- }
- }
- ServiceState CLocateTester::OnRunning( void )
- {
- const int rotation[4]={ROTATION_0,ROTATION_90,ROTATION_180,ROTATION_270};
- m_matchtask.resize(imgs.size()*_schema->size()*4);
- for (int idx_img=0,idx_task=0;idx_img<imgs.size();idx_img++){
- for (int idx_schema=0;idx_schema<_schema->size();idx_schema++)
- {
- for(int idx_dir=0;idx_dir<4;idx_dir++){
- for(bool disptch_failture=true;disptch_failture;){
- for (int idx_worker=0;disptch_failture&&idx_worker<WORKER_COUNT;idx_worker++)
- {
- if(m_worker[idx_worker].isBusy())continue;
- CTestMatchTask match_task(imgs[idx_img],&matcher[idx_worker],idx_schema,rotation[idx_dir]);
- m_matchtask[idx_task] =match_task;
- m_worker[idx_worker].addTask(&m_matchtask[idx_task]);
- disptch_failture=false;
- idx_task++;
- }
- if(disptch_failture){Sleep(1);}
- }
- }
- }
- }
- for (;;){
- bool someone_busy =false;
- for (int idx_worker=0;idx_worker<WORKER_COUNT;idx_worker++)
- {
- if(m_worker[idx_worker].isBusy()){someone_busy =true;break;}
- }
- if(someone_busy){Sleep(10);}else{break;}
- }
- bool isOk =true;
- std::string msg;
- for (int idx_img=0,idx_task=0,error_page_count=0;idx_img<imgs.size();idx_img++){
- int schema_match_task_idx =-1;
- int schema_match_schema_idx =-1;
- bool schema_mutil_dir_matched=false ;
- bool schema_mutil_page_matched=false;
- for (int idx_schema=0;idx_schema<_schema->size();idx_schema++){
- int dir_match_task_idx=-1;
- bool dir_mutil_dir_matched=false;
- for(int idx_dir=0;idx_dir<4;idx_dir++,idx_task++){
- if(m_matchtask[idx_task].result.matched){
- if(dir_match_task_idx<0||m_matchtask[idx_task].result.matched_count>m_matchtask[dir_match_task_idx].result.matched_count){
- dir_match_task_idx =idx_task;
- }
- if(dir_match_task_idx>=0&&dir_match_task_idx!=idx_task&&m_matchtask[idx_task].result.matched_count==m_matchtask[dir_match_task_idx].result.matched_count){
- dir_mutil_dir_matched=true;
- }
- }
- }
- if(dir_match_task_idx>=0){
- if(schema_match_schema_idx<0){
- schema_match_task_idx =dir_match_task_idx;
- schema_match_schema_idx = idx_schema;
- schema_mutil_dir_matched =dir_mutil_dir_matched;
- schema_mutil_page_matched =false;
- }else{
- int gailv1 =m_matchtask[schema_match_task_idx].result.matched_count*2-m_matchtask[schema_match_task_idx].result.total_count;
- int gailv2 =m_matchtask[dir_match_task_idx].result.matched_count*2-m_matchtask[dir_match_task_idx].result.total_count;
- if(gailv1<gailv2){
- schema_match_task_idx=dir_match_task_idx;
- schema_match_schema_idx=idx_schema;
- schema_mutil_dir_matched =dir_mutil_dir_matched;
- schema_mutil_page_matched =false;
- }else if(gailv1==gailv2){
- schema_mutil_page_matched= true;
- }
- }
- }
- }
- char msg0[512];
- if(idx_img==schema_match_schema_idx&&!schema_mutil_page_matched&&!schema_mutil_dir_matched){
- sprintf_s(msg0,"第%d页正常识别",idx_img+1);
- }else{
- if(isOk)isOk =false;
- if(schema_match_schema_idx<0){sprintf_s(msg0,"第%d页无法正确对应模板进行匹配,请重新设置定位点",idx_img+1);}
- else if(idx_img!=schema_match_schema_idx){sprintf_s(msg0,"第%d页无法匹配上对应页的模板,请重新设置定位点",idx_img+1);}
- else if(schema_mutil_page_matched&&schema_mutil_dir_matched)sprintf_s(msg0,"第%d页可以与多页的多个方向进行匹配,请重新设置定位点",idx_img+1);
- else if(schema_mutil_page_matched)sprintf_s(msg0,"第%d页可以与多页进行匹配,请重新设置定位点",idx_img+1);
- else if(schema_mutil_dir_matched)sprintf_s(msg0,"第%d页可以多个方向进行匹配,请重新设置定位点",idx_img+1);
- if(error_page_count>0)msg.append(",");
- msg.append(msg0);
- }
- }
- m_isOk =isOk;
- m_error_msg=msg;
- m_isResultOk =true;
- return ServiceState::stoping;
- }
- ServiceState CLocateTester::OnStarting( void )
- {
- for (int idx_worker=0;idx_worker<WORKER_COUNT;idx_worker++)
- {
- if(m_worker[idx_worker].GetServiceSate()==ServiceState::stoped){
- m_worker[idx_worker].Start();
- }
- }
- bool all_running;
- do
- {
- all_running=true;
- for (int idx_worker=0;idx_worker<WORKER_COUNT;idx_worker++)
- {
- if(m_worker[idx_worker].GetServiceSate()!=ServiceState::running){
- all_running=false;
- }
- }
- if(!all_running){Sleep(10);}
- } while (!all_running);
- return ServiceState::running;
- }
- bool CLocateTester::test( const schema::SCHEMA& schema,const std::vector<std::string>& img_paths )
- {
- if(isSameLocateSchema(schema))return true;
- m_isReady = false;
- m_isResultOk = false;
- last_test_schema = schema;
- for (int i=0;i<img_paths.size();i++)
- {
- imgs.push_back(imread(img_paths[i],CV_LOAD_IMAGE_GRAYSCALE));
- }
- loadSchema(last_test_schema,img_paths,*_schema);
- for(int idx_i=0;idx_i<WORKER_COUNT;idx_i++){
- matcher[idx_i].SetSchemaPages(_schema);
- }
- Start();
- return true;
- }
- bool CLocateTester::isSameLocateSchema( const schema::SCHEMA& schema )
- {
- bool isSame =true;
- if(schema.pages.size() == last_test_schema.pages.size()){
- for (int idx_page =0;isSame&&idx_page <schema.pages.size();idx_page++)
- {
- const SCHEMA_PAGE& pages1 =schema.pages[idx_page];
- const SCHEMA_PAGE& pages0 =last_test_schema.pages[idx_page];
- if(pages1.nID == pages0.nID&&pages1.angle == pages0.angle
- &&pages1.locatePoints.size()==pages0.locatePoints.size()
- &&pages1.locateAreas.size()==pages0.locateAreas.size()
- &&pages1.locateCrosses.size()==pages0.locateCrosses.size()){
- for (int idx_l=0;isSame&&idx_l<pages0.locatePoints.size();idx_l++)
- {
- if(pages0.locatePoints[idx_l].centerx!=pages1.locatePoints[idx_l].centerx
- ||pages0.locatePoints[idx_l].centery!=pages1.locatePoints[idx_l].centery
- ||pages0.locatePoints[idx_l].width!=pages1.locatePoints[idx_l].width
- ||pages0.locatePoints[idx_l].height!=pages1.locatePoints[idx_l].height){
- isSame=false;
- break;
- }
- }
- for (int idx_l=0;isSame&&idx_l<pages0.locateAreas.size();idx_l++)
- {
- if(pages0.locateAreas[idx_l].centerx!=pages1.locateAreas[idx_l].centerx
- ||pages0.locateAreas[idx_l].centery!=pages1.locateAreas[idx_l].centery
- ||pages0.locateAreas[idx_l].width!=pages1.locateAreas[idx_l].width
- ||pages0.locateAreas[idx_l].height!=pages1.locateAreas[idx_l].height){
- isSame=false;
- break;
- }
- }
- for (int idx_l=0;isSame&&idx_l<pages0.locateCrosses.size();idx_l++)
- {
- if(pages0.locateCrosses[idx_l].centerx!=pages1.locateCrosses[idx_l].centerx
- ||pages0.locateCrosses[idx_l].centery!=pages1.locateCrosses[idx_l].centery
- ||pages0.locateCrosses[idx_l].width!=pages1.locateCrosses[idx_l].width
- ||pages0.locateCrosses[idx_l].height!=pages1.locateCrosses[idx_l].height){
- isSame=false;
- break;
- }
- }
- }else{
- isSame=false;
- break;
- }
- }
- }else{
- isSame=false;
- }
- return isSame;
- }
- ServiceState CLocateTester::OnStoping( void )
- {
- _schema->clear();
- for (int idx_worker=0;idx_worker<WORKER_COUNT;idx_worker++)
- {
- if(m_worker[idx_worker].GetServiceSate()!=ServiceState::stoped){
- m_worker[idx_worker].Stop();
- }
- }
- imgs.clear();
- m_isReady = true;
- return IService::OnStoping();
- }
- bool CLocateTester::isReady()
- {
- return m_isReady;
- }
|