Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. my code :
  2. {
  3. string filename("g:/lib/opencv/samples/data/lena.jpg");
  4. Mat _I = imread(filename.c_str(), IMREAD_COLOR);
  5.  
  6. cv::Mat I = _I;
  7.  
  8. if (I.depth())
  9. cv::normalize(I, I, 0., 255., cv::NORM_MINMAX, CV_8U);
  10.  
  11. if (I.channels() == 3)
  12. {
  13. cv::Mat Lab, tmp;
  14. std::vector<cv::Mat> cns;
  15. cv::cvtColor(I, Lab, cv::COLOR_BGR2Lab);
  16. cv::decolor(Lab, I, tmp);
  17.  
  18. }
  19.  
  20. cv::Mat hist;
  21.  
  22. cv::calcHist(std::vector<cv::Mat>(1, I), { 0 }, cv::noArray(), hist, { 256 }, { 0.f,256.f });
  23.  
  24.  
  25. cv::Mat hd;
  26.  
  27. {
  28. cv::Mat curr = hist.rowRange(0, hist.rows - 1);
  29. cv::Mat next = hist.rowRange(1, hist.rows);
  30.  
  31. cv::absdiff(next, curr, hd);
  32. }
  33.  
  34. hd.convertTo(hd, CV_32S);
  35.  
  36. double thresh = cv::mean(hd)(0);
  37. // cout << utils::iqr(hd)(0);
  38. hd = hd > thresh;
  39.  
  40. cv::Mat1i idx;
  41.  
  42. int i = 0;
  43. for (auto it_current = hd.begin<uchar>(), it_next = hd.begin<uchar>() + 1; it_next != hd.end<uchar>(); it_current++, it_next++, i++)
  44. {
  45. uchar current = *it_current;
  46. uchar next = *it_next;
  47.  
  48. if (current && !next)
  49. idx.push_back(i);
  50. if (!current && next)
  51. idx.push_back(i + 1);
  52. }
  53.  
  54. std::cout << idx << std::endl;
  55. }
  56. output in c++
  57. [9;
  58. 9;
  59. 11;
  60. 14;
  61. 16;
  62. 20;
  63. 22;
  64. 24;
  65. 26;
  66. 26;
  67. 28;
  68. 30;
  69. 33;
  70. 34;
  71. 38;
  72. 39;
  73. 44;
  74. 44;
  75. 46;
  76. 46;
  77. 54;
  78. 54;
  79. 56;
  80. 57;
  81. 61;
  82. 63;
  83. 65;
  84. 65;
  85. 67;
  86. 69;
  87. 71;
  88. 71;
  89. 73;
  90. 73;
  91. 75;
  92. 75;
  93. 79;
  94. 81;
  95. 83;
  96. 91;
  97. 94;
  98. 95;
  99. 98;
  100. 98;
  101. 100;
  102. 105;
  103. 112;
  104. 114;
  105. 117;
  106. 118;
  107. 121;
  108. 121;
  109. 123;
  110. 124;
  111. 127;
  112. 127;
  113. 129;
  114. 133;
  115. 135;
  116. 136;
  117. 138;
  118. 143;
  119. 151;
  120. 151;
  121. 158;
  122. 159;
  123. 164;
  124. 164;
  125. 188;
  126. 188;
  127. 193;
  128. 195;
  129. 204;
  130. 204;
  131. 209;
  132. 209;
  133. 211;
  134. 211;
  135. 213;
  136. 213;
  137. 216;
  138. 216]
  139. ******************************
  140.  
  141. my python code :
  142.  
  143. imgpath="g:\\lib\\opencv\\samples\\data\\lena.jpg"
  144. I = cv2.imread(imgpath,cv2.IMREAD_COLOR)
  145. I, _ = cv2.decolor(cv2.cvtColor(I, cv2.COLOR_BGR2Lab))
  146. h = cv2.calcHist([I],[0],None,[256],[0,256]).astype(np.int32).reshape((-1,))
  147.  
  148.  
  149.  
  150. print(h.dtype, I.dtype, I.shape, h.min(), h.max(), I.min(), I.max())
  151.  
  152. hd = np.abs(np.diff(h))
  153.  
  154.  
  155. thresh = np.mean(hd)
  156.  
  157. print(thresh, np.mean(hd), iqr(hd),flush=True)
  158.  
  159. hd = hd > thresh
  160.  
  161. print(hd)
  162.  
  163. idx = list()
  164.  
  165. for i in range(254):
  166. if hd[i] and not hd[i+1]:
  167. idx.append(i)
  168. elif not hd[i] and hd[i+1]:
  169. idx.append(i+1)
  170. print(idx)
  171.  
  172. python output
  173. [9, 9, 11, 14, 16, 20, 22, 24, 26, 26, 28, 30, 33, 34, 38, 39, 44, 44, 46, 46, 54, 54, 56, 57, 61, 63, 65, 65, 67, 69, 71, 71, 73, 73, 75, 75, 79, 81, 83, 91, 94, 95, 98, 98, 100, 105, 112, 114, 117, 118, 121, 121, 123, 124, 127, 127, 129, 133, 135, 136, 138, 143, 151, 151, 158, 159, 164, 164, 188, 188, 193, 195, 204, 204, 209, 209, 211, 211, 213, 213, 216, 216]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement