Advertisement
tankian202

Untitled

Dec 19th, 2023
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.94 KB | None | 0 0
  1. int regionGrowing(Mat im, Point p0, Point& pbf, Point& pja) {
  2.     int count = 0;
  3.     Point* fifo = new Point[0x100000];
  4.     int nextIn = 0;
  5.     int nextOut = 0;
  6.     pbf = p0;
  7.     pja = p0;
  8.     if (getGray(im, p0.x, p0.y) < 128)
  9.         return 0;
  10.     fifo[nextIn++] = p0;
  11.     setGray(im, p0.x, p0.y, 100);
  12.     while (nextIn > nextOut) {
  13.         Point p = fifo[nextOut++];
  14.         ++count;
  15.         if (p.x > 0)
  16.             if (getGray(im, p.x - 1, p.y) > 128)
  17.             {
  18.                 fifo[nextIn++] = Point(p.x - 1, p.y);
  19.                 setGray(im, p.x - 1, p.y, 100);
  20.                 if (pbf.x > p.x - 1) pbf.x = p.x - 1;
  21.             }
  22.         if (p.x < im.cols - 1)
  23.             if (getGray(im, p.x + 1, p.y) > 128)
  24.             {
  25.                 fifo[nextIn++] = Point(p.x + 1, p.y);
  26.                 setGray(im, p.x + 1, p.y, 100);
  27.                 if (pja.x < p.x + 1) pja.x = p.x + 1;
  28.             }
  29.         if (p.y > 0)
  30.             if (getGray(im, p.x, p.y - 1) > 128)
  31.             {
  32.                 fifo[nextIn++] = Point(p.x, p.y - 1);
  33.                 setGray(im, p.x, p.y - 1, 100);
  34.                 if (pbf.y > p.y - 1) pbf.y = p.y - 1;
  35.             }
  36.         if (p.y < im.rows - 1)
  37.             if (getGray(im, p.x, p.y + 1) > 128)
  38.             {
  39.                 fifo[nextIn++] = Point(p.x, p.y + 1);
  40.                 setGray(im, p.x, p.y + 1, 100);
  41.                 if (pja.y < p.y + 1) pja.y = p.y + 1;
  42.             }
  43.     }
  44.     delete[]fifo;
  45.     return count;
  46. }
  47.  
  48.  
  49. void vidlab()
  50. {
  51.     //VideoCapture cap("IMG_6969.AVI");
  52.     VideoCapture cap("IMG_6909.MOV");
  53.     if (!cap.isOpened()) {
  54.         cout << "Error opening video stream or file" << endl;
  55.         return;
  56.     }
  57.     Mat frame, bground, result;
  58.     Mat grayframe, edgeframe, drawframe, blurframe, laplaceframe;
  59.     // Capture frame-by-frame
  60.     int i = 0;
  61.  
  62.     while (1)
  63.     {
  64.         cap >> frame;
  65.  
  66.         // If the frame is empty, break immediately
  67.         if (frame.empty()) {
  68.             break;
  69.         }
  70.  
  71.         resize(frame, frame, Size(frame.cols / 10, frame.rows / 10));
  72.  
  73.         i++;
  74.  
  75.         if (i == 7)
  76.         {
  77.             //elmentjuk hatterkepkent a 7. framet
  78.             bground = frame;
  79.             imshow("bground", bground);
  80.         }
  81.  
  82.         if (i > 13)
  83.         {
  84.             result = ((bground - frame) + (frame - bground));
  85.             //imshow("Abszolut kulonbseg", result);
  86.  
  87.             Mat rgb[3], & r = rgb[0], & g = rgb[1], & b = rgb[2];
  88.             split(result, rgb);
  89.             //osszeadjuk a kepcsatornakat
  90.             r = r + g;
  91.             r = r + b;
  92.             threshold(r, r, 170, 255, THRESH_BINARY);
  93.             erode(r, r, getStructuringElement(MORPH_RECT, Size(5, 5)));
  94.             //imshow("egy bináris képet, amelyiken a fehér képpontok jelzik azokat a helyeket a képen", r);
  95.             Point pbf, pja;
  96.             int nrRect = 0;
  97.             int roiSize;
  98.             Rect roi;
  99.  
  100.             for (int x = 0; x < r.cols; x++)
  101.             {
  102.                 for (int y = 0; y < r.rows; y++)
  103.                 {
  104.                     if (getGray(r, x, y) > 128)
  105.                     {
  106.                         int res = regionGrowing(r, Point(x, y), pbf, pja);
  107.                         if (res > 500)
  108.                         {
  109.                             if (!nrRect || res > roiSize)
  110.                             {
  111.                                 roi.x = pbf.x;
  112.                                 roi.y = pbf.y;
  113.                                 roi.width = pja.x - pbf.x + 1;
  114.                                 roi.height = pja.y - pbf.y + 1;
  115.                                 roiSize = res;
  116.                             }
  117.                             ++nrRect;
  118.                         }
  119.                     }
  120.                 }
  121.             }
  122.  
  123.             if (nrRect > 0) {
  124.                 rectangle(frame, Point(roi.x, roi.y),
  125.                     Point(roi.x + roi.width, roi.y + roi.height),
  126.                     Scalar(0, 255, 255, 0), 2);
  127.             }
  128.  
  129.             imshow("Kepfelismeres", frame);
  130.         }
  131.         waitKey(1);
  132.  
  133.  
  134.     }
  135.  
  136.  
  137.     cap.release();
  138. }
  139.  
  140. void lab11()
  141. {
  142.     VideoCapture cap("IMG_6909.MOV");
  143.     if (!cap.isOpened()) {
  144.         cout << "Error opening video stream or file" << endl;
  145.         return;
  146.     }
  147.     Mat frame;
  148.     // Capture frame-by-frame
  149.  
  150.     int lowTh = 45;
  151.     int highTh = 90;
  152.  
  153.     float szamok[9] = { 0.1, 0.1, 0.1, 0.1, 0.2, 0.1, 0.1, 0.1, 0.1 };//homalyositas
  154.  
  155.     for (;;)
  156.     {
  157.         // wait for a new frame from camera and store it into 'frame'
  158.         cap.read(frame);
  159.         // check if we succeeded
  160.         if (frame.empty()) {
  161.             cerr << "ERROR! blank frame grabbed\n";
  162.             break;
  163.         }
  164.         resize(frame, frame, Size(frame.cols / 10, frame.rows / 10));
  165.  
  166.         // show live and wait for a key with timeout long enough to show images
  167.         Mat grayscale, imgBlurred, imgCanny, median, maszk, F2;
  168.         F2 = frame.clone();
  169.         cvtColor(frame, grayscale, COLOR_RGB2GRAY); //gray video
  170.  
  171.         GaussianBlur(grayscale, imgBlurred, Size(5, 5), 1.8);           // Blur Effect  
  172.  
  173.         blur(frame, median, Size(5, 5)); //medianblur
  174.  
  175.         Canny(imgBlurred, imgCanny, lowTh, highTh); //edge detection
  176.  
  177.         maszk = Mat(3, 3, CV_32FC1, szamok);
  178.         filter2D(F2, F2, -1, maszk);
  179.  
  180.         Mat abs_dst, dst;
  181.         int kernel_size = 3;
  182.         int scale = 1;
  183.         int delta = 0;
  184.         int ddepth = CV_16S;
  185.         Laplacian(grayscale, dst, ddepth, kernel_size, scale, delta, BORDER_DEFAULT);
  186.         // converting back to CV_8U
  187.         convertScaleAbs(dst, abs_dst);
  188.  
  189.         Mat hist_equalized_image, frame2;
  190.         frame2 = frame.clone();
  191.         cvtColor(frame, hist_equalized_image, COLOR_BGR2YCrCb);
  192.  
  193.         //Split the image into 3 channels; Y, Cr and Cb channels respectively and store it in a std::vector
  194.         vector<Mat> vec_channels;
  195.         split(hist_equalized_image, vec_channels);
  196.  
  197.         //Equalize the histogram of only the Y channel
  198.         equalizeHist(vec_channels[0], vec_channels[0]);
  199.  
  200.         //Merge 3 channels in the vector to form the color image in YCrCB color space.
  201.         merge(vec_channels, hist_equalized_image);
  202.  
  203.         //Convert the histogram equalized image from YCrCb to BGR color space again
  204.         cvtColor(hist_equalized_image, hist_equalized_image, COLOR_YCrCb2BGR);
  205.  
  206.         //imshow("equalized", hist_equalized_image);
  207.  
  208.         Mat resized1, resized2, frame3;
  209.         frame3 = frame.clone();
  210.         resize(frame, resized1, Size(800, 600));
  211.         resize(frame3, resized2, Size(100, 100));
  212.         imshow("nagy", resized1);
  213.         imshow("kicsi", resized2);
  214.  
  215.         imshow("FilterLaplace", abs_dst);
  216.         imshow("MyVideo", grayscale);
  217.         imshow("Live", frame);
  218.         imshow("imgCanny", imgCanny);
  219.         imshow("imgGauss", imgBlurred);
  220.         imshow("MedianBlurr", median);
  221.         imshow("Filter", F2);
  222.         if (waitKey(5) >= 0)
  223.             break;
  224.     }
  225.  
  226.     // When everything done, release the video capture object
  227.     cap.release();
  228. }
  229.  
  230. int main() {
  231.     //lab01_changed();
  232.     //lab02();
  233.     //lab03();
  234.     //lab04();
  235.     //
  236.     //lab05_01();
  237.     //lab05_021();
  238.     //lab05_022();
  239.     //
  240.     //lab06_A();
  241.     //lab06_B();
  242.     //lab06_C();
  243.     //lab06_D();
  244.     //lab06_E();
  245.     //lab07_1();
  246.     //lab07_2();
  247.     //lab08();
  248.     //lab09();
  249.  
  250.     //Watershed();
  251.     vidlab();
  252.    
  253.     lab11();
  254.  
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement