Guest User

Untitled

a guest
Oct 22nd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "opencv2\opencv.hpp"
  4. #include <iostream>
  5.  
  6.  
  7. namespace harris {
  8.  
  9. using namespace std;
  10. using namespace cv;
  11.  
  12.  
  13. inline double det(const Mat1d& A)
  14. {
  15. return A(0, 0)*A(1, 1) - A(1, 0)*A(0, 1);
  16. }
  17.  
  18. inline double trace(const Mat1d& A)
  19. {
  20. return A(0, 0) + A(1, 1);
  21. }
  22.  
  23. inline double sign(double x) {
  24. return x > 0 ? 1.0 :
  25. x < 0 ? -1.0 : 0.0;
  26. }
  27.  
  28.  
  29.  
  30.  
  31. static void cornerEigenValsVecs(const Mat& src, Mat1f& eigenv,
  32. int block_size = 2, int aperture_size = 3,
  33. double kappa = 0., int borderType = cv::BORDER_DEFAULT) {
  34.  
  35. int depth = src.depth();
  36.  
  37. double scale = 1.0 / 255;
  38.  
  39. CV_Assert(src.type() == CV_8UC1 || src.type() == CV_32FC1);
  40.  
  41. Mat Dx, Dy;
  42.  
  43. Sobel(src, Dx, CV_32F, 1, 0, aperture_size, scale, 0, borderType);
  44. Sobel(src, Dy, CV_32F, 0, 1, aperture_size, scale, 0, borderType);
  45.  
  46. double dbgMax, dbgMin;
  47. cv::minMaxLoc(Dx, &dbgMin, &dbgMax);
  48. cout << "\tMaxDx : " << dbgMax << ", MinDx : " << dbgMin << endl;
  49. cv::minMaxLoc(Dy, &dbgMin, &dbgMax);
  50. cout << "\tMaxDy : " << dbgMax << ", MinDy : " << dbgMin << endl;
  51.  
  52. Size size = src.size();
  53. Mat3f cov(size);
  54. int i, j;
  55.  
  56. for (i = 0; i < size.height; i++)
  57. {
  58. float* cov_data = (float*)(cov.data + i*cov.step);
  59. const float* dxdata = (const float*)(Dx.data + i*Dx.step);
  60. const float* dydata = (const float*)(Dy.data + i*Dy.step);
  61.  
  62. for (j = 0; j < size.width; j++)
  63. {
  64. float dx = dxdata[j];
  65. float dy = dydata[j];
  66. #if 0
  67. dx = fabs(dx) > 1.0 ? sign(dx) : dx;
  68. dy = fabs(dy) > 1.0 ? sign(dy) : dy;
  69. #endif
  70.  
  71. cov_data[j * 3] = dx*dx;
  72. cov_data[j * 3 + 1] = dx*dy;
  73. cov_data[j * 3 + 2] = dy*dy;
  74. }
  75. }
  76.  
  77. boxFilter(cov, cov, cov.depth(), Size(block_size, block_size),
  78. Point(-1, -1), false, borderType);
  79.  
  80. eigenv = Mat1f::zeros(src.size());
  81.  
  82. for (int j = 0; j < src.rows; ++j) {
  83.  
  84. for (int i = 0; i < src.cols; ++i) {
  85. Mat1d A = Mat1d::zeros(2, 2);
  86. A(0, 0) = cov(j, i)[0]; // Ix**2
  87. A(1, 0) = A(0, 1) = cov(j, i)[1];
  88. A(1, 1) = cov(j, i)[2];
  89. eigenv(j, i) = det(A) - kappa * trace(A) * trace(A);
  90. }
  91. }
  92.  
  93. cv::minMaxLoc(eigenv, &dbgMin, &dbgMax);
  94. cout << "\tMaxEigen : " << dbgMax << ", MinEigen : " << dbgMin << endl;
  95. }
  96.  
  97. inline Mat3b get_vis(const Mat1d& src, double val_max, double val_min)
  98. {
  99. Mat3b rgb(src.rows, src.cols);
  100. for (int j = 0; j < src.rows; ++j) {
  101. for (int i = 0; i < src.cols; ++i)
  102. {
  103. rgb(j, i) = colorTrans::floatToColor<uchar>(src(j, i), val_min, val_max);
  104. }
  105. }
  106. return rgb;
  107. }
  108.  
  109. vector<Mat1f> getHarrisMap(const Mat1b& src, int blockSize, int apertureSize, double k, int maxLevel) {
  110. vector<Mat1b> srcs;
  111. cv::buildPyramid(src, srcs, maxLevel);
  112.  
  113. vector<Mat1f> dsts(srcs.size());
  114.  
  115. for (int n = 0; n < srcs.end() - srcs.begin(); ++n) {
  116. cout << n << endl;
  117.  
  118. // sobel掛けて固有値だす
  119. cornerEigenValsVecs(srcs[n], dsts[n], blockSize, apertureSize, k);
  120. //#ifdef _DEBUG
  121. #if 1
  122. double dbgMax, dbgMin;
  123. cv::minMaxLoc(dsts[n], &dbgMin, &dbgMax);
  124. //cout << "\tMax : " << dbgMax << ", Min : " << dbgMin << endl;
  125.  
  126.  
  127.  
  128. #if 0
  129. Mat3b rgb;
  130. Mat1f tmp = dsts[n].clone();
  131. tmp = tmp - (float)dbgMin;
  132. //tmp = tmp + 128;
  133. tmp = tmp * 255 / (dbgMax - dbgMin);
  134. tmp = tmp + (float)dbgMin * 255 / (dbgMax - dbgMin) + 128;
  135. cv::applyColorMap(Mat1b(tmp), rgb, cv::COLORMAP_JET);
  136. #else
  137. Mat1d tmp = dsts[n];
  138. const double harris_max_thr = 0.05;
  139. const double harris_min_thr = -0.05;
  140. Mat3b rgb = get_vis(tmp, harris_max_thr, harris_min_thr); // 値をカラー表示
  141. #endif
  142. imwrite(format("harrisMap_%03d.png", n), rgb);
  143. //imwrite(format("harrisMap_%03d.png", n), Mat1b(tmp));
  144. imwrite(format("img_layer_%03d.jpg", n), srcs[n]);
  145. #endif
  146.  
  147. }
  148.  
  149. return dsts;
  150. }
  151.  
  152. Mat1b genMask(const vector<Mat1f>& eigenValMaps, const vector<double>& thresholds) {
  153. if (eigenValMaps.empty()) {
  154. Mat1b();
  155. }
  156.  
  157. Mat1b mask = Mat1b::zeros(eigenValMaps[0].size());
  158. //vector<Mat1b> masks(eigenValMaps.size());
  159. for (int n = 0; n < eigenValMaps.size(); ++n) {
  160. Mat1f tmpMask = Mat1f::zeros(eigenValMaps[n].size());
  161. cv::threshold(eigenValMaps[n], tmpMask, thresholds[n], 255, cv::THRESH_BINARY);
  162. Mat1b resizedMask;
  163. cv::resize(Mat1b(tmpMask), resizedMask, mask.size());
  164. //#ifdef _DEBUG
  165. #if 1
  166. imwrite(format("thresholded_%03d.png", n), resizedMask);
  167. #endif
  168. for (int j = 0; j < mask.rows; ++j) {
  169. for (int i = 0; i < mask.cols; ++i) {
  170. if (resizedMask(j, i) > 0) {
  171. mask(j, i) = 255;
  172. }
  173. }
  174. }
  175.  
  176. }
  177.  
  178. return mask;
  179. }
  180.  
  181. void main(int argc, char* argv[]) {
  182. Mat1b src_gray = imread(argv[1], 0);
  183.  
  184. /// Detector parameters
  185. int blockSize = 2;
  186. int apertureSize = 3;
  187. double k = 0.04;
  188. int maxLevel = 4;
  189.  
  190. vector<Mat1f> harrisMap = getHarrisMap(src_gray, blockSize, apertureSize, k, maxLevel);
  191.  
  192. vector<double> thresholds(harrisMap.size());
  193. for (int n = 0; n < thresholds.size(); ++n) {
  194. thresholds[n] = 0.05;
  195. }
  196. Mat1b mask = genMask(harrisMap, thresholds);
  197. if (!mask.empty()) {
  198. imwrite("mask_merged.png", mask);
  199. }
  200. }
  201. }
Add Comment
Please, Sign In to add comment