{"id":136,"date":"2023-03-02T22:44:08","date_gmt":"2023-03-02T14:44:08","guid":{"rendered":"https:\/\/www.dirx.com.cn\/?p=136"},"modified":"2023-03-02T22:45:38","modified_gmt":"2023-03-02T14:45:38","slug":"1","status":"publish","type":"post","link":"https:\/\/www.dirx.com.cn\/index.php\/2023\/03\/02\/1\/","title":{"rendered":"\u6d4b\u8bd5"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">&lt;pre><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n#include &lt;iostream>  \n#include &lt;opencv2\/highgui\/highgui.hpp>  \n#include &lt;opencv2\/imgproc.hpp>\n#include &lt;opencv2\/imgproc\/types_c.h>\n#include &lt;map>\n\n#include \"opencv2\/imgproc\/imgproc.hpp\"\n\nusing namespace std;\nusing namespace cv;\n#include&lt;vector>\n#include&lt;string>\n#include &lt;opencv2\/features2d.hpp>\n#include &lt;opencv2\/photo.hpp>\n#include&lt;set>\n\n\/\/\u9ad8\u65af\u6ee4\u6ce2\n\n\n\n\n\nint getThreshold(Mat grayImg)\n{\n\t\/\/1.\u6c42\u7070\u5ea6\u76f4\u65b9\u56fe\n\tvector&lt;int>histo(256);\n\tfor (int i = 0; i &lt; grayImg.rows; i++)\n\t{\n\t\tfor (int j = 0; j &lt; grayImg.cols; j++)\n\t\t{\n\t\t\thisto&#91;grayImg.at&lt;uchar>(i, j)]++;\n\t\t}\n\t}\n\t\/\/2.\u6839\u636e\u4e0a\u9762\u7684\u76f4\u65b9\u56fe \u7528\u8fed\u4ee3\u6cd5\u6c42\u9608\u503c\n\tint count0, count1;\/\/count0,count1\u5206\u522b\u662f\u5927\u4e8et0\u548c\u5c0f\u4e8et0\u7684\u50cf\u7d20\u70b9\u7684\u4e2a\u6570\n\n\tint t0 = 150, t = 0; \/\/t0\u662f\u521d\u59cb\u7684\u9608\u503c\uff0ct\u662f\u6bcf\u4e00\u6b21\u7ecf\u8fc7\u8fed\u4ee3\u8fd0\u7b97\u540e\u7684\u9608\u503c \u5f53t=t0\u65f6\u8ba4\u4e3a\u627e\u5230\n\n\tint z0, z1;      \/\/z0,\u5728z1\u5206\u522b\u662f\u5927\u4e8et0\u548c\u5c0f\u4e8et0\u7684\u50cf\u7d20\u503c\u7684\u603b\u548c\n\twhile (1)\n\t{\n\t\tcount0 = count1 = z0 = z1 = 1;\n\t\tfor (int i = 0; i &lt; histo.size(); i++)\n\t\t{\n\t\t\tif (i &lt;= t0)\n\t\t\t{\n\t\t\t\tcount0 += histo&#91;i];\n\t\t\t\tz0 += i * histo&#91;i];\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tcount1 += histo&#91;i];\n\t\t\t\tz1 += i * histo&#91;i];\n\t\t\t}\n\t\t}\n\n\t\tt = (z0 \/ count0 + z1 \/ count1) \/ 2;\n\t\tif (t0 == t)\n\t\t\tbreak;\n\t\telse t0 = t;\n\t}\n\treturn  t0 - 11;\n}\n\nMat Hough(Mat srcImage)\n{\n\tMat grad_y;\n\t\/\/\u8c03\u6574\u7b2c\u516d\u4e2a\u53c2\u6570  \u6709\u4e0d\u540c\u7684\u6548\u679c1,3,5,7 \u7b2c3\u4e2a\u53c2\u6570\u4e5f\u53ef\u4ee5\u6362\u5177\u4f53\u53c2\u7167254\u9875\n\tSobel(srcImage, grad_y, -1, 0, 1, 5);\n\tvector&lt;Vec2f>lines;\n\tHoughLines(grad_y, lines, 1, CV_PI \/ 180, 210);\n\tfloat sum = 0;\n\tfor (int i = 0; i &lt; lines.size(); i++)\n\t{\n\t\tsum += lines&#91;i]&#91;1];\n\t}\n\tfloat alpha = 0;\n\tif (lines.size() == 0)\n\t{\n\t\talpha = CV_PI \/ 2;\n\t}\n\n\n\telse\n\t{\n\t\talpha = sum \/ lines.size();\n\t}\n\talpha = alpha * 180 \/ CV_PI - 90;\n\tMat dstImage;\n\tdouble width = srcImage.cols, height = srcImage.rows;\n\t\/\/Point2f center(srcImage.cols \/ 2, srcImage.rows \/ 2);\n\tMat M = getRotationMatrix2D(Point(width \/ 2, height \/ 2), alpha, 1);\n\twarpAffine(srcImage, dstImage, M, srcImage.size());\n\t\/\/imshow(\"\u65cb\u8f6c\", dstImage);\n\treturn dstImage;\n}\n\n\n\/\/\u4fb5\u8680\u64cd\u4f5c\n\/\/Mat qinshi(Mat&amp; img)\n\/\/{\n\/\/\n\/\/\t\/\/\tnamedWindow(\"\u539f\u59cb\u56fe\", WINDOW_NORMAL);\n\/\/\t\/\/\timshow(\"\u539f\u59cb\u56fe\", img); \n\/\/\tMat out;\n\/\/\t\/\/\u83b7\u53d6\u81ea\u5b9a\u4e49\u6838\n\/\/\tMat element = getStructuringElement(MORPH_RECT, Size(3, 3)); \/\/\u7b2c\u4e00\u4e2a\u53c2\u6570MORPH_RECT\u8868\u793a\u77e9\u5f62\u7684\u5377\u79ef\u6838\uff0c\u5f53\u7136\u8fd8\u53ef\u4ee5\u9009\u62e9\u692d\u5706\u5f62\u7684\u3001\u4ea4\u53c9\u578b\u7684\n\/\/\t\/\/\u8150\u8680\u64cd\u4f5c\n\/\/\tdilate(img, out, element);\n\/\/\t\/\/\u540c\u4e00\u56fe\u7247\u5927\u5c0f\n\/\/\tdouble width = 800;\n\/\/\tdouble height = width * img.rows \/ img.cols;\n\/\/\tresize(out, out, Size(width, height));\n\/\/\n\/\/\t\/\/imshow(\"\u8150\u8680\u64cd\u4f5c\", out);\n\/\/\treturn out;\n\/\/}\n\n\nMat splitbian(Mat&amp; src_img)\n{\n\tMat mattemp = src_img.clone();\/\/\u6df1\u5ea6\u62f7\u8d1d\n\tint height = src_img.rows, width = src_img.cols;\/\/\u8d4b\u503c\n\tvector&lt;int>hang(src_img.rows);\n\tvector&lt;int>lie(src_img.cols);\n\tfor (int i = 0; i &lt; height; i++)\n\t{\n\t\tfor (int j = 0; j &lt; width; j++)\n\t\t{\n\t\t\tif (src_img.at&lt;uchar>(i, j) != 0)\n\t\t\t{\n\t\t\t\thang&#91;i]++;\n\t\t\t\tlie&#91;j]++;\n\t\t\t}\n\t\t}\n\t}\n\t\/*cout &lt;&lt; \"\u6bcf\u5217\u7684\u548c\uff1a\" &lt;&lt; endl;\n\tfor (int i = 0; i &lt; width; i++)\n\t\tcout &lt;&lt; lie&#91;i] &lt;&lt; \"  \" &lt;&lt; i &lt;&lt; endl;\n\n\tcout &lt;&lt; \"\u6bcf\u884c\u7684\u548c\uff1a\" &lt;&lt; endl;\n\tfor (int i = 0; i &lt; height; i++)\n\t\tcout &lt;&lt; hang&#91;i] &lt;&lt; \"   \";*\/\n\tint shangqie = 0;  \/\/\u8868\u793a\u5de6\u4e0a\u7684y\n\tfor (int i = 0; i &lt; height; i++)\n\t{\n\t\tif (hang&#91;i] > 400)\n\t\t\tshangqie = i;\n\t\tif (hang&#91;i] &lt; 400)\n\t\t{\n\t\t\t\/\/shangqie = i;\n\t\t\tbreak;\n\t\t}\n\t}\n\tint xiaqie = (height \/ 20) * 19;\/\/\u8868\u793a\u53f3\u4e0b\u7684y\n\tfor (int i = height - 1; i > (height \/ 20) * 19; i--)\n\t{\n\t\tif (hang&#91;i] > 400)\n\t\t\txiaqie = i;\n\t\tif (hang&#91;i] &lt; 400)\n\t\t{\n\t\t\t\/\/\txiaqie = i;\n\t\t\tbreak;\n\t\t}\n\n\t}\n\tint zuoqie = (width \/ 20);\n\tfor (int i = 0; i &lt; width; i++)\n\t{\n\t\tif (lie&#91;i] > 400)\n\t\t\tzuoqie = i;\n\t\tif (lie&#91;i] &lt; 400)\n\t\t{\n\t\t\t\/\/zuoqie = i;\n\t\t\tbreak;\n\t\t}\n\n\t}\n\tint youqie = (width \/ 20) * 19;\n\tfor (int i = width - 1; i > (width \/ 20) * 19; i--)\n\t{\n\t\tif (lie&#91;i] > 300)\n\t\t\tyouqie = i;\n\t\tif (lie&#91;i] &lt; 200)\n\t\t\tbreak;\n\t}\n\tif (zuoqie &lt; (width \/ 10))\n\t{\n\t\tzuoqie = zuoqie;\n\t}\n\telse\n\t\tzuoqie = (width \/ 10);\n\tconst Rect img1 = Rect(zuoqie, 0, youqie - zuoqie - 5, height);\n\tMat temp1;\n\ttemp1 = src_img(img1);\n\t\/\/\timshow(\"\u88c1\u526a\u56fe\", temp1);\n\n\twaitKey();\n\treturn temp1;\n}\n\nint th(Mat src_img)\n{\n\tMat mattemp = src_img.clone();\/\/\u6df1\u5ea6\u62f7\u8d1d\n\tint height = src_img.rows, width = src_img.cols;\/\/\u8d4b\u503c\n\tint sum = 0;\n\tfor (int i = 0; i &lt; height; i++)\n\t{\n\t\tfor (int j = 0; j &lt; width; j++)\n\t\t{\n\t\t\tif (src_img.at&lt;uchar>(i, j) != 0)\n\t\t\t{\n\t\t\t\tsum++;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn sum;\n}\n\n\n\/\/\u6c34\u5e73\u6295\u5f71\nMat splitrow(Mat mat)\n{\n\n\tMat mattemp = mat.clone();\/\/\u6df1\u5ea6\u62f7\u8d1d\n\t\/\/\timshow(\"\u539f\u56fe\", mattemp);\n\t\t\/\/waitKey();\n\tint height = mattemp.rows, width = mattemp.cols;\n\tint temp = 0;\/\/\u5b58\u50a8255\u7070\u5ea6\u7ea7\u7684\u4e2a\u6570\uff1f\n\tint* heightnum = new int&#91;height];\/\/\u884c\u53f7\/\/\u4fdd\u5b58\u6bcf\u4e00\u884c255\u6570\u76ee\u7684\u6570\u7ec4\n\tint y1 = height;\n\tint y2 = 0;\n\t\/\/\u5f00\u59cb\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\n\tfor (int i = 0; i &lt; height; i++)\n\t{\n\t\ttemp = 0;\n\t\tfor (int j = 0; j &lt; width; j++)\n\t\t{\n\t\t\tif (mattemp.at&lt;uchar>(i, j) != 0)\n\t\t\t{\n\t\t\t\ttemp++;\/\/\u7edf\u8ba1\u767d\u8272\n\n\t\t\t}\n\t\t}\n\t\t\/\/\u4e00\u884c\u5b8c\u4e86\n\t\theightnum&#91;i] = temp;\n\t\t\/\/cout &lt;&lt; heightnum&#91;i] &lt;&lt; \"   \" &lt;&lt; i &lt;&lt; endl;\n\n\t}\n\n\n\n\n\tfor (int i = 0; i &lt; height - 1; i++)\n\t{\n\n\n\t\tif (heightnum&#91;i] &lt; 10 &amp;&amp; heightnum&#91;i + 10]>10)\n\t\t{\n\t\t\ty1 = i;\n\n\t\t\tcout &lt;&lt; \"y1\u662f\uff1a\" &lt;&lt; y1 &lt;&lt; endl;\n\t\t\tbreak;\n\t\t}\n\n\t}\n\tfor (int i = y1 + 10; i &lt; height - 1; i++)\n\t{\n\n\n\t\tif (heightnum&#91;i] &lt; 10)\n\t\t{\n\t\t\ty2 = i;\n\t\t\tcout &lt;&lt; \"y2\u662f\uff1a\" &lt;&lt; y2 &lt;&lt; endl;\n\t\t\tbreak;\n\n\t\t}\n\t}\n\tcout &lt;&lt; \"\u6267\u884c\u5b8c\u6bd5\" &lt;&lt; endl;\n\tMat temp1;\n\tif (y1 > height \/ 2 || y2 > height \/ 3 * 2)\n\t{\n\t\ttemp1 = splitbian(mat);\n\t\ttemp1 = splitrow(temp1);\n\n\t}\n\telse\n\t{\n\t\tif (y2 - y1 &lt;= 20)\n\t\t\ty2 = y1 + 60;\n\n\n\t\t\/\/\u5f00\u59cb\u5207\u5272\u56fe\u7247\n\n\t\t\/\/\u8bbe\u6a2a\u8f74\u662fx\u8f74\uff0c\u7ad6\u8f74\u662fy\u8f74\n\t\t\/\/\u8fd9\u4e2a\u5148\u8f93\u5165x ,\u540e\u8f93\u5165y\n\t\tRect img1 = Rect(0, y1, width, y2 - y1 + 5);\n\t\ttemp1 = mat(img1);\n\t\t\/\/imshow(\"\u88c1\u526a\u56fe\", temp1);\n\t\t\/\/waitKey();\n\t}\n\tdelete&#91;] heightnum;\n\treturn temp1;\n\n\n}\n\n\n\n\n\n\n\/\/\u5782\u76f4\u6295\u5f71\u622a\u53d6\u5217\n\/\/\u7ad6\u76f4\u6295\u5f71\u5206\u5272\u5217\nvoid splitCol(Mat&amp; mat, int* position1, int* position2, int* stringwidth, int&amp; idx)\n{\n\tcout &lt;&lt; \"\u5f00\u59cb\u5782\u76f4\u6295\u5f71\" &lt;&lt; endl;\n\n\tMat mattemp = mat.clone();\/\/\u6df1\u5ea6\u62f7\u8d1d\n\t\/\/imshow(\"\u539f\u56fe\", mattemp);\n\tint height = mattemp.rows, width = mattemp.cols;\/\/\u8d4b\u503c\n\tint* proj = new int&#91;width];\n\n\n\n\tfor (int col = 0; col &lt; width; col++)\n\t{\n\t\tint temp = 0;\n\t\tfor (int row = 0; row &lt; height; row++)\n\t\t{\n\t\t\tif (mattemp.at&lt;uchar>(row, col) != 0)\n\t\t\t{\n\t\t\t\ttemp++;\n\t\t\t}\n\t\t}\n\t\tproj&#91;col] = temp;  \/\/\u7edf\u8ba1\u767d\u7684\u4e2a\u6570\n\n\t}\n\t\/\/    \/\/\u9ed1\u5e95\u767d\u5b57 \u9047\u5230\u9ed1\u662f\u5b57\n\t\t  \/\/for (int i = 0; i &lt; height; i++)\n\t\t  \/\/\tcout &lt;&lt; proj&#91;i] &lt;&lt; endl;\n\t\t  \/\/\u786e\u8ba4\u5b57\u7b26\u4f4d\u7f6e\n\n\tint count = 1;\n\tint pix;\/\/\u5b57\u7b26\u7684\u5206\u5272\u4f4d\u7f6e\n\t\/\/int position1&#91;100000];\/\/\u7b2c\u4e00\u6b21\u9047\u5230\u767d\u8272\u7684\u4f4d\u7f6e\n\t\/\/int position2&#91;100000];\/\/\u7b2c\u4e8c\u6b21\u9047\u5230\u767d\u8272\u7684\u4f4d\u7f6e\n\tfor (int i = 0; i &lt; width; i++)\n\t{\n\t\tpix = proj&#91;i];\n\t\tif (pix >= 2 &amp;&amp; (count % 2 != 0))  \/\/\u9047\u5230\u9ed1\u8272\u4e86\u8981\u5f00\u59cb\u526a\u4e86\n\t\t{\n\t\t\tif (i - 1 > 0)\n\t\t\t\tposition1&#91;idx] = i - 1;\n\t\t\telse\n\t\t\t\tposition1&#91;idx] = i;\n\t\t\tcount++;\n\t\t\tcontinue;\n\t\t}\n\t\telse if (pix &lt; 2 &amp;&amp; (count % 2 == 0))\/\/\u9047\u5230 \u767d\u8272\u526a\u7684\u505c\u6b62\u4f4d\u7f6e\n\t\t{\n\t\t\tif (i + 1 &lt; width)\n\t\t\t\tposition2&#91;idx] = i + 1;\n\t\t\telse\n\t\t\t\tposition2&#91;idx] = i;\n\t\t\tcount++;\n\t\t\tidx++;\n\t\t}\n\n\t\t\/\/if (i > (width - 2) &amp;&amp; (count % 2 != 0))  \/\/\u524d\u8fb9\u6709\u4e00\u534a\u4e86\uff0c\u4f46\u662f\u8fd9\u540e\u4e00\u534a\u6ca1\u627e\u5230\n\t\t\/\/{\n\t\t\/\/\t\n\t\t\/\/\tposition2&#91;idx] = i;\n\t\t\/\/\tcount++;\n\t\t\/\/\tidx++;\n\t\t\/\/}\n\n\t}\n\n\n\t\/\/\u8bb0\u5f55\u6240\u6709\u5b57\u7b26\u7684\u5bbd\u5ea6\n\tfor (int i = 0; i &lt; idx; i++)\n\t{\n\t\tstringwidth&#91;i] = position2&#91;i] - position1&#91;i];\n\t\t\/\/\tcout &lt;&lt; \"\u5bbd\u4e3a\uff1a\"&lt;&lt; stringwidth&#91;i] &lt;&lt; endl;\n\t}\n\n\n\n\n\t\/\/Mat number_img = Mat(Scalar(0));\n\t\/\/for (int i = 0; i &lt; idx; i++) \n\t\/\/{\n\t\/\/\tRect choose_rect(position1&#91;i], 0, swidth&#91;i], height);\n\t\/\/\tnumber_img = mat(choose_rect);\n\t\/\/\timshow(\"number\" + to_string(i), number_img);\n\t\/\/\n\t\/\/\t\/\/ imwrite(\"number\" + to_string(i) + \".jpg\", number_img);\n\t\/\/}\n\n}\n\n\n\n\/\/\u627e\u6700\u5c0f\u77e9\u9635\nvoid findminjuzhen(Mat src, pair&lt;int, int>&amp; num1, pair&lt;int, int>&amp; num2)\n{\n\n\n\tint height = src.rows, width = src.cols;\/\/\u8d4b\u503c\n\tvector&lt;int>hang(src.rows);\n\tvector&lt;int>lie(src.cols);\n\tfor (int i = 0; i &lt; height; i++)\n\t{\n\t\tfor (int j = 0; j &lt; width; j++)\n\t\t{\n\t\t\tif (src.at&lt;uchar>(i, j) != 0)\n\t\t\t{\n\t\t\t\thang&#91;i]++;\n\t\t\t\tlie&#91;j]++;\n\t\t\t}\n\t\t}\n\t}\n\tfor (int i = 0; i &lt; height; i++)\n\t\tif (hang&#91;i] != 0)\n\t\t\tnum1.second = i;    \/\/\u6700\u4e0b\u754c\n\tfor (int i = 0; i &lt; width; i++)\n\t\tif (lie&#91;i] != 0)\n\t\t\tnum2.second = i;\n\tfor (int i = height - 1; i >= 0; i--)\n\t\tif (hang&#91;i] != 0)\n\t\t\tnum1.first = i; \/\/\u6700\u4e0a\u754c\n\tfor (int i = width - 1; i >= 0; i--)\n\t\tif (lie&#91;i] != 0)\n\t\t\tnum2.first = i;\n\n\n\n\n\tcout &lt;&lt; \"(\" &lt;&lt; num1.first &lt;&lt; \" \" &lt;&lt; num1.second &lt;&lt; \")\";  \/\/\u4e0a\u4e0b\u8fb9\u754c\n\tcout &lt;&lt; \" \";\n\tcout &lt;&lt; \"(\" &lt;&lt; num2.first &lt;&lt; \" \" &lt;&lt; num2.second &lt;&lt; \")\";\/\/\u5de6\u53f3\u8fb9\u754c\n\n}\n\n\/\/\u5b57\u7b26\u5339\u914d\n\/\/\u9996\u5148\u662f\u4e24\u5f20\u56fe\u4f5c\u5dee\ndouble imgcha(Mat inimg1, Mat mubanimg)\n{\n\t\/\/\u8bb0\u5f55\u4e24\u5f20\u56fe\u4e0d\u540c\u7684\u50cf\u7d20\u70b9\u7684\u4e2a\u6570\n\tdouble difnum = 0;\n\t\/\/cvtColor(mubanimg, mubanimg, COLOR_BGR2GRAY);\n\t\/\/threshold(mubanimg, mubanimg, getThreshold( mubanimg), 255, THRESH_OTSU);  \/\/\u6a21\u677f\u4e8c\u503c\u5316\n\t\/\/\u540c\u6837\u5927\u5c0f\u7684\u5904\u7406\n\tresize(inimg1, inimg1, Size(30, 55));\n\tresize(mubanimg, mubanimg, Size(30, 55));\n\n\tMat temp0;\n\tabsdiff(inimg1, mubanimg, temp0);\n\tfor (int i = 0; i &lt; temp0.rows; i++)\n\t{\n\t\tfor (int j = 0; j &lt; temp0.cols; j++)\n\t\t{\n\t\t\tdifnum += temp0.at&lt;uchar>(i, j);\n\t\t}\n\t}\n\n\treturn difnum;  \/\/\u8fd4\u56de\u4e0d\u4e00\u6837\u7684\u503c\n}\ntypedef pair&lt;double, int>PDL;\nint pathnum = 0;\n\n\nvoid splitSample(char ch, int k, Mat inputImg)\n{\n\tstring st = to_string(k) + \".jpg\";\n\t\/\/cout &lt;&lt; st &lt;&lt; endl;\n\timwrite(\"F:\\\\erjixiangmu\\\\erjixiangmu\\\\muban2\\\\\" + to_string(ch - 48) + \".\" + st, inputImg);\n\n}\nchar recognition(Mat inputImg, int k)\n{\n\tstring sampleimgpathall = \"muban\";\n\tvector&lt;String> sampleimgpath;\n\tglob(sampleimgpathall, sampleimgpath, false);\/\/\u8bfb\u53d6\u6a21\u677f\u6240\u6709\u7684\u8def\u5f84\uff0c\u5b58\u5230FN\u91cc\u9762\n\tint sampleimgnum = sampleimgpath.size();\n\tvector&lt;pair&lt; double, int> >nums(sampleimgnum + 1);\/\/i\u8868\u793a\u56fe\u7247\u5728\u8def\u5f84vector\u91cc\u7684\u4e0b\u6807 \u5e76\u5b58\u50a8\u8be5\u56fe\u7247\u548c\u6a21\u677f\u91cc\u7684\u56fe\u7247\u7684\u5dee\n\tfor (int i = 0; i &lt; sampleimgnum; i++) {\n\n\t\tMat numImg = imread(sampleimgpath&#91;i], 0);\n\t\t\/\/\u5927\u5c0f\u540c\u4e00\n\t\tresize(numImg, numImg, Size(40, 60));\n\t\tresize(inputImg, inputImg, Size(40, 60));\n\t\tnums&#91;i].first = imgcha(inputImg, numImg);\/\/\u4e0d\u540c\u50cf\u7d20\u503c\u7684\u5dee\n\t\tnums&#91;i].second = i;\/\/\u5728\u8fed\u4ee3\u5668\u4e2d\u7684\u4e0b\u6807\n\t}\n\t\/\/ imshow(\"\u56fe\u7247\", inputImg);\n\twaitKey();\n\t\/\/\u6392\u5e8f \u8d8a\u5c0f\u8bf4\u660e\u5339\u914d\u5ea6\u8d8a\u9ad8\n\tsort(&amp;nums&#91;0], &amp;nums&#91;sampleimgnum]);\n\tint index = nums&#91;0].second; \/\/\u8bfb\u53d6\u6a21\u677f\u91cc\u7684\u8be5\u56fe\u7247\n\t\/\/\u622a\u53d6\u6a21\u677f\u7684\u51fd\u6570\n\tsplitSample(sampleimgpath&#91;index]&#91;sampleimgpathall.size() + 1], k, inputImg);\n\treturn sampleimgpath&#91;index]&#91;sampleimgpathall.size() + 1];\n\n}\n\n\n\/\/\u56fe\u50cf\u9510\u5316\nMat ToSharoen(Mat inputImage)\n{\n\tMat outputImage(inputImage.size(), inputImage.type());\n\tint cnel = inputImage.channels();\n\tint rows = inputImage.rows;\n\tint cols = (inputImage.cols - 1) * cnel;\n\n\tfor (int row = 1; row &lt; rows - 1; row++)\n\t{\n\t\tconst uchar* previous = inputImage.ptr&lt;uchar>(row - 1);\n\t\tconst uchar* current = inputImage.ptr&lt;uchar>(row);\n\t\tconst uchar* next = inputImage.ptr&lt;uchar>(row + 1);\n\t\tuchar* output = outputImage.ptr(row);\n\t\tfor (int col = cnel; col &lt; cols; col++)\n\t\t{\n\t\t\toutput&#91;col] = saturate_cast&lt;uchar>(5 * current&#91;col] - (previous&#91;col] + current&#91;col + 1] + current&#91;col - 1] + next&#91;col]));\n\t\t}\n\t}\n\t\/\/namedWindow(\"\u9510\u5316\u56fe\", WINDOW_AUTOSIZE);\n\t\/\/imshow(\"\u9510\u5316\u56fe\", outputImage);\n\treturn outputImage;\n}\n\n\/\/ \u56fe\u50cf\u9510\u5316\n\n\n\n\n\nint main()\n{\n\tint imgsum = 0, rightimg = 0;\n\tint stringnum = 0, rightstringnum = 0;\n\tint errornum = 0;\n\t\/\/\u8bfb\u53d6ISBN\u56fe\u7247\n\tstring testimgpathall = \"ISBN\";\n\tvector&lt;string>testimgpath;\/\/\u5b58\u50a8\u5355\u4e2a\u56fe\u7247\u7684\u8def\u5f84\n\n\tglob(testimgpathall, testimgpath, false);\/\/\u8bfb\u53d6\u51fd\u6570\n\tint testimgsums = testimgpath.size();\n\tvector&lt;string>errorpath(testimgsums);\n\tvector&lt;int>yuzhi(testimgsums);\n\t\/\/\u5f00\u59cb\u5bf9\u6bcf\u4e00\u4e2aISBN\u8fdb\u884c\u64cd\u4f5c\n\tfor (int testindex = 0; testindex &lt; testimgsums; testindex++)\n\t{\n\t\tcout &lt;&lt; \"\u5f00\u59cb\u5904\u7406\u7b2c\" &lt;&lt; testindex &lt;&lt; \"\u5f20\u7167\u7247\" &lt;&lt; endl;\n\t\tMat src = imread(testimgpath&#91;testindex]);\n\n\t\t\/\/\u540c\u4e00\u56fe\u7247\u5927\u5c0f\n\t\tdouble width = 600;\n\t\tdouble height = width * src.rows \/ src.cols;\n\t\tresize(src, src, Size(width, height));\n\t\t\/\/imshow(\"\u539f\u56fe\", src);\n\n\t\tMat gary_src;\n\t\tcvtColor(src, gary_src, COLOR_BGR2GRAY);\n\t\t\/\/imshow(\"\u7070\u5ea6\u56fe\", gary_src);\n\n\t\tMat gary_sharoen = ToSharoen(gary_src);\n\t\t\/\/imshow(\"\u9510\u5316\u540e\u7684\u7070\u5ea6\u56fe\", gary_sharoen);\n\t\t\/\/ \n\t\t\t\/\/\u9ad8\u65af\u53bb\u566a\u5904\u7406\n\t\tMat gas;\n\t\tGaussianBlur(gary_sharoen, gas, Size(7, 7), 1, 1);\n\t\t\/\/\timshow(\"\u9ad8\u65af\u53bb\u566a\u540e\u7684\u7070\u5ea6\u56fe\", gas);\n\t\twaitKey();\n\n\n\n\n\t\t\/\/\u4e8c\u503c\u5316\n\t\tMat binImg;\n\t\tint thethreshold = getThreshold(gas);\n\t\tcout &lt;&lt; \"\u9608\u503c\u4e3a\uff1a\" &lt;&lt; thethreshold &lt;&lt; endl;\n\t\t\/\/\tTHRESH_BINARY (\u767d\u5e95\u9ed1\u5b57\uff09\u3001THRESH_BINARY_INV\uff08\u9ed1\u5e95\u767d\u5b57\uff09\n\t\t\t\/\/THRESH_TRUNC(\u5f88\u6697\u5f88\u6697\uff09THRESH_OTSU\n\t\tif (thethreshold &lt; 123)\n\t\t\tthethreshold = thethreshold;\n\t\telse\n\t\t\tthethreshold += 31;\n\n\t\tthreshold(gas, binImg, thethreshold, 255, THRESH_BINARY_INV);\n\n\t\t\/\/\u5224\u65ad\u662f\u767d\u5e95\u8fd8\u662f\u9ed1\u5e95\u56fe\n\t\tint thethreshold2 = th(binImg);\n\t\tcout &lt;&lt; \"\u767d\u70b9\u6570\uff1a\" &lt;&lt; thethreshold2;\n\t\tif (thethreshold2 > 190000)\n\t\t{\n\t\t\tcout &lt;&lt; \"\u767d\u5e95\u56fe\";\n\t\t\tthreshold(gas, binImg, thethreshold, 255, THRESH_BINARY);\n\t\t}\n\t\telse\n\t\t\tthreshold(gas, binImg, thethreshold, 255, THRESH_BINARY_INV);\n\n\n\t\t\/\/\timshow(\"\u4e8c\u503c\u5316\u56fe\", binImg);\n\t\twaitKey();\n\t\t\/\/\u65cb\u8f6c\n\t\tMat temp1 = Hough(binImg);\n\n\n\n\t\t\/\/\timshow(\"\u4e8c\u503c\u5316\u56fe\u7247\u8fdb\u884c\u970d\u592b\u65cb\u8f6c\", temp1);\n\t\twaitKey();\n\n\n\n\n\t\t\/\/\u6c34\u5e73\u88c1\u526a\n\n\t\tMat shui_img;\n\t\tshui_img = splitrow(temp1);\n\t\t\/\/\timshow(\"\u6c34\u5e73\u526a\u5b8c\u7684\u4e8c\u503c\u56fe\", shui_img);\n\t\twaitKey();\n\t\t\/\/\u4fb5\u8680\u4e00\u4e0b\n\t\t\t\/\/shui_img = qinshi(shui_img);\n\t\t\t\/\/imshow(\"\u6c34\u5e73\u5bdd\u5ba4\", shui_img);\n\t\twaitKey();\n\n\n\t\t\/\/\u505a\u5217\u7684\u5206\u5272\n\t\tint position1&#91;100000];\/\/\u7b2c\u4e00\u6b21\u9047\u5230\u767d\u8272\u7684\u4f4d\u7f6e\n\t\tint position2&#91;100000];\/\/\u7b2c\u4e8c\u6b21\u9047\u5230\u767d\u8272\u7684\u4f4d\u7f6e\n\t\tint  swidth&#91;1000];\/\/\u8bb0\u5f55\u5b57\u7b26\u5bbd\n\t\tint posidx = 0;\n\n\t\t\/\/\u5782\u76f4\u88c1\u526a\n\t\tsplitCol(shui_img, position1, position2, swidth, posidx);\n\t\t\/\/\u5f00\u59cb\u5bf9\u6bcf\u4e2a\u6570\u5b57\u8fdb\u884c\u5904\u7406\n\n\t\tstring ans = \"\";\n\t\tMat number_img = Mat(Scalar(0));\n\t\tfor (int i = 4; i &lt; posidx; i++)\n\t\t{\n\t\t\t\/\/Mat subImg = Mat(shuiimg, Range(0, shuiimg.rows), Range(position1&#91;i], position2&#91;i]));\n\t\t\t\/\/\u505a\u5217\u5206\u5272\n\t\t\tRect choose_rect(position1&#91;i], 0, swidth&#91;i], shui_img.rows);\n\t\t\tnumber_img = shui_img(choose_rect);\n\t\t\t\/\/\timshow(\"\u5782\u76f4\u6295\u5f71\u7684\u5206\u5272\u56fe\" + to_string(i), number_img);\n\t\t\twaitKey();\n\t\t\t\/\/\u627e\u8be5\u6570\u5b57\u7684\u6700\u5c0f\u77e9\u9635\n\t\t\tpair&lt;int, int> num1;\n\t\t\tpair&lt;int, int> num2;\n\t\t\tfindminjuzhen(number_img, num1, num2);\n\t\t\t\/\/imshow(\"\u6700\u5c0f\u77e9\u9635\", number_img);\n\t\t\t\/\/waitKey();\n\t\t\tint numimgheight = num1.second - num1.first;\n\t\t\tint numimgwidth = num2.second - num2.first;\n\t\t\tif (numimgheight > numimgwidth \/ 2 &amp;&amp; numimgwidth > 3 &amp;&amp; numimgheight > 5)\n\t\t\t{\n\t\t\t\tMat minImg = Mat(number_img, Range(num1.first, num1.second + 1), Range(num2.first, num2.second + 1));\n\t\t\t\t\/\/ imshow(\"\u6700\u5c0f\u77e9\u5f62\", minImg);\n\t\t\t\t\/\/waitKey();\n\t\t\t\t\/\/\u662f\u6570\u5b57\u6216\u8005\u5b57\u6bcd\n\t\t\t\tchar ch = recognition(minImg, testindex);\n\t\t\t\t\/\/char ch = pipei(minImg);\n\t\t\t\tif (ch >= '0' &amp;&amp; ch &lt;= '9')\n\t\t\t\t{\n\t\t\t\t\tans += ch;\n\t\t\t\t\tcout &lt;&lt; ch &lt;&lt; \" \";\n\t\t\t\t}\n\t\t\t}\n\n\n\n\t\t}\n\t\tcout &lt;&lt; endl;\n\n\t\t\/\/\u73b0\u5728\u5f00\u59cb\u5bf9\u6bd4\n\t\tcout &lt;&lt; \"\u73b0\u5728\u7684\u8def\u5f84\u662f\uff1a\" &lt;&lt; testimgpath&#91;testindex] &lt;&lt; endl;\n\t\tstring rightans = \"\";\n\t\tfor (int i = 0; i &lt; testimgpath&#91;testindex].length(); i++)\n\t\t{\n\t\t\tchar temp = testimgpath&#91;testindex]&#91;i];\n\t\t\tif (temp >= '0' &amp;&amp; temp &lt;= '9')\n\t\t\t{\n\t\t\t\trightans += temp;\n\t\t\t}\n\t\t}\n\n\t\tcout &lt;&lt; \"\u6b63\u786e\u7684\u6570\u636e\u662f\uff1a\" &lt;&lt; rightans &lt;&lt; endl;\n\t\tcout &lt;&lt; \"\u8bfb\u51fa\u6765\u7684\u7b54\u6848\u662f\uff1a\" &lt;&lt; ans &lt;&lt; endl;\n\t\tstringnum += rightans.length();\/\/\u603b\u7684\u5b57\u7b26\u6570\n\t\tint ansidx = min(ans.length(), rightans.length());\n\t\tbool right = true;\n\t\tfor (int i = 0; i &lt; ansidx; i++)\n\t\t{\n\t\t\tif (ans&#91;i] != rightans&#91;i])\n\t\t\t\tright = false;\n\t\t\telse\n\t\t\t\trightstringnum++;\/\/\u7b54\u5bf9\u7684\u5b57\u7b26\u6570\t\t\t\t\n\t\t}\n\t\tif (ans.length() == rightans.length() &amp;&amp; right)\/\/\u8bc6\u522b\u6b63\u786e\n\t\t{\n\t\t\trightimg++;\n\t\t\tcout &lt;&lt; \"YES\" &lt;&lt; endl;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tcout &lt;&lt; \"NO\" &lt;&lt; endl;\n\t\t\tcout &lt;&lt; \"-------------------------------------\" &lt;&lt; endl;\n\n\t\t\terrorpath&#91;errornum] = testimgpath&#91;testindex];\n\t\t\terrornum++;\n\t\t}\n\n\t}\n\tprintf(\"\u6b63\u786e\u4e2a\u6570\uff1a%4.d \u6b63\u786e\u7387\uff1a%f\\n\", rightimg, rightimg * 1.0 \/ testimgsums);\n\tprintf(\"\u51c6\u786e\u4e2a\u6570\uff1a%4.d \u51c6\u786e\u7387\uff1a%f\\n\", rightstringnum, rightstringnum * 1.0 \/ stringnum);\n\n\n\n\tfor (int i = 0; i &lt; errornum; i++)\n\t\tcout &lt;&lt; \"\u9519\u8bef\u7684\u8def\u5f84\u662f\uff1a\" &lt;&lt; errorpath&#91;i] &lt;&lt; endl;\n\n\twaitKey(0);\n\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;pre><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&lt;pre> &lt;pre><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-136","post","type-post","status-publish","format-standard","hentry","category-10"],"_links":{"self":[{"href":"https:\/\/www.dirx.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dirx.com.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dirx.com.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dirx.com.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dirx.com.cn\/index.php\/wp-json\/wp\/v2\/comments?post=136"}],"version-history":[{"count":3,"href":"https:\/\/www.dirx.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/136\/revisions"}],"predecessor-version":[{"id":139,"href":"https:\/\/www.dirx.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/136\/revisions\/139"}],"wp:attachment":[{"href":"https:\/\/www.dirx.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dirx.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dirx.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}