ResultReader.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #pragma once
  2. #include <opencv2/opencv.hpp>
  3. #include <opencv2/xfeatures2d.hpp>
  4. #include "identify_struct.h"
  5. #include "schema_struct.h"
  6. #include <vector>
  7. #include <boost/smart_ptr.hpp>
  8. using namespace std;
  9. using namespace cv;
  10. using namespace cv::ml;
  11. using namespace cv::xfeatures2d;
  12. using namespace identify::schema;
  13. using namespace identify::result;
  14. using namespace identify::analysis;
  15. namespace identify{
  16. class CResultReader
  17. {
  18. public:
  19. CResultReader(void);
  20. ~CResultReader(void);
  21. bool m_nBxuanxiang;
  22. public:
  23. int ReadResult(const IplImage* img, int schemaIndex, cv::Mat transform, double scale, int dir, OMR_RESULT* out_result);
  24. int ReadResult(int shemaIndex);
  25. void SetSchemaPages(boost::shared_ptr<const ISCH_Schema>schema);
  26. private:
  27. //模板
  28. boost::shared_ptr<const ISCH_Schema>schemaPages;
  29. //源图像
  30. const IplImage *src;
  31. int m_pageDirection;
  32. double m_Scaler0;
  33. Mat m_AffineTransform;//防射变换(由模板变换到图像)
  34. AnalyseResult anlyseResult;
  35. int dianshu_meihaomi;
  36. OMR_RESULT* omr_result;
  37. std::vector<cv::Point2f> point_muban;
  38. std::vector<cv::Point2f> point_shijuan;
  39. cv::Ptr<cv::ml::KNearest> knn;
  40. private:
  41. template<typename T1, typename T2 >
  42. double GetDistance(T1 point1, T2 point2)
  43. {
  44. double dx = point1.x - point2.x;
  45. double dy = point1.y - point2.y;
  46. return sqrt(dx*dx + dy*dy);
  47. }
  48. //截取图像
  49. int ClipImg(const ISCH_SCHEMA_PAGE& schemaPage);
  50. /************************************************************************/
  51. /* 设置识别结果默认值 */
  52. /************************************************************************/
  53. void SetDefaultOMR_Result(OMR_RESULT& omr_result);
  54. template<typename T1, typename T2> T2 warpAffinePoint(T1& src, Mat * map_matrix);
  55. template<typename T1> CvRect GetResultRect(T1& item);
  56. template<typename T1> CvRect GetJiaoZhengResultRect(T1& item);
  57. template<typename T1, typename T2> T2 GetJiaoZhengResultPoint(T1& point);
  58. template<class T> void SaveRect(CvRect& rect, T& result);
  59. //读取二维码
  60. int ReadBarCode(const ISCH_SCHEMA_PAGE& schemaPage);
  61. unsigned char(*_GetGray)(const IplImage * dst, int x, int y);
  62. unsigned char GetGray(const IplImage * dst, int x, int y);
  63. //获取全局背景灰度,通过统计所有涂点周围像素(灰度不小于160)的灰度平均值获得
  64. int caculate_global_background(const ISCH_SCHEMA_PAGE& schemaPage);
  65. //计算涂点属性
  66. int caculate_cell_property(ISCH_SCHEMA_PAGE& schemaPage, ISCH_SCHEMA_ITEM &item, const IplImage * dst, int &cellSize, double &refArea, double &avg_gray, int &grayNum, double &mo_hu_du, double &gao_heidu, int &char_area_size, int& char_area_black_count, double& char_area_avg_gray, int & diheidushu);
  67. int caculate_item_property(const ISCH_SCHEMA_PAGE& schemaPage, const ISCH_SCHEMA_ITEM &item, const IplImage * dst, AnalyseResult::itemResult& item_result);
  68. void GetCorrectedRect(CvRect &rr, const IplImage * src, int tembackcolor);
  69. //分析全局属性
  70. int analyseOmrPanoramic(ISCH_SCHEMA_PAGE& schemaPage);
  71. /************************************************************************/
  72. /* 生成OMR涂点字符串(客观题填涂判断) 2014年11月28日10:54:19 */
  73. /************************************************************************/
  74. int GenerateOmrStr(const ISCH_SCHEMA_PAGE& schemaPage);
  75. //读取客观题分数
  76. int ReadKeGuanTi(const ISCH_SCHEMA_PAGE &schemaPage);
  77. int ReadDingWeiDian(const ISCH_SCHEMA_PAGE& schemaPage);
  78. void caculate_item_global_property(const ISCH_SCHEMA_PAGE & schemaPage);
  79. int findAssitLocate(const ISCH_SCHEMA_PAGE& schemaPage);
  80. //读取填空题,和主观题分数
  81. int ReadQuestionScore(const ISCH_SCHEMA_PAGE &schemaPage);
  82. //获取图像指定区域红色点数(主观题) 去除长直线和周边像素 减少直线边缘偏色影响
  83. int GetZhuGuanTiRedCount(IplImage * dst, const CvRect& rect, const CvRect& normal_rect, int * red_counts, int * red_in_counts, int count);
  84. //获取图像指定区域红色点数(填空题)
  85. int GetTianKongTiRedCount(IplImage * dst, const CvRect& rect_normal, const CvRect& rect_detect);
  86. BOOL GetRedBinary(IplImage * dst, const CvRect& rect_detect, IplImage ** red_binary);
  87. };
  88. }