Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sys/stat.h>
  4.  
  5. #include <rapidjson/document.h>
  6. #include <rapidjson/filereadstream.h>
  7. #include <cmath>
  8.  
  9. #include "opencv2/core/core.hpp"
  10. #include "opencv2/imgproc/imgproc.hpp"
  11. #include "opencv2/highgui/highgui.hpp"
  12.  
  13. using std::string;
  14.  
  15. #define FULL_VERSION 1
  16.  
  17.  
  18. /*
  19. TODO: Applies the oil painting effect on the original image:
  20.  
  21. - for each pixel create an intensity histogram, and histograms for red,
  22. green and blue as specified in the assignment document
  23. - get the bin and value of the most common bin found in the intensity
  24. histogram
  25. - use the previous findings to calculate the color of a given pixel
  26.  
  27. @param original_image (in) the original image
  28. @param radius (in) the search window for neighbourhood pixels
  29. @param levels (in) the amount of levels used in the oil
  30. painting effect algorithm
  31. @return the image with the effect applied
  32. */
  33.  
  34. cv::Mat getFilteredImage(const cv::Mat &original_image,
  35. int radius,
  36. int levels)
  37. {
  38.  
  39. cv::Mat output = cv::Mat::zeros(original_image.size(),
  40. original_image.type());
  41. int SG [levels+1];
  42. int SR [levels+1];
  43. int SB [levels+1];
  44. int intensityCount [levels];
  45. int curMax = 0;
  46. int finalR;
  47. int finalG;
  48. int finalB;
  49. int maxIndex=0;
  50. int intensityLevels=0;
  51. int curIntensity=0;
  52. int R,G,B;
  53.  
  54.  
  55.  
  56. for (int i=0 ; i<original_image.rows ; i++)
  57. {
  58. for(int j=0 ; j<original_image.cols; j++)
  59. {
  60. for(int ctr = 0; ctr<levels+1 ; ctr++)
  61. {
  62. SG[ctr]=0;
  63. SR[ctr]=0;
  64. SB[ctr]=0;
  65. }
  66. for(int f = 0; f < levels; f++)
  67. {
  68. intensityCount[f] = 0;
  69. }
  70. int fromX = i - radius;
  71. if(i < 0)
  72. fromX = 0;
  73. int toX = radius + i;
  74. if(toX > original_image.rows-1)
  75. toX = original_image.rows-1;
  76. int toY = radius+j;
  77. if(toY > original_image.cols-1)
  78. toY = original_image.cols -1;
  79. int fromY = j - radius;
  80. if(fromY < 0)
  81. fromY = 0;
  82. for(int x=fromX; x != toX+1 ; x++)
  83. {
  84.  
  85. for(int y=fromY; y != toY+1; y++)
  86. {
  87.  
  88.  
  89. B=original_image.at<cv::Vec3b>(x,y)[0];
  90. G=original_image.at<cv::Vec3b>(x,y)[1];
  91. R=original_image.at<cv::Vec3b>(x,y)[2];
  92.  
  93.  
  94. int varijabla=R+G+B;
  95. curIntensity = floor((varijabla *levels) / (765.0));
  96. intensityCount[curIntensity]+=1;
  97. SR[curIntensity] += R;
  98. SG[curIntensity] += G;
  99. SB[curIntensity] += B;
  100.  
  101. }
  102. }
  103. int curMax = 0;
  104. int maxIndex = 0;
  105. for (int counter=0 ; counter<levels; counter++)
  106. {
  107. if (intensityCount[counter] > curMax)
  108. {
  109. curMax = intensityCount[counter];
  110. maxIndex = counter;
  111. }
  112. else if(intensityCount[counter] == curMax)
  113. {
  114. maxIndex=counter;
  115. }
  116. }
  117.  
  118. finalR = SR[maxIndex] / curMax;
  119. finalG = SG[maxIndex] / curMax;
  120. finalB = SB[maxIndex] / curMax;
  121.  
  122. output.at<cv::Vec3b>(i,j)[0] = finalB;
  123. output.at<cv::Vec3b>(i,j)[1] = finalG;
  124. output.at<cv::Vec3b>(i,j)[2] = finalR;
  125.  
  126.  
  127.  
  128.  
  129.  
  130. }
  131. }
  132.  
  133.  
  134.  
  135.  
  136. return output;
  137. }
  138.  
  139.  
  140. /*
  141. TODO: Computes a binary mask for foreground segmentation in the specified
  142. location:
  143.  
  144. - convert the original image into the HSV color space using CV_BGR2HSV
  145. - use the value of H and the given threshold to create the binary
  146. foreground mask in the specified location
  147. - use dilation to expand the mask, for the structuring element use
  148. cv::MORPH_ELLIPSE and dilation_size
  149.  
  150. @param original_image (in) the original image
  151. @param location (in) the location bounding box where the
  152. segmentation needs to be performed
  153. @param threshold (in) threshold for the H channel used during
  154. the segmentation
  155. @return a binary mask where a logic 1 represents
  156. the foreground, while a logic 0
  157. represents the background
  158. */
  159.  
  160. cv::Mat getForegroundMask(const cv::Mat &original_image,
  161. const cv::Rect &location,
  162. int threshold,
  163. int dilation_size)
  164. {
  165. cv::Mat foreground_mask = cv::Mat::zeros(original_image.size(), CV_8UC1);
  166.  
  167.  
  168.  
  169.  
  170.  
  171. return foreground_mask;
  172. }
  173.  
  174.  
  175. /*
  176. TODO: Combines the oil-painted image and the original image using the
  177. foreground segmentation mask:
  178.  
  179. - combine the filtered image and the original image using the
  180. foreground mask
  181. - for the combined image: if a pixel in the foreground mask is set to 1
  182. use the original image, otherwise use the filtered image
  183.  
  184. @param original_image (in) the original image
  185. @param filtered_image (in) the filtered image
  186. @param foreground_mask (in) the binary segmentation mask
  187. @return the combined image
  188. */
  189.  
  190. cv::Mat combineImages(const cv::Mat &original_image,
  191. const cv::Mat &filtered_image,
  192. const cv::Mat &foreground_mask)
  193. {
  194. cv::Mat output = cv::Mat::zeros(original_image.size(),
  195. original_image.type());
  196.  
  197.  
  198. return output;
  199. }
  200.  
  201. /*
  202. Prints the usage message to std::cout. Do not change!
  203. @return always 1
  204. */
  205. int readme()
  206. {
  207. std::cout << "Usage: ./cvtask1a <config-file>" << std::endl;
  208. return 1;
  209. }
  210.  
  211. /*
  212. Framework main method. Do not change!
  213. @return program status
  214. */
  215. int main(int argc, char** argv)
  216. {
  217. // DO NOT REMOVE THIS LINE!!!
  218. std::cout << "CV/task1a framework version 1.0" << std::endl;
  219. if (argc != 2)
  220. return readme();
  221.  
  222. try
  223. {
  224.  
  225. rapidjson::Document cfg;
  226. FILE* fp = fopen(argv[1], "r");
  227. char readBuffer[65536];
  228. rapidjson::FileReadStream is(fp, readBuffer, sizeof(readBuffer));
  229.  
  230. cfg.ParseStream(is);
  231. assert(cfg.IsObject());
  232.  
  233. //param parsing
  234. string input_path = cfg["input_path"].GetString();
  235. string output_path = cfg["output_path"].GetString();
  236. int radius = cfg["radius"].GetInt();
  237. int levels = cfg["levels"].GetInt();
  238. int threshold = cfg["threshold"].GetInt();
  239. int dilation_size = cfg["dilation_size"].GetInt();
  240. int label_x = cfg["label_x"].GetInt();
  241. int label_y = cfg["label_y"].GetInt();
  242. int label_width = cfg["label_width"].GetInt();
  243. int label_height = cfg["label_height"].GetInt();
  244.  
  245. #if defined(_WIN32)
  246. _mkdir("./output/");
  247. _mkdir(output_path.c_str());
  248. #else
  249. mkdir("./output/", 0777);
  250. mkdir(output_path.c_str(),0777);
  251. #endif
  252.  
  253. std::cout << "Running TC: " << argv[1] << std::endl;
  254.  
  255. cv::Mat original_image = cv::imread(input_path, CV_LOAD_IMAGE_COLOR);
  256. cv::Mat foreground_mask = getForegroundMask(original_image,
  257. cv::Rect(label_x,
  258. label_y,
  259. label_width,
  260. label_height),
  261. threshold,
  262. dilation_size);
  263.  
  264. cv::Mat filtered_image = getFilteredImage(original_image,
  265. radius,
  266. levels);
  267.  
  268. cv::Mat final_image = combineImages(original_image,
  269. filtered_image,
  270. foreground_mask);
  271.  
  272. cv::imwrite(output_path + "filtered.png", filtered_image);
  273. cv::imwrite(output_path + "mask.png", foreground_mask * 255);
  274. cv::imwrite(output_path + "final.png", final_image);
  275.  
  276.  
  277. }
  278. catch (std::exception &ex)
  279. {
  280. std::cout << "An exception occured:" << std::endl;
  281. std::cout << ex.what() << std::endl;
  282. return -2;
  283. }
  284. return 0;
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement