Guest User

Untitled

a guest
Jul 20th, 2020
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. #include <QApplication>
  2. #include "widget.h"
  3.  
  4.  
  5. using namespace std;
  6. using namespace cv;
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10. QApplication a(argc, argv);
  11.  
  12. Mat fg = imread("C:\\Users\\Roman\\Pictures\\pf8.bmp");
  13. Mat bg = imread("C:\\Users\\Roman\\Pictures\\background.bmp");
  14.  
  15. if(fg.empty()) {
  16. cout << "I can`t read fg file";
  17. return -1;
  18. }
  19. if(bg.empty()) {
  20. cout << "I can`t read bg file";
  21. return -1;
  22. }
  23.  
  24. if(fg.size != bg.size) {
  25. cout << "Input image have different size!";
  26. return -2;
  27. }
  28.  
  29. if(fg.rows > 500 && fg.cols > 500) resize(fg, fg, Size(), 0.5, 0.5, INTER_LINEAR);
  30. if(fg.rows > 500 && fg.cols > 500) resize(fg, fg, Size(), 0.5, 0.5, INTER_LINEAR);
  31.  
  32. if(bg.rows > 500 && bg.cols > 500) resize(bg, bg, Size(), 0.5, 0.5, INTER_LINEAR);
  33. if(bg.rows > 500 && bg.cols > 500) resize(bg, bg, Size(), 0.5, 0.5, INTER_LINEAR);
  34.  
  35. Mat distance = Mat::zeros(fg.rows, fg.cols, CV_32F);
  36.  
  37. vector<Mat> fgChannels;
  38. split(fg, fgChannels);
  39.  
  40. vector<Mat> bgChannels;
  41. split(bg, bgChannels);
  42.  
  43. for(size_t i = 0; i < fgChannels.size(); i++) {
  44. Mat temp = abs(fgChannels[i] - bgChannels[i]);
  45. temp.convertTo(temp, CV_32F);
  46. distance = distance + temp;
  47. }
  48.  
  49. Mat mask;
  50. threshold(distance, mask, 100, 255, THRESH_TOZERO); // Убираем фон
  51.  
  52. mask.convertTo(mask, CV_8UC1); // Преобразование в 8 битное 1 канальное изображение
  53.  
  54. vector<vector<Point>> contours;
  55. findContours(mask, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE); // Находим контуры
  56. drawContours(mask, contours, -1, Scalar(255), FILLED); // Заполняем найденные замкнутые контуры
  57.  
  58. Mat crop(fg.rows, fg.cols, CV_8UC3);
  59. crop.setTo(Scalar(0, 255, 0));
  60. fg.copyTo(crop, mask);
  61. normalize(mask.clone(), mask, 0.0, 255.0, NORM_MINMAX, CV_8UC1);
  62.  
  63. imwrite("cropped_preform.png", crop);
  64. imshow("cropped_preform", crop);
  65.  
  66. //Canny(crop, mask, 100, 200);
  67.  
  68. if(crop.empty()) {
  69. cout << "Don`t find crop image";
  70. return 3;
  71. }
  72.  
  73. vector<vector<Point>> contours_poly(contours.size());
  74. vector<Rect> boundRect(contours.size());
  75.  
  76. for(size_t i = 0; i < contours.size(); i++) {
  77. approxPolyDP(contours[i], contours_poly[i], 3, true);
  78. boundRect[i] = boundingRect(contours_poly[i]);
  79. }
  80.  
  81. Mat drawing = Mat::zeros(mask.rows, mask.cols, CV_8UC3);
  82.  
  83. for(size_t i = 0; i < contours.size(); i++) {
  84. drawContours(drawing, contours_poly, (int)i, Scalar(0, 255, 0));
  85. rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), Scalar(0, 255, 0), 1);
  86. }
  87.  
  88. imshow("Bound", drawing);
  89. waitKey(0);
  90.  
  91. destroyAllWindows();
  92.  
  93. return a.exec();
  94. }
Advertisement
Add Comment
Please, Sign In to add comment