Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //contour, convex hull, open/close, dilate/erose
- #include "opencv2/opencv.hpp"
- #include <opencv2/core/core.hpp>
- #include <opencv2/imgcodecs.hpp>
- #include <opencv2/highgui/highgui.hpp>
- #include <iostream>
- #include <conio.h>
- #include <threaD>
- using namespace cv;
- using namespace std;
- //////////////////////////////////
- #include "opencv2/imgproc/imgproc.hpp"
- #include "opencv2/highgui/highgui.hpp"
- #include <stdlib.h>
- #include <stdio.h>
- using namespace cv;
- /// Global variables
- Mat src, erosion_dst, dilation_dst;
- int erosion_elem = 0;
- int erosion_size = 0;
- int dilation_elem = 0;
- int dilation_size = 0;
- int const max_elem = 2;
- int const max_kernel_size = 21;
- /** @function Erosion */
- void Erosion(int, void*)
- {
- int erosion_type;
- if (erosion_elem == 0) { erosion_type = MORPH_RECT; }
- else if (erosion_elem == 1) { erosion_type = MORPH_CROSS; }
- else if (erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; }
- Mat element = getStructuringElement(erosion_type,
- Size(2 * erosion_size + 1, 2 * erosion_size + 1),
- Point(erosion_size, erosion_size));
- /// Apply the erosion operation
- erode(src, erosion_dst, element);
- imshow("Erosion Demo", erosion_dst);
- }
- void Detekcia(string cesta)
- {
- IplImage* img = cvLoadImage(cesta.c_str());
- //show the original image
- /*cvNamedWindow("Raw");
- cvShowImage("Raw", img);*/
- //converting the original image into grayscale
- IplImage* imgGrayScale = cvCreateImage(cvGetSize(img), 8, 1);
- cvCvtColor(img, imgGrayScale, CV_BGR2GRAY);
- //thresholding the grayscale image to get better results
- cvThreshold(imgGrayScale, imgGrayScale, 128, 255, CV_THRESH_BINARY);
- ///////////////////Filter////////////////
- CvSeq* contours; //hold the pointer to a contour in the memory block
- CvSeq* result; //hold sequence of points of a contour
- CvMemStorage *storage = cvCreateMemStorage(0); //storage area for all contours
- //finding all contours in the image
- cvFindContours(imgGrayScale, storage, &contours, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
- //iterating through each contour
- while (contours)
- {
- //obtain a sequence of points of contour, pointed by the variable 'contour'
- result = cvApproxPoly(contours, sizeof(CvContour), storage, CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.02, 0);
- //if there are 3 vertices in the contour(It should be a triangle)
- //if (result->total == 3)
- if (result->total == 3 && fabs(cvContourArea(result, CV_WHOLE_SEQ)) < 250000 && fabs(cvContourArea(result, CV_WHOLE_SEQ)) > 100)
- {
- //iterating through each point
- CvPoint *pt[3];
- for (int i = 0; i < 3; i++) {
- pt[i] = (CvPoint*)cvGetSeqElem(result, i);
- }
- //drawing lines around the triangle
- cvLine(img, *pt[0], *pt[1], cvScalar(255, 0, 0), 4);
- cvLine(img, *pt[1], *pt[2], cvScalar(255, 0, 0), 4);
- cvLine(img, *pt[2], *pt[0], cvScalar(255, 0, 0), 4);
- }
- //if there are 4 vertices in the contour(It should be a quadrilateral)
- //else if (result->total == 4)
- else if (result->total == 4 && fabs(cvContourArea(result, CV_WHOLE_SEQ)) < 250000 && fabs(cvContourArea(result, CV_WHOLE_SEQ)) > 100)
- {
- //iterating through each point
- CvPoint *pt[4];
- for (int i = 0; i < 4; i++) {
- pt[i] = (CvPoint*)cvGetSeqElem(result, i);
- }
- //drawing lines around the quadrilateral
- cvLine(img, *pt[0], *pt[1], cvScalar(0, 255, 0), 4);
- cvLine(img, *pt[1], *pt[2], cvScalar(0, 255, 0), 4);
- cvLine(img, *pt[2], *pt[3], cvScalar(0, 255, 0), 4);
- cvLine(img, *pt[3], *pt[0], cvScalar(0, 255, 0), 4);
- }
- //if there are 7 vertices in the contour(It should be a heptagon)
- //obtain the next contour
- contours = contours->h_next;
- }
- //show the image in which identified shapes are marked
- //cvNamedWindow("Tracked");
- //cvShowImage("Tracked", img);
- //cvWaitKey(0); //wait for a key press
- // //cleaning up
- //cvDestroyAllWindows();
- //cvReleaseMemStorage(&storage);
- //cvReleaseImage(&img);
- //cvReleaseImage(&imgGrayScale);
- //////////////////////////////////////////////////////////
- //KRUH
- IplImage* gray = cvCreateImage(cvGetSize(img), 8, 1);
- //CvMemStorage* storage = cvCreateMemStorage(0);
- cvCvtColor(img, gray, CV_BGR2GRAY);
- cvSmooth(gray, gray, CV_GAUSSIAN, 9, 9);
- CvSeq* circles = cvHoughCircles(gray, storage,
- CV_HOUGH_GRADIENT, 2, gray->height / 4, 200, 100);
- int i;
- for (i = 0; i < circles->total; i++)
- {
- float* p = (float*)cvGetSeqElem(circles, i);
- cvCircle(img, cvPoint(cvRound(p[0]), cvRound(p[1])),
- 3, CV_RGB(0, 255, 0), -1, 8, 0);
- cvCircle(img, cvPoint(cvRound(p[0]), cvRound(p[1])),
- cvRound(p[2]), CV_RGB(255, 0, 0), 3, 8, 0);
- }
- //cvNamedWindow("circles", 1);
- cvShowImage("Detection", img);
- //waitKey(0);
- }
- /** @function Dilation */
- void Dilation(int, void*)
- {
- int dilation_type;
- if (dilation_elem == 0) { dilation_type = MORPH_RECT; }
- else if (dilation_elem == 1) { dilation_type = MORPH_CROSS; }
- else if (dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; }
- Mat element = getStructuringElement(dilation_type,
- Size(2 * dilation_size + 1, 2 * dilation_size + 1),
- Point(dilation_size, dilation_size));
- /// Apply the dilation operation
- dilate(src, dilation_dst, element);
- imshow("Dilation Demo", dilation_dst);
- }
- /** Function Headers */
- void Erosion(int, void*);
- void Dilation(int, void*);
- ///////////////
- /*
- int main(int, char**)
- {
- string cesta = "../data/detekciaIgor.bmp";
- Mat src, src_gray;
- /// Read the image
- //src = imread(argv[1], 1);
- src= imread(cesta.c_str(), IMREAD_COLOR);
- if (!src.data)
- {
- return -1;
- }
- /// Convert it to gray
- cvtColor(src, src_gray, CV_BGR2GRAY);
- /// Reduce the noise so we avoid false circle detection
- GaussianBlur(src_gray, src_gray, Size(9, 9), 2, 2);
- vector<Vec3f> circles;
- /// Apply the Hough Transform to find the circles
- HoughCircles(src_gray, circles, CV_HOUGH_GRADIENT, 1, src_gray.rows / 8, 50, 100, 0, 0);
- /// Draw the circles detected
- for (size_t i = 0; i < circles.size(); i++)
- {
- Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
- int radius = cvRound(circles[i][2]);
- // circle center
- circle(src, center, 3, Scalar(0, 255, 0), -1, 8, 0);
- // circle outline
- circle(src, center, radius, Scalar(0, 0, 255), 3, 8, 0);
- }
- /// Show your results
- namedWindow("Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE);
- imshow("Hough Circle Transform Demo", src);
- waitKey(0);
- return 0;
- }*/
- using namespace std;
- int main()
- {
- ////-----------FOTENIE-------------
- //VideoCapture cap(0); // open the default camera
- //if (!cap.isOpened()) // check if we succeeded
- // return -1;
- //string defaultCesta = "../data/fotkaCislo";
- //string cesta;
- //int pocetFotiek = 1;
- //std::string si;
- //Mat frame;
- //printf("Zacina fotenie\n");
- ///*for (int i = 3; i >= 1; i--) {
- // printf("%d\n", i);
- // std::this_thread::sleep_for(std::chrono::milliseconds(1000));
- //}*/
- //for (int i = 1; i <= pocetFotiek; i++)
- //{
- // printf("\nFotka %d\n", i);
- // std::this_thread::sleep_for(std::chrono::milliseconds(1000));
- // cap >> frame; // get a new frame from camera
- // // do any processing
- // //imwrite("path/to/image.png", frame);
- // si = std::to_string(i);
- // cesta = defaultCesta + si + ".bmp";
- // imwrite(cesta, frame);
- // std::this_thread::sleep_for(std::chrono::milliseconds(1000));
- // //if (waitKey(30) >= 0) break; // you can increase delay to 2 seconds here
- //}
- //// the camera will be deinitialized automatically in VideoCapture destructor
- //VideoCapture cap(0); // open the default camera
- //if (!cap.isOpened()) // check if we succeeded
- // return -1;
- //Mat edges;
- //namedWindow("edges", 1);
- //IplImage* imm;
- //for (;;)
- //{
- // Mat frame;
- // cap >> imm; // get a new frame from camera
- // /*cvtColor(frame, edges, COLOR_BGR2GRAY);
- // GaussianBlur(edges, edges, Size(7, 7), 1.5, 1.5);
- // Canny(edges, edges, 0, 30, 3);
- // imshow("edges", edges);*/
- // if (waitKey(30) >= 0) break;
- //}
- ////////////------GALERIA----///////////
- string defaultCesta = "../data/fotkaCislo";
- string cesta;
- int pocetFotiek = 49;
- printf("\nPrehravanie galerie\n");
- printf("\nOvladanie pismenami 'a' a 'd',\nvypnutie stlacenim q\n");
- int index = 1;
- std::string si;
- IplImage* img;
- printf("\nFotka 1\n");
- //myImage = imread(filenames[i]);
- si = std::to_string(index);
- cesta = defaultCesta + si + ".bmp";
- Detekcia(cesta);
- //img = cvLoadImage(cesta.c_str(), IMREAD_COLOR);
- //cvShowImage("Galeria", img);
- //int c = cv::waitKey(1);
- waitKey(1000);
- char znak;
- while (1) {
- znak = _getch();
- if ((znak == 'd') && (index < pocetFotiek)) {
- index += 1;
- printf("\nFotka %d\n", index);
- //myImage = imread(filenames[i]);
- si = std::to_string(index);
- cesta = defaultCesta + si + ".bmp";
- Detekcia(cesta);
- //img = cvLoadImage(cesta.c_str(), IMREAD_COLOR);
- //cvShowImage("Galeria", img);
- //int c = cv::waitKey(1);
- waitKey(30);
- }
- if ((znak == 'a') && (index > 1)) {
- index -= 1;
- printf("\nFotka %d\n", index);
- //myImage = imread(filenames[i]);
- si = std::to_string(index);
- cesta = defaultCesta + si + ".bmp";
- Detekcia(cesta);
- //img = cvLoadImage(cesta.c_str(), IMREAD_COLOR);
- //cvShowImage("Galeria", img);
- //int c = cv::waitKey(1);
- waitKey(30);
- }
- if (znak == 'q') {
- printf("Koniec");
- std::this_thread::sleep_for(std::chrono::milliseconds(1000));
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement