Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.48 KB | None | 0 0
  1. //contour, convex hull, open/close, dilate/erose
  2.  
  3. #include "opencv2/opencv.hpp"
  4.  
  5. #include <opencv2/core/core.hpp>
  6. #include <opencv2/imgcodecs.hpp>
  7. #include <opencv2/highgui/highgui.hpp>
  8. #include <iostream>
  9. #include <conio.h>
  10.  
  11. #include <threaD>
  12.  
  13. using namespace cv;
  14. using namespace std;
  15.  
  16. //////////////////////////////////
  17. #include "opencv2/imgproc/imgproc.hpp"
  18. #include "opencv2/highgui/highgui.hpp"
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21.  
  22. using namespace cv;
  23.  
  24. /// Global variables
  25. Mat src, erosion_dst, dilation_dst;
  26.  
  27. int erosion_elem = 0;
  28. int erosion_size = 0;
  29. int dilation_elem = 0;
  30. int dilation_size = 0;
  31. int const max_elem = 2;
  32. int const max_kernel_size = 21;
  33.  
  34.  
  35.  
  36. /** @function Erosion */
  37. void Erosion(int, void*)
  38. {
  39. int erosion_type;
  40. if (erosion_elem == 0) { erosion_type = MORPH_RECT; }
  41. else if (erosion_elem == 1) { erosion_type = MORPH_CROSS; }
  42. else if (erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; }
  43.  
  44. Mat element = getStructuringElement(erosion_type,
  45. Size(2 * erosion_size + 1, 2 * erosion_size + 1),
  46. Point(erosion_size, erosion_size));
  47.  
  48. /// Apply the erosion operation
  49. erode(src, erosion_dst, element);
  50. imshow("Erosion Demo", erosion_dst);
  51. }
  52.  
  53. void Detekcia(string cesta)
  54. {
  55. IplImage* img = cvLoadImage(cesta.c_str());
  56.  
  57. //show the original image
  58. /*cvNamedWindow("Raw");
  59. cvShowImage("Raw", img);*/
  60.  
  61. //converting the original image into grayscale
  62. IplImage* imgGrayScale = cvCreateImage(cvGetSize(img), 8, 1);
  63. cvCvtColor(img, imgGrayScale, CV_BGR2GRAY);
  64.  
  65. //thresholding the grayscale image to get better results
  66. cvThreshold(imgGrayScale, imgGrayScale, 128, 255, CV_THRESH_BINARY);
  67.  
  68.  
  69.  
  70. ///////////////////Filter////////////////
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77. CvSeq* contours; //hold the pointer to a contour in the memory block
  78. CvSeq* result; //hold sequence of points of a contour
  79. CvMemStorage *storage = cvCreateMemStorage(0); //storage area for all contours
  80.  
  81. //finding all contours in the image
  82. cvFindContours(imgGrayScale, storage, &contours, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
  83.  
  84. //iterating through each contour
  85. while (contours)
  86. {
  87. //obtain a sequence of points of contour, pointed by the variable 'contour'
  88. result = cvApproxPoly(contours, sizeof(CvContour), storage, CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.02, 0);
  89.  
  90. //if there are 3 vertices in the contour(It should be a triangle)
  91. //if (result->total == 3)
  92. if (result->total == 3 && fabs(cvContourArea(result, CV_WHOLE_SEQ)) < 250000 && fabs(cvContourArea(result, CV_WHOLE_SEQ)) > 100)
  93.  
  94. {
  95. //iterating through each point
  96. CvPoint *pt[3];
  97. for (int i = 0; i < 3; i++) {
  98. pt[i] = (CvPoint*)cvGetSeqElem(result, i);
  99. }
  100.  
  101. //drawing lines around the triangle
  102. cvLine(img, *pt[0], *pt[1], cvScalar(255, 0, 0), 4);
  103. cvLine(img, *pt[1], *pt[2], cvScalar(255, 0, 0), 4);
  104. cvLine(img, *pt[2], *pt[0], cvScalar(255, 0, 0), 4);
  105.  
  106. }
  107.  
  108. //if there are 4 vertices in the contour(It should be a quadrilateral)
  109. //else if (result->total == 4)
  110. else if (result->total == 4 && fabs(cvContourArea(result, CV_WHOLE_SEQ)) < 250000 && fabs(cvContourArea(result, CV_WHOLE_SEQ)) > 100)
  111. {
  112. //iterating through each point
  113. CvPoint *pt[4];
  114. for (int i = 0; i < 4; i++) {
  115. pt[i] = (CvPoint*)cvGetSeqElem(result, i);
  116. }
  117.  
  118. //drawing lines around the quadrilateral
  119. cvLine(img, *pt[0], *pt[1], cvScalar(0, 255, 0), 4);
  120. cvLine(img, *pt[1], *pt[2], cvScalar(0, 255, 0), 4);
  121. cvLine(img, *pt[2], *pt[3], cvScalar(0, 255, 0), 4);
  122. cvLine(img, *pt[3], *pt[0], cvScalar(0, 255, 0), 4);
  123. }
  124.  
  125.  
  126. //if there are 7 vertices in the contour(It should be a heptagon)
  127.  
  128.  
  129. //obtain the next contour
  130. contours = contours->h_next;
  131. }
  132.  
  133. //show the image in which identified shapes are marked
  134. //cvNamedWindow("Tracked");
  135. //cvShowImage("Tracked", img);
  136.  
  137. //cvWaitKey(0); //wait for a key press
  138.  
  139. // //cleaning up
  140. //cvDestroyAllWindows();
  141. //cvReleaseMemStorage(&storage);
  142. //cvReleaseImage(&img);
  143. //cvReleaseImage(&imgGrayScale);
  144.  
  145. //////////////////////////////////////////////////////////
  146. //KRUH
  147. IplImage* gray = cvCreateImage(cvGetSize(img), 8, 1);
  148. //CvMemStorage* storage = cvCreateMemStorage(0);
  149. cvCvtColor(img, gray, CV_BGR2GRAY);
  150. cvSmooth(gray, gray, CV_GAUSSIAN, 9, 9);
  151. CvSeq* circles = cvHoughCircles(gray, storage,
  152. CV_HOUGH_GRADIENT, 2, gray->height / 4, 200, 100);
  153. int i;
  154.  
  155. for (i = 0; i < circles->total; i++)
  156. {
  157. float* p = (float*)cvGetSeqElem(circles, i);
  158. cvCircle(img, cvPoint(cvRound(p[0]), cvRound(p[1])),
  159. 3, CV_RGB(0, 255, 0), -1, 8, 0);
  160. cvCircle(img, cvPoint(cvRound(p[0]), cvRound(p[1])),
  161. cvRound(p[2]), CV_RGB(255, 0, 0), 3, 8, 0);
  162. }
  163. //cvNamedWindow("circles", 1);
  164. cvShowImage("Detection", img);
  165.  
  166.  
  167. //waitKey(0);
  168.  
  169. }
  170.  
  171. /** @function Dilation */
  172. void Dilation(int, void*)
  173. {
  174. int dilation_type;
  175. if (dilation_elem == 0) { dilation_type = MORPH_RECT; }
  176. else if (dilation_elem == 1) { dilation_type = MORPH_CROSS; }
  177. else if (dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; }
  178.  
  179. Mat element = getStructuringElement(dilation_type,
  180. Size(2 * dilation_size + 1, 2 * dilation_size + 1),
  181. Point(dilation_size, dilation_size));
  182. /// Apply the dilation operation
  183. dilate(src, dilation_dst, element);
  184. imshow("Dilation Demo", dilation_dst);
  185. }
  186.  
  187.  
  188.  
  189.  
  190.  
  191. /** Function Headers */
  192. void Erosion(int, void*);
  193. void Dilation(int, void*);
  194.  
  195. ///////////////
  196. /*
  197. int main(int, char**)
  198. {
  199. string cesta = "../data/detekciaIgor.bmp";
  200. Mat src, src_gray;
  201.  
  202. /// Read the image
  203. //src = imread(argv[1], 1);
  204. src= imread(cesta.c_str(), IMREAD_COLOR);
  205.  
  206.  
  207. if (!src.data)
  208. {
  209. return -1;
  210. }
  211.  
  212. /// Convert it to gray
  213. cvtColor(src, src_gray, CV_BGR2GRAY);
  214.  
  215. /// Reduce the noise so we avoid false circle detection
  216. GaussianBlur(src_gray, src_gray, Size(9, 9), 2, 2);
  217.  
  218. vector<Vec3f> circles;
  219.  
  220. /// Apply the Hough Transform to find the circles
  221. HoughCircles(src_gray, circles, CV_HOUGH_GRADIENT, 1, src_gray.rows / 8, 50, 100, 0, 0);
  222.  
  223. /// Draw the circles detected
  224. for (size_t i = 0; i < circles.size(); i++)
  225. {
  226. Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
  227. int radius = cvRound(circles[i][2]);
  228. // circle center
  229. circle(src, center, 3, Scalar(0, 255, 0), -1, 8, 0);
  230. // circle outline
  231. circle(src, center, radius, Scalar(0, 0, 255), 3, 8, 0);
  232. }
  233.  
  234. /// Show your results
  235. namedWindow("Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE);
  236. imshow("Hough Circle Transform Demo", src);
  237.  
  238. waitKey(0);
  239. return 0;
  240. }*/
  241.  
  242. using namespace std;
  243.  
  244. int main()
  245. {
  246. ////-----------FOTENIE-------------
  247. //VideoCapture cap(0); // open the default camera
  248. //if (!cap.isOpened()) // check if we succeeded
  249. // return -1;
  250. //string defaultCesta = "../data/fotkaCislo";
  251. //string cesta;
  252. //int pocetFotiek = 1;
  253.  
  254. //std::string si;
  255. //Mat frame;
  256. //printf("Zacina fotenie\n");
  257. ///*for (int i = 3; i >= 1; i--) {
  258. // printf("%d\n", i);
  259. // std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  260. //}*/
  261. //for (int i = 1; i <= pocetFotiek; i++)
  262. //{
  263. // printf("\nFotka %d\n", i);
  264. // std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  265. // cap >> frame; // get a new frame from camera
  266. // // do any processing
  267. // //imwrite("path/to/image.png", frame);
  268. // si = std::to_string(i);
  269. // cesta = defaultCesta + si + ".bmp";
  270. // imwrite(cesta, frame);
  271. // std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  272. // //if (waitKey(30) >= 0) break; // you can increase delay to 2 seconds here
  273. //}
  274. //// the camera will be deinitialized automatically in VideoCapture destructor
  275.  
  276.  
  277.  
  278.  
  279. //VideoCapture cap(0); // open the default camera
  280. //if (!cap.isOpened()) // check if we succeeded
  281. // return -1;
  282. //Mat edges;
  283. //namedWindow("edges", 1);
  284. //IplImage* imm;
  285. //for (;;)
  286. //{
  287. // Mat frame;
  288. // cap >> imm; // get a new frame from camera
  289. // /*cvtColor(frame, edges, COLOR_BGR2GRAY);
  290. // GaussianBlur(edges, edges, Size(7, 7), 1.5, 1.5);
  291. // Canny(edges, edges, 0, 30, 3);
  292. // imshow("edges", edges);*/
  293. // if (waitKey(30) >= 0) break;
  294. //}
  295.  
  296. ////////////------GALERIA----///////////
  297. string defaultCesta = "../data/fotkaCislo";
  298. string cesta;
  299. int pocetFotiek = 49;
  300.  
  301.  
  302.  
  303. printf("\nPrehravanie galerie\n");
  304. printf("\nOvladanie pismenami 'a' a 'd',\nvypnutie stlacenim q\n");
  305. int index = 1;
  306. std::string si;
  307.  
  308.  
  309. IplImage* img;
  310.  
  311. printf("\nFotka 1\n");
  312. //myImage = imread(filenames[i]);
  313. si = std::to_string(index);
  314. cesta = defaultCesta + si + ".bmp";
  315. Detekcia(cesta);
  316. //img = cvLoadImage(cesta.c_str(), IMREAD_COLOR);
  317. //cvShowImage("Galeria", img);
  318. //int c = cv::waitKey(1);
  319. waitKey(1000);
  320.  
  321. char znak;
  322. while (1) {
  323. znak = _getch();
  324. if ((znak == 'd') && (index < pocetFotiek)) {
  325. index += 1;
  326. printf("\nFotka %d\n", index);
  327. //myImage = imread(filenames[i]);
  328. si = std::to_string(index);
  329. cesta = defaultCesta + si + ".bmp";
  330. Detekcia(cesta);
  331. //img = cvLoadImage(cesta.c_str(), IMREAD_COLOR);
  332. //cvShowImage("Galeria", img);
  333. //int c = cv::waitKey(1);
  334. waitKey(30);
  335. }
  336. if ((znak == 'a') && (index > 1)) {
  337. index -= 1;
  338. printf("\nFotka %d\n", index);
  339. //myImage = imread(filenames[i]);
  340. si = std::to_string(index);
  341. cesta = defaultCesta + si + ".bmp";
  342. Detekcia(cesta);
  343. //img = cvLoadImage(cesta.c_str(), IMREAD_COLOR);
  344. //cvShowImage("Galeria", img);
  345. //int c = cv::waitKey(1);
  346. waitKey(30);
  347. }
  348. if (znak == 'q') {
  349. printf("Koniec");
  350. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  351. break;
  352. }
  353. }
  354.  
  355.  
  356. return 0;
  357. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement