Guest User

Untitled

a guest
Jan 17th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. #include <iostream>
  2. #include "opencv2/opencv.hpp"
  3.  
  4. #define DEBUG_OUTPUT 1
  5.  
  6. using namespace std;
  7. using namespace cv;
  8.  
  9. Mat own_threshold(Mat conf_map, float lower, float upper){
  10. Mat temp1 = conf_map > lower;
  11. Mat temp2 = conf_map < upper;
  12. return temp1 & temp2;
  13. }
  14.  
  15. int main()
  16. {
  17. // Read in images
  18. Mat one = imread("/data/flower/up.jpg");
  19. Mat two = imread("/data/flower/right.jpg");
  20. Mat three = imread("/data/flower/down.jpg");
  21. Mat four = imread("/data/flower/left.jpg");
  22.  
  23. vector<Mat> channels_one, channels_two, channels_three, channels_four;
  24. split(one, channels_one);
  25. split(two, channels_two);
  26. split(three, channels_three);
  27. split(four, channels_four);
  28.  
  29. // Calculate the average value needed for further processing
  30. Mat int1(one.rows, one.cols, CV_32FC1), int2(two.rows, two.cols, CV_32FC1), int3(three.rows, three.cols, CV_32FC1), int4(four.rows, four.cols, CV_32FC1);
  31. for(int row = 0; row < one.rows; row++){
  32. for(int col =0; col < one.cols; col++){
  33. Scalar m1 = mean( Vec3f(channels_one[0].at<uchar>(row,col), channels_one[1].at<uchar>(row,col), channels_one[2].at<uchar>(row,col)) );
  34. Scalar m2 = mean( Vec3f(channels_two[0].at<uchar>(row,col), channels_two[1].at<uchar>(row,col), channels_two[2].at<uchar>(row,col)) );
  35. Scalar m3 = mean( Vec3f(channels_three[0].at<uchar>(row,col), channels_three[1].at<uchar>(row,col), channels_three[2].at<uchar>(row,col)) );
  36. Scalar m4 = mean( Vec3f(channels_four[0].at<uchar>(row,col), channels_four[1].at<uchar>(row,col), channels_four[2].at<uchar>(row,col)) );
  37. int1.at<float>(row, col) = m1[0];
  38. int2.at<float>(row, col) = m2[0];
  39. int3.at<float>(row, col) = m3[0];
  40. int4.at<float>(row, col) = m4[0];
  41. }
  42. }
  43.  
  44. Scalar aint1 = mean(int1); Scalar aint2 = mean(int2); Scalar aint3 = mean(int3); Scalar aint4 = mean(int4);
  45. float avint1 = aint1[0];
  46. float avint2 = aint2[0];
  47. float avint3 = aint3[0];
  48. float avint4 = aint4[0];
  49.  
  50. Scalar aint = mean( Vec4f(avint1, avint2, avint3, avint4) );
  51. float avint = aint[0];
  52.  
  53. // Perform matrix operation on the average data using the average values
  54. // This is for normalizing the intensities
  55. int1 = (avint/avint1) * int1;
  56. int2 = (avint/avint2) * int2;
  57. int3 = (avint/avint3) * int3;
  58. int4 = (avint/avint4) * int4;
  59.  
  60. // Now calculate the ambient images
  61. Mat temp1, temp2, maxint;
  62. max(int1, int2, temp1);
  63. max(int2, int3, temp2);
  64. max(int2, int3, temp2);
  65. max(temp1, temp2, maxint);
  66.  
  67. Mat maxint_viz = maxint/255;
  68.  
  69. imshow("ambient image on mean intensities", maxint_viz); waitKey(0);
  70.  
  71. Mat tempC1, tempC2, maxrgb;
  72. max(one,two, tempC1);
  73. max(three,four, tempC2);
  74. max(tempC1, tempC2, maxrgb);
  75.  
  76. imshow("ambient image on color data", maxrgb); waitKey(0);
  77.  
  78. // Now calculate the ratio images
  79. Mat rad1, rad2, rad3, rad4;
  80. divide(int1, maxint, rad1); //imshow("ratio image up", rad1); waitKey(0);
  81. divide(int2, maxint, rad2); //imshow("ratio image right", rad2); waitKey(0);
  82. divide(int3, maxint, rad3); //imshow("ratio image down", rad3); waitKey(0);
  83. divide(int4, maxint, rad4); //imshow("ratio image left", rad4); waitKey(0);
  84.  
  85. // Apply vertical and horizontal Sobel filters
  86. // Based on orientation of flash
  87. Mat edge1, edge2, edge3, edge4;
  88. Mat rad1b, rad2b, rad3b, rad4b;
  89. Sobel(rad1, edge1, CV_32F, 1, 0, -1); // Vertical Sobel
  90. Sobel(rad2, edge2, CV_32F, 0, 1, -1); // Horizontal Sobel
  91. Sobel(rad3, edge3, CV_32F, 1, 0, -1); // Vertical Sobel
  92. Sobel(rad4, edge4, CV_32F, 0, 1, -1); // Horizontal Sobel
  93.  
  94. // Calculation of negative transitions
  95. Mat mask1, mask2, mask3, mask4;
  96. Mat mask1f, mask2f, mask3f, mask4f;
  97.  
  98. mask1 = edge1 > 0; mask1.convertTo(mask1f, CV_32FC1); mask1f = mask1f/255;
  99. multiply(edge1, mask1f, edge1);
  100.  
  101. mask2 = edge2 < 0; mask2.convertTo(mask2f, CV_32FC1); mask2f = mask2f/255;
  102. multiply(edge2, mask2f, edge2);
  103. edge2 = abs(edge2);
  104.  
  105. mask3 = edge3 < 0; mask3.convertTo(mask3f, CV_32FC1); mask3f = mask3f/255;
  106. multiply(edge3, mask3f, edge3);
  107. edge3 = abs(edge3);
  108.  
  109. mask4 = edge4 > 0; mask4.convertTo(mask4f, CV_32FC1); mask4f = mask4f/255;
  110. multiply(edge4, mask4f, edge4);
  111.  
  112. imshow("edge map up", edge1); waitKey(0);
  113. imshow("edge map right", edge2); waitKey(0);
  114. imshow("edge map down", edge3); waitKey(0);
  115. imshow("edge map left", edge4); waitKey(0);
  116.  
  117. // Now we want all edges combined into a single edge map by taking the maximal value of each map
  118. Mat tempConf1, tempConf2, conf;
  119. max(edge1, edge2, tempConf1);
  120. max(edge3, edge4, tempConf2);
  121. max(tempConf1, tempConf2, conf);
  122.  
  123. imshow("confidence map", conf); waitKey(0);
  124.  
  125. // Threshold to achieve a cleaner confidence map
  126. edge1 = own_threshold(edge1, 0.5, 1.0);
  127. edge2 = own_threshold(edge2, 0.5, 1.0);
  128. edge3 = own_threshold(edge3, 0.5, 1.0);
  129. edge4 = own_threshold(edge4, 0.5, 1.0);
  130.  
  131. Mat edges = edge1 | edge2 | edge3 | edge4;
  132.  
  133. imshow("edge map up - binarized", edge1); waitKey(0);
  134. imshow("edge map right - binarized", edge2); waitKey(0);
  135. imshow("edge map down - binarized", edge3); waitKey(0);
  136. imshow("edge map left - binarized", edge4); waitKey(0);
  137. imshow("edge map binarized combined", edges); waitKey(0);
  138.  
  139. // Calculate texture edges
  140. Mat tedges;
  141. Sobel(maxint, tedges, CV_32F, 1, 1); // Combined into one call in OpenCV while Matlab code does 2 calls --> same output
  142. tedges = own_threshold(tedges/255, 0.3, 0.6);
  143. imshow("texture edges map", tedges); waitKey(0);
  144.  
  145. // Remove depth
  146. tedges = tedges - edges;
  147. imshow("texture edges map - depth removed", tedges); waitKey(0);
  148.  
  149. // All edges
  150. Mat alledges = edges + tedges;
  151. bitwise_not(alledges,alledges);
  152.  
  153. imshow("all edges", alledges); waitKey(0);
  154.  
  155. Mat canvas = maxrgb.clone();
  156. alledges.convertTo(alledges, CV_8UC1);
  157. // Blend the edge map with the original image
  158. for(int row = 0; row < alledges.rows; row++){
  159. for(int col =0; col < alledges.cols; col++){
  160. if(alledges.at<uchar>(row, col) == 0){
  161. canvas.at<Vec3b>(row, col) = Vec3b(0,0,0);
  162. }
  163. }
  164. }
  165. imshow("final", canvas); waitKey(0);
  166.  
  167. return 0;
  168. }
Add Comment
Please, Sign In to add comment