• 基于opencv的缺陷检测


    1..基于opencv 差分图像二值分析的刀片缺陷检测 

    1. #include
    2. #include
    3. using namespace cv;
    4. using namespace std;
    5. string rootdir = "F:/Study/opencv/opencv-4.8.0/opencv/sources/samples/data/";
    6. void sort_box(vector &boxes) {
    7. int size = boxes.size();
    8. for (int i = 0; i < size - 1; i++) {
    9. for (int j = i; j < size; j++) {
    10. int x = boxes[j].x;
    11. int y = boxes[j].y;
    12. if (y < boxes[i].y) {
    13. Rect temp = boxes[i];
    14. boxes[i] = boxes[j];
    15. boxes[j] = temp;
    16. }
    17. }
    18. }
    19. }
    20. void detect_defect(Mat &binary, vector rects, vector &defect, Mat &tpl) {
    21. int h = tpl.rows;
    22. int w = tpl.cols;
    23. int size = rects.size();
    24. for (int i = 0; i < size; i++) {
    25. // 构建diff
    26. Mat roi = binary(rects[i]);
    27. resize(roi, roi, tpl.size());
    28. Mat mask;
    29. subtract(tpl, roi, mask);
    30. Mat se = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
    31. morphologyEx(mask, mask, MORPH_OPEN, se);
    32. threshold(mask, mask, 0, 255, THRESH_BINARY);
    33. //imshow("mask", mask);
    34. //waitKey(0);*/
    35. // 根据diff查找缺陷,阈值化
    36. int count = 0;
    37. for (int row = 0; row < h; row++) {
    38. for (int col = 0; col < w; col++) {
    39. int pv = mask.at(row, col);
    40. if (pv == 255) {
    41. count++;
    42. }
    43. }
    44. }
    45. // 填充一个像素宽
    46. int mh = mask.rows + 2;
    47. int mw = mask.cols + 2;
    48. Mat m1 = Mat::zeros(Size(mw, mh), mask.type());
    49. Rect mroi;
    50. mroi.x = 1;
    51. mroi.y = 1;
    52. mroi.height = mask.rows;
    53. mroi.width = mask.cols;
    54. mask.copyTo(m1(mroi));
    55. /*imshow("mask2", mask);
    56. imshow("ml", m1);*/
    57. waitKey(0);
    58. // 轮廓分析
    59. vector> contours;
    60. vector hierarchy;
    61. findContours(m1, contours, hierarchy, RETR_LIST, CHAIN_APPROX_SIMPLE);
    62. bool find = false;
    63. for (size_t t = 0; t < contours.size(); t++) {
    64. Rect rect = boundingRect(contours[t]);
    65. float ratio = (float)rect.width / ((float)rect.height);
    66. if (ratio > 4.0 && (rect.y < 5 || (m1.rows - (rect.height + rect.y)) < 10)) {
    67. continue;
    68. }
    69. double area = contourArea(contours[t]);
    70. if (area > 10) {
    71. printf("ratio : %.2f, area : %.2f \n", ratio, area);
    72. find = true;
    73. }
    74. }
    75. if (count > 50 && find) {
    76. printf("count : %d \n", count);
    77. defect.push_back(rects[i]);
    78. }
    79. }
    80. }
    81. void multiple_defects_detection(Mat &src) {
    82. Mat tpl = imread(rootdir + "dt.png", IMREAD_GRAYSCALE);
    83. // 图像二值化
    84. Mat gray, binary;
    85. cvtColor(src, gray, COLOR_BGR2GRAY);
    86. threshold(gray, binary, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
    87. imshow("binary", binary);
    88. imwrite("D:/binary.png", binary);
    89. // 定义结构元素
    90. Mat se = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
    91. morphologyEx(binary, binary, MORPH_OPEN, se);
    92. // 轮廓发现
    93. vector> contours;
    94. vector hierarchy;
    95. vector rects;
    96. findContours(binary, contours, hierarchy, RETR_LIST, CHAIN_APPROX_SIMPLE);
    97. int height = src.rows;
    98. for (size_t t = 0; t < contours.size(); t++) {
    99. Rect rect = boundingRect(contours[t]);
    100. double area = contourArea(contours[t]);
    101. if (rect.height > (height / 2)) {
    102. continue;
    103. }
    104. if (area < 150) {
    105. continue;
    106. }
    107. /*imshow("roi", binary(rect));
    108. waitKey(0);*/
    109. rects.push_back(rect);
    110. }
    111. // 对每个刀片进行比对检测
    112. sort_box(rects);
    113. vector defects;
    114. detect_defect(binary, rects, defects, tpl);
    115. // 显示检测结果
    116. for (int i = 0; i < defects.size(); i++) {
    117. rectangle(src, defects[i], Scalar(0, 0, 255), 2, 8, 0);
    118. putText(src, "bad", defects[i].tl(), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(255, 0, 0), 2, 8);
    119. }
    120. imshow("多个缺陷检测", src);
    121. waitKey(0);
    122. }
    123. int main() {
    124. Mat src=imread(rootdir+"ce_01.jpg");
    125. multiple_defects_detection(src);
    126. return 0;
    127. }

    2.基于opencv DNN模块的UNet道路裂纹检测 

    1. #include
    2. #include
    3. using namespace cv;
    4. using namespace std;
    5. string model_dir="F:/Study/opencv/opencv-4.8.0/opencv/sources/samples/data/";
    6. void resnet_surface_detection(Mat &image) {
    7. String defect_labels[] = { "In","Sc","Cr","PS","RS","Pa" };
    8. dnn::Net net = dnn::readNetFromONNX(model_dir + "surface_defect_resnet18.onnx");
    9. Mat inputBlob = dnn::blobFromImage(image, 0.00392, Size(200, 200), Scalar(127, 127, 127), false, false);
    10. inputBlob /= 0.5;
    11. // 执行图像分类
    12. Mat prob;
    13. net.setInput(inputBlob);
    14. prob = net.forward();
    15. // 得到最可能分类输出
    16. Mat probMat = prob.reshape(1, 1);
    17. Point classNumber;
    18. double classProb;
    19. minMaxLoc(probMat, NULL, &classProb, NULL, &classNumber);
    20. int classidx = classNumber.x;
    21. printf("\n current image classification : %s, possible : %.2f\n", defect_labels[classidx].c_str(), classProb);
    22. // 显示文本
    23. putText(image, defect_labels[classidx].c_str(), Point(20, 40), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 255), 2, 8);
    24. imshow("基于分类的缺陷检测", image);
    25. waitKey(0);
    26. }
    27. int main() {
    28. Mat image = imread("F:/Study/opencv/opencv-4.8.0/opencv/sources/samples/data/NEU-CLS/NEU-CLS/Ps_1.bmp");
    29. resnet_surface_detection(image);
    30. return 0;
    31. }

  • 相关阅读:
    使用 Postman 工具高效管理和测试 SAP ABAP OData 服务的试读版
    计算机网络学习笔记(II)——应用层
    Apache Paimon Flink引擎解析
    SQL explain解析器
    WT588F02B-8S(C006_03)单芯片语音ic方案为智能门铃设计降本增效赋能
    甲骨文发布适用于 MongoDB 的 Oracle Database API;Chrome 和 Edge 互相“拉踩”;树莓派驱动程序现可在 Android 上运行 | 开源日报
    2-3查找树
    MacBook Pro M1虚拟机安装西门子TIA博图v16无法正常运行部分组件
    js基础总结
    kubesphere 一键部署K8Sv1.21.5版本
  • 原文地址:https://blog.csdn.net/qq_52758467/article/details/136319333