algorithm.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #include "algorithm.h"
  2. #include "algorithmregistry.h"
  3. CannyEdge::CannyEdge() {
  4. cannyMeta.name = "Canny Edge Detection";
  5. cannyMeta.addParam("Threshold 1", "double", 50.0, 0.0, 255.0);
  6. cannyMeta.addParam("Threshold 2", "double", 150.0, 0.0, 255.0);
  7. cannyMeta.addParam("Aperture Size", "int", 3, 1, 7);
  8. cannyMeta.addParam("L2 Gradient", "bool", false, 0.0, 1.0);
  9. }
  10. void CannyEdge::process(cv::Mat image) {
  11. // Store input parameters
  12. runtimeParams.inputimage = image.clone(); // Store input image
  13. // Get parameters from cannyMeta
  14. double threshold1 = 50.0;
  15. double threshold2 = 150.0;
  16. int apertureSize = 3;
  17. bool l2gradient = false;
  18. for (const auto& param : cannyMeta.params) {
  19. if (param.name == "Threshold 1") threshold1 = param.value.toDouble();
  20. if (param.name == "Threshold 2") threshold2 = param.value.toDouble();
  21. if (param.name == "Aperture Size") apertureSize = param.value.toInt();
  22. if (param.name == "L2 Gradient") l2gradient = param.value.toBool();
  23. }
  24. cv::Mat gray;
  25. if (image.channels() == 3) {
  26. cv::cvtColor(image, gray, cv::COLOR_BGR2GRAY);
  27. } else {
  28. gray = image;
  29. }
  30. cv::Canny(gray, image, threshold1, threshold2, apertureSize, l2gradient);
  31. // Store output parameters
  32. runtimeParams.outimage = image.clone(); // Store output image
  33. // Count edges (simple implementation)
  34. runtimeParams.edgeCount = cv::countNonZero(image);
  35. }
  36. AlgorithmMeta CannyEdge::getParams() {
  37. return cannyMeta;
  38. }
  39. void CannyEdge::setParams(const AlgorithmMeta& params) {
  40. cannyMeta = params;
  41. }
  42. Thresholding::Thresholding() {
  43. AlgoritName = "Thresholding";
  44. Thresh = 127;
  45. MaxVal = 255;
  46. Type = 0;
  47. }
  48. void Thresholding::process(cv::Mat image) {
  49. // Store input parameters
  50. runtimeParams.inputimage = image.clone(); // Store input image
  51. // Convert to grayscale image
  52. cv::Mat gray;
  53. if (image.channels() == 3) {
  54. cv::cvtColor(image, gray, cv::COLOR_BGR2GRAY);
  55. } else {
  56. gray = image;
  57. }
  58. // Execute thresholding
  59. cv::threshold(gray, image, Thresh, MaxVal, Type);
  60. // Count white pixels
  61. runtimeParams.whitePixelCount = cv::countNonZero(image);
  62. // Store output parameters
  63. runtimeParams.outimage = image.clone(); // Store output image
  64. }
  65. AlgorithmMeta Thresholding::getParams() {
  66. AlgorithmMeta p;
  67. p.name = QString::fromStdString(AlgoritName);
  68. p.addParam("Threshold", "int", Thresh, 0, 255);
  69. p.addParam("Max Value", "int", MaxVal, 0, 255);
  70. p.addParam("Threshold Type", "int", Type, 0, 4);
  71. return p;
  72. }
  73. void Thresholding::setParams(const AlgorithmMeta& params) {
  74. AlgoritName = params.name.toStdString();
  75. for (auto& param : params.params) {
  76. if (param.name == "Threshold") Thresh = param.value.toInt();
  77. if (param.name == "Max Value") MaxVal = param.value.toInt();
  78. if (param.name == "Threshold Type") Type = param.value.toInt();
  79. }
  80. }
  81. GaussianBlur::GaussianBlur() {
  82. AlgoritName = "Gaussian Blur";
  83. KernelSize = 5;
  84. SigmaX = 1.5;
  85. SigmaY = 0.0;
  86. }
  87. void GaussianBlur::process(cv::Mat image) {
  88. // Store input parameters
  89. runtimeParams.inputimage = image.clone(); // Store input image
  90. // Ensure kernel size is odd
  91. int ksize = KernelSize;
  92. if (ksize % 2 == 0) {
  93. ksize++;
  94. }
  95. // Execute Gaussian blur
  96. cv::GaussianBlur(image, image, cv::Size(ksize, ksize), SigmaX, SigmaY);
  97. // Store output parameters
  98. runtimeParams.outimage = image.clone(); // Store output image
  99. }
  100. AlgorithmMeta GaussianBlur::getParams() {
  101. AlgorithmMeta p;
  102. p.name = QString::fromStdString(AlgoritName);
  103. p.addParam("Kernel Size", "int", KernelSize, 1, 31);
  104. p.addParam("Sigma X", "double", SigmaX, 0.0, 10.0);
  105. p.addParam("Sigma Y", "double", SigmaY, 0.0, 10.0);
  106. return p;
  107. }
  108. void GaussianBlur::setParams(const AlgorithmMeta& params) {
  109. AlgoritName = params.name.toStdString();
  110. for (auto& param : params.params) {
  111. if (param.name == "Kernel Size") KernelSize = param.value.toInt();
  112. if (param.name == "Sigma X") SigmaX = param.value.toDouble();
  113. if (param.name == "Sigma Y") SigmaY = param.value.toDouble();
  114. }
  115. }
  116. MedianBlur::MedianBlur() {
  117. AlgoritName = "Median Blur";
  118. KernelSize = 5;
  119. }
  120. void MedianBlur::process(cv::Mat image) {
  121. // Store input parameters
  122. runtimeParams.inputimage = image.clone(); // Store input image
  123. // Ensure kernel size is odd
  124. int ksize = KernelSize;
  125. if (ksize % 2 == 0) {
  126. ksize++;
  127. }
  128. // Execute median blur
  129. cv::medianBlur(image, image, ksize);
  130. // Store output parameters
  131. runtimeParams.outimage = image.clone(); // Store output image
  132. }
  133. AlgorithmMeta MedianBlur::getParams() {
  134. AlgorithmMeta p;
  135. p.name = QString::fromStdString(AlgoritName);
  136. p.addParam("Kernel Size", "int", KernelSize, 1, 31);
  137. return p;
  138. }
  139. void MedianBlur::setParams(const AlgorithmMeta& params) {
  140. AlgoritName = params.name.toStdString();
  141. for (auto& param : params.params) {
  142. if (param.name == "Kernel Size") KernelSize = param.value.toInt();
  143. }
  144. }
  145. Dilate::Dilate() {
  146. AlgoritName = "Dilate";
  147. KernerSize = 3;
  148. Iterations = 1;
  149. }
  150. void Dilate::process(cv::Mat image) {
  151. // Store input parameters
  152. runtimeParams.inputimage = image.clone(); // Store input image
  153. // Create structuring element
  154. cv::Mat kernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(KernerSize, KernerSize));
  155. // Execute dilation
  156. cv::dilate(image, image, kernel, cv::Point(-1, -1), Iterations);
  157. // Store output parameters
  158. runtimeParams.outimage = image.clone(); // Store output image
  159. }
  160. AlgorithmMeta Dilate::getParams() {
  161. AlgorithmMeta p;
  162. p.name = QString::fromStdString(AlgoritName);
  163. p.addParam("Kernel Size", "int", KernerSize, 0, 100);
  164. p.addParam("Iterations", "int", Iterations, 0, 100);
  165. return p;
  166. }
  167. void Dilate::setParams(const AlgorithmMeta& params) {
  168. AlgoritName = params.name.toStdString();
  169. for (auto& param : params.params) {
  170. if (param.name == "Kernel Size") KernerSize = param.value.toInt();
  171. if (param.name == "Iterations") Iterations = param.value.toInt();
  172. }
  173. }
  174. Erode::Erode() {
  175. AlgoritName = "Erode";
  176. KernerSize = 3;
  177. Iterations = 1;
  178. }
  179. void Erode::process(cv::Mat image) {
  180. // Store input parameters
  181. runtimeParams.inputimage = image.clone(); // Store input image
  182. // Create structuring element
  183. cv::Mat kernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(KernerSize, KernerSize));
  184. // Execute erosion
  185. cv::erode(image, image, kernel, cv::Point(-1, -1), Iterations);
  186. // Store output parameters
  187. runtimeParams.outimage = image.clone(); // Store output image
  188. }
  189. AlgorithmMeta Erode::getParams() {
  190. AlgorithmMeta p;
  191. p.name = QString::fromStdString(AlgoritName);
  192. p.addParam("Kernel Size", "int", KernerSize, 0, 100);
  193. p.addParam("Iterations", "int", Iterations, 0, 100);
  194. return p;
  195. }
  196. void Erode::setParams(const AlgorithmMeta& params) {
  197. AlgoritName = params.name.toStdString();
  198. for (auto& param : params.params) {
  199. if (param.name == "Kernel Size") KernerSize = param.value.toInt();
  200. if (param.name == "Iterations") Iterations = param.value.toInt();
  201. }
  202. }
  203. Grayscale::Grayscale() {
  204. AlgoritName = "Grayscale";
  205. }
  206. void Grayscale::process(cv::Mat image) {
  207. // Store input parameters
  208. runtimeParams.inputimage = image.clone(); // Store input image
  209. // Execute grayscale conversion
  210. if (image.channels() == 3) {
  211. cv::cvtColor(image, image, cv::COLOR_BGR2GRAY);
  212. }
  213. // Store output parameters
  214. runtimeParams.outimage = image.clone(); // Store output image
  215. }
  216. AlgorithmMeta Grayscale::getParams() {
  217. AlgorithmMeta p;
  218. p.name = QString::fromStdString(AlgoritName);
  219. return p;
  220. }
  221. void Grayscale::setParams(const AlgorithmMeta& params) {
  222. AlgoritName = params.name.toStdString();
  223. }
  224. HistogramEqualization::HistogramEqualization() {
  225. AlgoritName = "Histogram Equalization";
  226. }
  227. void HistogramEqualization::process(cv::Mat image) {
  228. // Store input parameters
  229. runtimeParams.inputimage = image.clone(); // Store input image
  230. // Convert to grayscale image
  231. cv::Mat gray;
  232. if (image.channels() == 3) {
  233. cv::cvtColor(image, gray, cv::COLOR_BGR2GRAY);
  234. } else {
  235. gray = image;
  236. }
  237. // Execute histogram equalization
  238. cv::equalizeHist(gray, image);
  239. // Store output parameters
  240. runtimeParams.outimage = image.clone(); // Store output image
  241. }
  242. AlgorithmMeta HistogramEqualization::getParams() {
  243. AlgorithmMeta p;
  244. p.name = QString::fromStdString(AlgoritName);
  245. return p;
  246. }
  247. void HistogramEqualization::setParams(const AlgorithmMeta& params) {
  248. AlgoritName = params.name.toStdString();
  249. }
  250. FindContours::FindContours() {
  251. AlgoritName = "Find Contours";
  252. Mode = 3; // RETR_TREE
  253. Method = 2; // CHAIN_APPROX_SIMPLE
  254. }
  255. void FindContours::process(cv::Mat image) {
  256. // Store input parameters
  257. runtimeParams.inputimage = image.clone(); // Store input image
  258. // Convert to grayscale image
  259. cv::Mat gray;
  260. if (image.channels() == 3) {
  261. cv::cvtColor(image, gray, cv::COLOR_BGR2GRAY);
  262. } else {
  263. gray = image;
  264. }
  265. // Binarization
  266. cv::Mat binary;
  267. cv::threshold(gray, binary, 127, 255, cv::THRESH_BINARY);
  268. // Find contours
  269. std::vector<std::vector<cv::Point>> contours;
  270. std::vector<cv::Vec4i> hierarchy;
  271. cv::findContours(binary, contours, hierarchy, Mode, Method);
  272. // Draw contours
  273. cv::Mat result = cv::Mat::zeros(image.size(), CV_8UC3);
  274. cv::drawContours(result, contours, -1, cv::Scalar(0, 255, 0), 2);
  275. // Convert result back to grayscale if needed
  276. if (image.channels() == 1) {
  277. cv::cvtColor(result, image, cv::COLOR_BGR2GRAY);
  278. } else {
  279. image = result;
  280. }
  281. runtimeParams.contourCount = static_cast<int>(contours.size());
  282. // Store output parameters
  283. runtimeParams.outimage = image.clone(); // Store output image
  284. }
  285. AlgorithmMeta FindContours::getParams() {
  286. AlgorithmMeta p;
  287. p.name = QString::fromStdString(AlgoritName);
  288. p.addParam("Retrieval Mode", "int", Mode, 0, 4);
  289. p.addParam("Approx Method", "int", Method, 0, 3);
  290. return p;
  291. }
  292. void FindContours::setParams(const AlgorithmMeta& params) {
  293. AlgoritName = params.name.toStdString();
  294. for (auto& param : params.params) {
  295. if (param.name == "Retrieval Mode") Mode = param.value.toInt();
  296. if (param.name == "Approx Method") Method = param.value.toInt();
  297. }
  298. }