Guest User

Untitled

a guest
Jan 23rd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. #include"stdafx.h"
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <iostream>
  5. #include <sstream>
  6. #include <opencv/Labeling.h>
  7. #include <cstdio>
  8. #include<vector>
  9. #include<strstream>
  10. #include<string>
  11.  
  12. void run()
  13. {
  14. cv::Mat image;
  15.  
  16. //メインループ
  17. while (1) {
  18. //データの
  19.  
  20. 更新を待つ
  21. DWORD ret = ::WaitForSingleObject(streamEvent, INFINITE);
  22. ::ResetEvent(streamEvent);
  23.  
  24. drawRgbImage(image);
  25.  
  26. cv::Mat h_image;
  27. cv::flip(image, h_image, 1);
  28.  
  29.  
  30. // RGB画像をHSV画像に変換
  31. cv::Mat hsv, extracted;
  32. cv::cvtColor(h_image, hsv, CV_RGB2HSV); // カメラ画像がBGRの場合はCV_BGR2HSV
  33.  
  34. cv::Scalar hsv_min = cv::Scalar(120, 80, 80); // 色が100<=H<=120, 80<=S<=255, 80<=V<=255の範囲の部分を抽出する
  35. cv::Scalar hsv_max = cv::Scalar(125, 200, 200); // 色の範囲は抽出したい物体の色に合わせて調整する
  36. cv::inRange(hsv, hsv_min, hsv_max, extracted);
  37.  
  38. //グレースケール化
  39. cv::Mat gray;
  40. cv::cvtColor(hsv, gray, cv::COLOR_BGR2GRAY);
  41. //cv::imshow("グレースケール化", gray);
  42.  
  43. // 2値化
  44. cv::Mat thresh;
  45. cv::threshold(gray, thresh, 0, 255, cv::THRESH_OTSU);
  46. //cv::imshow("2値化", thresh);
  47.  
  48. // モルフォロジー処理
  49. cv::Mat opening;
  50. cv::Mat kernel(3, 3, CV_8U, cv::Scalar(1));
  51. cv::morphologyEx(thresh, opening, cv::MORPH_OPEN, kernel, cv::Point(-1, -1), 2);
  52. //cv::imshow("モルフォロジー処理化", opening);
  53.  
  54. // 背景領域抽出
  55. cv::Mat sure_bg;
  56. cv::dilate(opening, sure_bg, kernel, cv::Point(-1, -1), 3);
  57. //cv::imshow("背景領域", sure_bg);
  58.  
  59. // 前景領域抽出
  60. cv::Mat dist_transform;
  61. cv::distanceTransform(opening, dist_transform, CV_DIST_L2, 5); //輪郭から距離が離れるほど濃くする
  62. cv::Mat sure_fg;
  63. double minVal, maxVal;
  64. cv::Point minLoc, maxLoc;
  65. cv::minMaxLoc(dist_transform, &minVal, &maxVal, &minLoc, &maxLoc);
  66. cv::threshold(dist_transform, sure_fg, 0.2*maxVal, 255, 0);
  67. dist_transform = dist_transform / maxVal;
  68. //cv::imshow("距離変換", dist_transform);
  69. //cv::imshow("前景領域", sure_fg);
  70.  
  71. // 不明領域抽出
  72. cv::Mat unknown, sure_fg_uc1;
  73. sure_fg.convertTo(sure_fg_uc1, CV_8UC1);
  74. cv::subtract(sure_bg, sure_fg_uc1, unknown);
  75. //cv::imshow("不明領域", unknown);
  76.  
  77. // 前景ラベリング
  78. using namespace std;
  79.  
  80. int compCount = 0;
  81. vector<vector<cv::Point> > contours;
  82. vector<cv::Vec4i> hierarchy;
  83. sure_fg.convertTo(sure_fg, CV_32SC1, 1.0);
  84. cv::findContours(sure_fg, contours, hierarchy, cv::RETR_CCOMP, cv::CHAIN_APPROX_SIMPLE);
  85. if (contours.empty()) return;
  86. cv::Mat markers = cv::Mat::zeros(sure_fg.rows, sure_fg.cols, CV_32SC1);
  87. int idx = 0;
  88. for (; idx >= 0; idx = hierarchy[idx][0], compCount++)
  89. cv::drawContours(markers, contours, idx, cv::Scalar::all(compCount + 1), -1, 8, hierarchy, INT_MAX);
  90. markers = markers + 1;
  91.  
  92. // 不明領域は今のところゼロ
  93. for (int i = 0; i<markers.rows; i++) {
  94. for (int j = 0; j<markers.cols; j++) {
  95. unsigned char &v = unknown.at<unsigned char>(i, j);
  96. if (v == 255) {
  97. markers.at<int>(i, j) = 0;
  98. }
  99.  
  100. }
  101.  
  102. }
  103. // 分水嶺
  104. watershed(hsv, markers);
  105. cv::Mat wshed(markers.size(), CV_8UC3);
  106. vector<cv::Vec3b> colorTab;
  107. for (int i = 0; i < compCount; i++)
  108. {
  109. int b = cv::theRNG().uniform(0, 255);
  110. int g = cv::theRNG().uniform(0, 255);
  111. int r = cv::theRNG().uniform(0, 255);
  112. colorTab.push_back(cv::Vec3b((uchar)b, (uchar)g, (uchar)r));
  113. }
  114.  
  115. // paint the watershed image
  116. for (int i = 0; i < markers.rows; i++) {
  117. for (int j = 0; j < markers.cols; j++)
  118. {
  119. int index = markers.at<int>(i, j);
  120. if (index == -1)
  121. wshed.at<cv::Vec3b>(i, j) = cv::Vec3b(255, 255, 255);
  122. else if (index <= 0 || index > compCount)
  123. wshed.at<cv::Vec3b>(i, j) = cv::Vec3b(0, 0, 0);
  124. else
  125. wshed.at<cv::Vec3b>(i, j) = colorTab[index - 1];
  126. }
  127. }
  128. cv::Mat imgG;
  129. cvtColor(gray, imgG, cv::COLOR_GRAY2BGR);
  130. wshed = wshed * 0.5 + imgG * 0.5;
  131. cv::imshow("分水嶺", wshed);
  132.  
  133. //終了のためのキー入力チェック兼、表示のためのウェイト
  134. int key = cv::waitKey(10);
  135. if (key == 'q') {
  136. break;
  137. }
  138. }
  139. }
  140.  
  141. private:
Add Comment
Please, Sign In to add comment