NumberDectect.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. #include "NumberDectect.h"
  2. #include "DoublePointerCountALGO.h"
  3. #include "AtmosphericPressureALGO.h"
  4. #include "CircularArresterCurrentALGO.h"
  5. #include "ArresterCircularZeroThreeALGO.h"
  6. using namespace std;
  7. cv::Mat NumberDectect::DoPerspectiveTransform(cv::Mat& SrcImg)
  8. {
  9. bool isShowWindow = false;
  10. vector<pair<cv::Scalar, int>> sideScalarCount;
  11. Mat src = SrcImg;
  12. for (int row = 0; row < src.rows; row++) {
  13. uchar* uc_pixel = src.data + row * src.step;
  14. for (int col = 0; col < src.cols; col++) {
  15. /*uc_pixel[0] = 255 - uc_pixel[0];
  16. uc_pixel[1] = 255 - uc_pixel[1];
  17. uc_pixel[2] = 255 - uc_pixel[2];*/
  18. if (row == src.rows - 1 || row == 0)
  19. {
  20. //sideScalarCount.push_back(pair())
  21. cv::Scalar sc(uc_pixel[0], uc_pixel[1], uc_pixel[2]);
  22. auto item = std::find_if(sideScalarCount.begin(), sideScalarCount.end(), [sc](pair<cv::Scalar, int> p) { return p.first == sc; });
  23. if (item != sideScalarCount.end())
  24. {
  25. item->second++;
  26. }
  27. else {
  28. sideScalarCount.push_back(pair<cv::Scalar, int>(sc, 0));
  29. }
  30. }
  31. else
  32. {
  33. if (col == src.cols - 1 || col == 0)
  34. {
  35. cv::Scalar sc(uc_pixel[0], uc_pixel[1], uc_pixel[2]);
  36. auto item = std::find_if(sideScalarCount.begin(), sideScalarCount.end(), [sc](pair<cv::Scalar, int> p) { return p.first == sc; });
  37. if (item != sideScalarCount.end())
  38. {
  39. item->second++;
  40. }
  41. else {
  42. sideScalarCount.push_back(pair<cv::Scalar, int>(sc, 0));
  43. }
  44. }
  45. }
  46. uc_pixel += 3;
  47. }
  48. }
  49. int maxCount = 0;
  50. cv::Scalar maxSc;
  51. for (int i = 0; i < sideScalarCount.size(); i++) {
  52. if (sideScalarCount[i].second > maxCount)
  53. {
  54. maxCount = sideScalarCount[i].second;
  55. maxSc = sideScalarCount[i].first;
  56. }
  57. }
  58. int matCols = 500;
  59. Size size(500, 420);
  60. resize(src, src, size, (float)matCols / src.cols, (float)matCols / src.cols);
  61. if (isShowWindow) if (isShowWindow) cv::imshow("input image", src);
  62. cv::Point p(50, 50);
  63. Mat largeImage(Size(600, 520), CV_8UC3, maxSc);// Mat(Size(520, 440), , CV_8UC3);
  64. Mat imageROI;
  65. imageROI = largeImage(Rect(p.x, p.y, src.cols, src.rows));
  66. src.copyTo(imageROI);
  67. if (isShowWindow) cv::imshow("input image imageROI", largeImage);
  68. src = largeImage;
  69. //bgr 2 gray 转为灰度图像
  70. Mat src_gray;
  71. cvtColor(src, src_gray, COLOR_BGR2GRAY);
  72. //binary 二值化
  73. Mat binary;
  74. threshold(src_gray, binary, 0, 255, THRESH_BINARY_INV | THRESH_OTSU); //THRESH_BINARY_INV二值化后取反
  75. //imshow("binary", binary);//因为有一些斑点存在
  76. //形态学 闭操作:可以填充小的区域
  77. Mat morhp_img;
  78. Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5), Point(-1, -1));
  79. morphologyEx(binary, morhp_img, MORPH_CLOSE, kernel, Point(-1, -1), 3);
  80. //imshow("morphology", morhp_img);
  81. Mat dst;
  82. bitwise_not(morhp_img, dst);//在取反
  83. if (isShowWindow) cv::imshow("dst", dst);//
  84. //轮廓发现
  85. vector<vector<Point>> contous;
  86. vector<Vec4i> hireachy;
  87. findContours(dst, contous, hireachy, CV_RETR_TREE, CHAIN_APPROX_SIMPLE, Point());
  88. std::cout << "contous.size:" << contous.size() << endl;
  89. //轮廓绘制
  90. int width = src.cols;
  91. int height = src.rows;
  92. Mat drawImage = Mat::zeros(src.size(), CV_8UC3);
  93. std::cout << contous.size() << endl;
  94. for (size_t t = 0; t < contous.size(); t++)
  95. {
  96. Rect rect = boundingRect(contous[t]);
  97. if (rect.width > width / 2 && rect.height > height / 2 && rect.width < width - 5 && rect.height < height - 5)
  98. {
  99. drawContours(drawImage, contous, static_cast<int>(t), Scalar(0, 0, 255), 2, 8, hireachy, 0, Point(0, 0));
  100. }
  101. }
  102. if (isShowWindow) cv::imshow("contours", drawImage);//显示找到的轮廓
  103. //直线检测
  104. vector<Vec4i> lines;
  105. Mat contoursImg;
  106. int accu = min(width * 0.2, height * 0.2);
  107. cvtColor(drawImage, contoursImg, COLOR_BGR2GRAY);
  108. if (isShowWindow) cv::imshow("contours", contoursImg);
  109. Mat linesImage = Mat::zeros(src.size(), CV_8UC3);
  110. HoughLinesP(contoursImg, lines, 2, CV_PI / 180.0, accu, accu, 3);
  111. for (size_t t = 0; t < lines.size(); t++)
  112. {
  113. Vec4i ln = lines[t];
  114. line(linesImage, Point(ln[0], ln[1]), Point(ln[2], ln[3]), Scalar(0, 0, 255), 2, 8, 0);//绘制直线
  115. }
  116. std::cout << "number of lines:" << lines.size() << endl;
  117. if (isShowWindow) cv::imshow("linesImages", linesImage);
  118. //寻找与定位上下 左右 四条直线
  119. int deltah = 0; //高度差
  120. int deltaw = 0; //宽度差
  121. Vec4i topLine, bottomLine; //直线定义
  122. Vec4i rightLine, leftLine;
  123. for (int i = 0; i < lines.size(); i++)
  124. {
  125. Vec4i ln = lines[i];//?????
  126. /*
  127. Opencv中的累计霍夫变换HoughLinesP(),输出的是一个Vector of Vec4i,
  128. Vector每个元素代表一条直线,是由一个4元浮点数构成,
  129. 前两个一组x_1,y_1,后两个一组x_2,y_2,代表了图像中直线的起始点和结束点。
  130. */
  131. deltah = abs(ln[3] - ln[1]); //计算高度差(y2-y1)
  132. //topLine
  133. if (ln[3] < height / 2.0 && ln[1] < height / 2.0 && deltah < accu - 1)
  134. {
  135. topLine = lines[i];
  136. }
  137. //bottomLine
  138. if (ln[3] > height / 2.0 && ln[1] > height / 2.0 && deltah < accu - 1)
  139. {
  140. bottomLine = lines[i];
  141. }
  142. deltaw = abs(ln[2] - ln[0]); //计算宽度差(x2-x1)
  143. //leftLine
  144. if (ln[0] < height / 2.0 && ln[2] < height / 2.0 && deltaw < accu - 1)
  145. {
  146. leftLine = lines[i];
  147. }
  148. //rightLine
  149. if (ln[0] > width / 2.0 && ln[2] > width / 2.0 && deltaw < accu - 1)
  150. {
  151. rightLine = lines[i];
  152. }
  153. }
  154. // 打印四条线的坐标
  155. std::cout << "topLine : p1(x,y)= " << topLine[0] << "," << topLine[1] << "; p2(x,y)= " << topLine[2] << "," << topLine[3] << endl;
  156. std::cout << "bottomLine : p1(x,y)= " << bottomLine[0] << "," << bottomLine[1] << "; p2(x,y)= " << bottomLine[2] << "," << bottomLine[3] << endl;
  157. std::cout << "leftLine : p1(x,y)= " << leftLine[0] << "," << leftLine[1] << "; p2(x,y)= " << leftLine[2] << "," << leftLine[3] << endl;
  158. std::cout << "rightLine : p1(x,y)= " << rightLine[0] << "," << rightLine[1] << "; p2(x,y)= " << rightLine[2] << "," << rightLine[3] << endl;
  159. //拟合四条直线
  160. float k1, k2, k3, k4, c1, c2, c3, c4;
  161. k1 = float(topLine[3] - topLine[1]) / float(topLine[2] - topLine[0]);
  162. c1 = topLine[1] - k1 * topLine[0];
  163. k2 = float(bottomLine[3] - bottomLine[1]) / float(bottomLine[2] - bottomLine[0]);
  164. c2 = bottomLine[1] - k2 * bottomLine[0];
  165. k3 = float(leftLine[3] - leftLine[1]) / float(leftLine[2] - leftLine[0]);
  166. c3 = leftLine[1] - k3 * leftLine[0];
  167. k4 = float(rightLine[3] - rightLine[1]) / float(rightLine[2] - rightLine[0]);
  168. c4 = rightLine[1] - k4 * rightLine[0];
  169. //求四个角点,
  170. Point p1;//topLine leftLine 左上角
  171. p1.x = static_cast<int>(c1 - c3) / k3 - k1;
  172. p1.y = k1 * p1.x + c1;
  173. Point p2;//topLine rightLine 右上角
  174. p2.x = static_cast<int>(c1 - c4) / k4 - k1;
  175. p2.y = k1 * p2.x + c1;
  176. Point p3;//bottomLine leftLine 左下角
  177. p3.x = static_cast<int>(c2 - c3) / k3 - k2;
  178. p3.y = k2 * p3.x + c2;
  179. Point p4;//bottomLine rightLine 右下角
  180. p4.x = static_cast<int>(c2 - c4) / k4 - k2;
  181. p4.y = k2 * p4.x + c2;
  182. std::cout << "Point p1: (" << p1.x << "," << p1.y << ")" << endl;
  183. std::cout << "Point p2: (" << p2.x << "," << p2.y << ")" << endl;
  184. std::cout << "Point p3: (" << p3.x << "," << p3.y << ")" << endl;
  185. std::cout << "Point p4: (" << p4.x << "," << p4.y << ")" << endl;
  186. if (p1.x > 0 && p1.y > 0 && p2.x > 0 && p2.y > 0 && p3.x > 0 && p3.y > 0 && p4.x > 0 && p4.y > 0)
  187. {
  188. if (p1.x < width && p2.x < width && p3.x < width && p4.x < width)
  189. {
  190. if (p1.y < height && p2.y < height && p3.y < height && p4.y < height)
  191. {
  192. //显示四个点
  193. cv::circle(linesImage, p1, 2, Scalar(0, 255, 0), 2);
  194. cv::circle(linesImage, p2, 2, Scalar(0, 255, 0), 2);
  195. cv::circle(linesImage, p3, 2, Scalar(0, 255, 0), 2);
  196. cv::circle(linesImage, p4, 2, Scalar(0, 255, 0), 2);
  197. if (isShowWindow) cv::imshow("find four points", linesImage);
  198. //透视变换
  199. vector<Point2f> src_corners(4);
  200. src_corners[0] = p1;
  201. src_corners[1] = p2;
  202. src_corners[2] = p3;
  203. src_corners[3] = p4;
  204. Mat result_images = Mat::zeros(height * 0.7, width * 0.9, CV_8UC3);
  205. vector<Point2f> dst_corners(4);
  206. dst_corners[0] = Point(0, 0);
  207. dst_corners[1] = Point(result_images.cols, 0);
  208. dst_corners[2] = Point(0, result_images.rows);
  209. dst_corners[3] = Point(result_images.cols, result_images.rows);
  210. Mat warpmatrix = getPerspectiveTransform(src_corners, dst_corners); //获取透视变换矩阵
  211. //imshow("final result warpmatrix", warpmatrix);
  212. cv::warpPerspective(src, result_images, warpmatrix, result_images.size()); //透视变换
  213. if (isShowWindow) cv::imshow("final result", result_images);
  214. //imwrite(imgpath, result_images);
  215. if (isShowWindow) cv::waitKey(5000);
  216. return result_images;
  217. }
  218. }
  219. }
  220. return Mat();
  221. }
  222. bool NumberDectect::Init(bool isCuda)
  223. {
  224. string model_path = "models/number-sim.onnx";
  225. try {
  226. net = cv::dnn::readNet(model_path);
  227. if (isCuda) {
  228. net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
  229. net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA_FP16);
  230. }
  231. //cpu
  232. else {
  233. net.setPreferableBackend(cv::dnn::DNN_BACKEND_DEFAULT);
  234. net.setPreferableTarget(cv::dnn::DNN_TARGET_CPU);
  235. }
  236. }
  237. catch (const std::exception& ex)
  238. {
  239. YunDaISASImageRecognitionService::ConsoleLog(ex.what());
  240. return false;
  241. }
  242. //cuda
  243. return true;
  244. return false;
  245. }
  246. IDetection::DectectResult NumberDectect::GetStateResult(cv::Mat img, cv::Rect rec)
  247. {
  248. return resultValue;
  249. }
  250. IDetection::DectectResult NumberDectect::GetDigitResult(cv::Mat img, cv::Rect rec)
  251. {
  252. //resultValue.clear();
  253. //std::cout << "test" << std::endl;
  254. //try
  255. //{
  256. // cv::Mat ROI = img(rec);
  257. // /*imwrite("test.png", ROI);
  258. // YunDaISASImageRecognitionService::SetImage(QString::fromStdString("test.png"));*/
  259. // Detect(ROI);
  260. //}
  261. //catch (const std::exception& ex)
  262. //{
  263. // YunDaISASImageRecognitionService::ConsoleLog(ex.what());
  264. //}
  265. //if (resultValue.m_confidence < 0.1)
  266. //{
  267. // resultValue = DectectResult(0.45, 0, "");
  268. //}
  269. return resultValue;
  270. }
  271. vector<IDetection::DectectResult> NumberDectect::GetDigitResults()
  272. {
  273. return this->resultValues;
  274. }
  275. bool NumberDectect::Detect(cv::Mat& SrcImg)
  276. {
  277. auto pecImg = DoPerspectiveTransform(SrcImg);
  278. if (pecImg.rows>0&& pecImg.cols>0 )
  279. {
  280. SrcImg = pecImg;
  281. }
  282. cv::Mat blob;
  283. int col = SrcImg.cols;
  284. int row = SrcImg.rows;
  285. int maxLen = MAX(col, row);
  286. cv::Mat netInputImg = SrcImg.clone();
  287. if (maxLen > 1.2 * col || maxLen > 1.2 * row) {
  288. cv::Mat resizeImg = cv::Mat::zeros(maxLen, maxLen, CV_8UC3);
  289. SrcImg.copyTo(resizeImg(cv::Rect(0, 0, col, row)));
  290. netInputImg = resizeImg;
  291. }
  292. cv::dnn::blobFromImage(netInputImg, blob, 1 / 255.0, cv::Size(netWidth, netHeight), cv::Scalar(0, 0, 0), true, false);
  293. net.setInput(blob);
  294. std::vector<cv::Mat> netOutputImg;
  295. net.forward(netOutputImg, net.getUnconnectedOutLayersNames());
  296. std::vector<int> classIds;//结果id数组
  297. std::vector<float> confidences;//结果每个id对应置信度数组
  298. std::vector<cv::Rect> boxes;//每个id矩形框
  299. float ratio_h = (float)netInputImg.rows / netHeight;
  300. float ratio_w = (float)netInputImg.cols / netWidth;
  301. int net_width = className.size() + 5; //输出的网络宽度是类别数+5
  302. float* pdata = (float*)netOutputImg[0].data;
  303. for (int stride = 0; stride < strideSize; stride++) { //stride
  304. int grid_x = (int)(netWidth / netStride[stride]);
  305. int grid_y = (int)(netHeight / netStride[stride]);
  306. for (int anchor = 0; anchor < 3; anchor++) { //anchors
  307. const float anchor_w = netAnchors[stride][anchor * 2];
  308. const float anchor_h = netAnchors[stride][anchor * 2 + 1];
  309. for (int i = 0; i < grid_y; i++) {
  310. for (int j = 0; j < grid_x; j++) {
  311. float box_score = pdata[4]; ;//获取每一行的box框中含有某个物体的概率
  312. if (box_score >= boxThreshold) {
  313. cv::Mat scores(1, className.size(), CV_32FC1, pdata + 5);
  314. cv::Point classIdPoint;
  315. double max_class_socre;
  316. minMaxLoc(scores, 0, &max_class_socre, 0, &classIdPoint);
  317. max_class_socre = (float)max_class_socre;
  318. if (max_class_socre >= classThreshold)
  319. {
  320. //rect [x,y,w,h]
  321. float x = pdata[0]; //x
  322. float y = pdata[1]; //y
  323. float w = pdata[2]; //w
  324. float h = pdata[3]; //h
  325. int left = (x - 0.5 * w) * ratio_w;
  326. int top = (y - 0.5 * h) * ratio_h;
  327. left = left < 0 ? 0 : left;
  328. top = top < 0 ? 0 : top;
  329. int widthBox = int(w * ratio_w);
  330. int heightBox = int(h * ratio_h);
  331. widthBox = widthBox > col ? col : widthBox;
  332. heightBox = heightBox > row ? row : heightBox;
  333. if (left < 0 || left>col || top < 0 || top>row || widthBox > col || heightBox > row
  334. || left + widthBox > col || top + heightBox > row
  335. )
  336. {
  337. continue;
  338. }
  339. classIds.push_back(classIdPoint.x);
  340. confidences.push_back(max_class_socre * box_score);
  341. boxes.push_back(cv::Rect(left, top, widthBox, heightBox));
  342. }
  343. }
  344. pdata += net_width;//下一行
  345. }
  346. }
  347. }
  348. }
  349. //执行非最大抑制以消除具有较低置信度的冗余重叠框(NMS)
  350. vector<int> nms_result;
  351. cv::dnn::NMSBoxes(boxes, confidences, nmsScoreThreshold, nmsThreshold, nms_result);
  352. float confidenceMax = 0;
  353. int confidenceMaxId = 0;
  354. output.clear();
  355. resultValues.clear();
  356. if (nms_result.size() > 0)
  357. {
  358. vector<string> units;
  359. vector<pair<string, Point>> numCoordinates;
  360. for (int i = 0; i < nms_result.size(); i++) {
  361. int idx = nms_result[i];
  362. Output result(classIds[idx], confidences[idx], boxes[idx]);
  363. auto typeName = className[classIds[idx]];
  364. auto item = std::find_if(classTypeName.begin(), classTypeName.end(), [typeName](string strValue)
  365. {
  366. return typeName == strValue;
  367. });
  368. output.push_back(result);
  369. if (item!= classTypeName.end())
  370. {
  371. units.push_back(typeName);
  372. }
  373. else
  374. {
  375. numCoordinates.push_back(pair<string, Point>(className[classIds[idx]], Point(boxes[idx].x + (boxes[idx].width / 2), boxes[idx].y + boxes[idx].height)));
  376. }
  377. YunDaISASImageRecognitionService::ConsoleLog(QString::fromStdString(className[classIds[idx]]));
  378. }
  379. vector<pair<vector<Point>,vector<string>>> sameYNums;
  380. if (numCoordinates.size()>0)
  381. {
  382. for (size_t i = 0; i < numCoordinates.size(); i++)
  383. {
  384. Point yCondinate = numCoordinates[i].second;
  385. string sValue = numCoordinates[i].first;
  386. auto item = std::find_if(sameYNums.begin(), sameYNums.end(), [yCondinate](pair<vector<Point>, vector<string>> p)
  387. {
  388. if (p.second.size()>0)
  389. {
  390. if (cv::abs(p.first[0].y - yCondinate.y)<50)
  391. {
  392. return true;
  393. }
  394. }
  395. return false ;
  396. });
  397. if (item != sameYNums.end())
  398. {
  399. item->second.push_back(sValue);
  400. item->first.push_back(yCondinate);
  401. }
  402. else {
  403. auto resultPair = pair<vector<Point>, vector<string >>(vector<Point>{ yCondinate }, vector<string>{sValue});
  404. sameYNums.push_back(resultPair);
  405. /*if (sameYNums.size()>0)
  406. {
  407. if(sameYNums[sameYNums.size() - 1].first[0].y> yCondinate.y) {
  408. sameYNums.emplace(sameYNums.end(), resultPair);
  409. }
  410. }
  411. else
  412. {
  413. sameYNums.push_back(resultPair);
  414. }*/
  415. }
  416. }
  417. }
  418. if (sameYNums.size()>0)
  419. {
  420. Point exchange ;
  421. string excStr;
  422. for (size_t k = 0; k < sameYNums.size(); k++)
  423. {
  424. for (int i = 1; i < sameYNums[k].first.size()-1; i++) //主要算法
  425. {
  426. for (int j = 1; j <= sameYNums[k].first.size() - i; j++)
  427. {
  428. if (sameYNums[k].first[j - 1].x > sameYNums[k].first[j].x)
  429. {
  430. exchange = sameYNums[k].first[j - 1];
  431. excStr = sameYNums[k].second[j - 1];
  432. sameYNums[k].first[j - 1] = sameYNums[k].first[j];
  433. sameYNums[k].second[j - 1] = sameYNums[k].second[j];
  434. sameYNums[k].first[j] = exchange;
  435. sameYNums[k].second[j] = excStr;
  436. }
  437. }
  438. }
  439. }
  440. for (size_t i = 0; i < sameYNums.size(); i++)
  441. {
  442. //int pointPos = -1;
  443. string strValue = "";
  444. for (size_t j = 0; j < sameYNums[i].second.size(); j++)
  445. {
  446. if (sameYNums[i].second[j] == "point")
  447. {
  448. strValue += ".";
  449. }
  450. else
  451. {
  452. //stoi(sameYNums[i].second[j])
  453. strValue += sameYNums[i].second[j];
  454. }
  455. }
  456. if (sameYNums.size() == units.size())
  457. {
  458. resultValues.push_back(DectectResult(0.99, atof(strValue.c_str()), units[i]));
  459. }
  460. else if (units.size()>0)
  461. {
  462. resultValues.push_back(DectectResult(0.99, atof(strValue.c_str()), units[0]));
  463. }
  464. else {
  465. resultValues.push_back(DectectResult(0.99, atof(strValue.c_str()), ""));
  466. }
  467. }
  468. }
  469. }
  470. else {
  471. resultValue = DectectResult(confidenceMax, 0.00, "");
  472. }
  473. return false;
  474. }