Facebook
From Innocent Finch, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 127
  1. #include "opencv2/opencv.hpp"
  2. #include <iostream>
  3. #include "opencv2/imgcodecs.hpp"
  4. #include "opencv2/highgui.hpp"
  5. #include "opencv2/imgproc.hpp"
  6. #include <chrono>
  7. using namespace std::chrono;
  8. using namespace std;
  9. using namespace cv;
  10. void polarToCartesian(double rhoo, int thot, Point& p1, Point& p2);
  11. void polarToCartesian1(double rhoo2, int thot2, Point& p3, Point& p4);
  12. bool sobelEdge(const cv::Mat image, cv::Mat& result, uchar threshold);
  13. int main() {
  14.  
  15.     Mat inputImage = imread("f000901.png");
  16.     imshow("Display Image", inputImage);
  17.    // Mat inputImage1 = imread("prestrojavanje00221_9.png");
  18.   //  imshow("Display Image", inputImage1);
  19.     /*
  20.     */
  21.     Mat frame1, mask1, mask2, res, res1, res2, sobelh, tmp, sobelv, absobelh, absobelv, canny, sobel, sobel2, sobel3; //1600
  22.     double theta1 = 0, temp1 = 0, temp2 = 0, rhoo = 0, rhoo2 = 0;
  23.     uchar pixel_value2 = 0;
  24.     uchar pixel_value3 = 0;
  25.     vector<vector<int>> votes(856, vector<int>(180, 0));
  26.     int rho = 0, naj = votes[0][0], naj2 = votes[0][0], thot = 0, thot2 = 0, tw = 180, thh = 856;
  27.     //cout << "height" << frame.;
  28.    // cout << "width" << endl;
  29.    // transpose(frame, frame);
  30.   //  flip(frame, frame, +1);
  31.     auto start = high_resolution_clock::now();
  32.     Rect region_of_interest = Rect(140, 170, 360, 180);;//ROI 75, 170, 490, 200  g    40, 245, 410, 120
  33.     Mat image_roi = inputImage(region_of_interest);// roi na sliku za hsv i filtriranje
  34.     Mat image_roi1 = inputImage(region_of_interest); // roi na sliku za cannya
  35.     cvtColor(image_roi1, image_roi1, cv::COLOR_BGR2GRAY);
  36.     GaussianBlur(image_roi1, image_roi1, Size(3, 3), 0);
  37.     cvtColor(image_roi, image_roi, cv::COLOR_BGR2HSV);
  38.     Scalar lowerby = Scalar(15, 30, 50);
  39.     Scalar upperby = Scalar(45, 255, 255);
  40.     Scalar lowerbw = Scalar(0, 0, 100); // 0 0 100
  41.     Scalar upperbw = Scalar(150, 150, 255); // 150 150 255
  42.     inRange(image_roi, lowerby, upperby, mask1);
  43.     inRange(image_roi, lowerbw, upperbw, mask2);
  44.     //imshow("res", image_roi);
  45.     res = mask1 + mask2;
  46.     //res = mask2;
  47.     imshow("res", res);
  48.     image_roi1.copyTo(sobel);
  49.     sobelEdge(image_roi1, sobel, 255);
  50.     inRange(sobel, 180, 256, sobel);
  51.     imshow("sobel", sobel);
  52.     bitwise_and(res, res, tmp, sobel); // spajanje slika
  53.     imshow("fusion", tmp);// spojena slika
  54.     //Rect region_of_interest = Rect(140, 170, 360, 180);;//ROI 75, 170, 490, 200
  55.     //Mat image_roi = frame(region_of_interest);// roi na sliku za hsv i filtriranje
  56.     //Mat image_roi1 = frame(region_of_interest); // roi na sliku za cannya
  57.     for (int rows = 0; rows < tmp.rows; rows++)
  58.     {
  59.         for (int cols = 0; cols < tmp.cols; cols++)
  60.         {
  61.             pixel_value2 = (int)tmp.at<unsigned char>(rows, cols);
  62.             if (pixel_value2 > 0)
  63.             {
  64.                 for (int theta = 0; theta < tw; theta++)
  65.                 {
  66.                     if (theta > 20 && 160 > theta) // 42 138
  67.                     {
  68.                         theta1 = (((double)theta - 90.0) * 3.1415) / 180.0;
  69.                         temp1 = cos(theta1);
  70.                         temp2 = sin(theta1);
  71.                         rho = cvRound(cols * temp1 + rows * temp2 + 428);
  72.                         if (rho > 480 && rho < 721)
  73.                             votes[rho][theta]++;
  74.                     }
  75.  
  76.                 }
  77.             }
  78.         }
  79.     }
  80.     for (int i = 0; i < thh; i += 2)
  81.     {
  82.         for (int theta = 0; theta < tw; theta += 2)
  83.         {
  84.             if (votes[i][theta] > naj && votes[i][theta] >= 40) /*&& theta > 46 &&  60> theta  && votes[i][theta]>=15*/
  85.             {
  86.                 naj = votes[i][theta];
  87.                 rhoo = i - 428.0;
  88.                 thot = theta;
  89.  
  90.             }
  91.         }
  92.     }
  93.     for (int i = 0; i < thh; i += 2)
  94.     {
  95.         for (int theta = 0; theta < tw; theta += 2)
  96.         {
  97.             if (votes[i][theta] > naj2 && votes[i][theta] != naj && abs(thot - theta) > 40 && abs(thot - theta) < 110 && votes[i][theta] >= 40)/*theta > 110 && 138 > theta && votes[i][theta] >= 15*/
  98.             {
  99.                 naj2 = votes[i][theta];
  100.                 rhoo2 = i - 428.0;
  101.                 thot2 = theta;
  102.             }
  103.         }
  104.     }
  105.     Point p1, p2;
  106.     Point p3, p4;
  107.     if (thot >= 92 && 104 >= thot || thot2 >= 92 && 104 >= thot2)
  108.     {
  109.         if (rhoo > 159 && 263 > rhoo || rhoo2 >= 159 && 263 >= rhoo2)
  110.             printf("prestrojavanje\n");
  111.         //  printf("rho %lf\n", rhoo);
  112.          // printf("theta %d\n", thot);
  113.        //   printf("rho2 %lf\n", rhoo2);
  114.        //  printf("theta2 %d\n", thot2);
  115.  
  116.  
  117.     }
  118.     printf("rho %lf\n", rhoo);
  119.     printf("theta %d\n", thot);
  120.     printf("rho2 %lf\n", rhoo2);
  121.     printf("theta2 %d\n", thot2);
  122.     polarToCartesian(rhoo, thot, p1, p2);
  123.     polarToCartesian1(rhoo2, thot2, p3, p4);
  124.     //printf("naj %d", naj);
  125.     //printf("naj2 %d", naj2);
  126.     line(tmp, p1, p2, Scalar(255, 255, 0), 3, LINE_AA);
  127.     line(tmp, p3, p4, Scalar(255, 255, 0), 3, LINE_AA);
  128.      auto stop = high_resolution_clock::now();
  129.     imshow("output", tmp);
  130.     imwrite("f000901 - output40.jpg", tmp);
  131.     waitKey();
  132.     // return 0;
  133.     auto duration = duration_cast<milliseconds>(stop - start);
  134.  
  135.     // To get the value of duration use the count()
  136.     // member function on the duration object
  137.     cout << duration.count() << endl;
  138.  
  139.    // imshow("Frame2", image_roi1);
  140.  //   imshow("Frame3", image_roi);
  141.     // Display the resulting frame
  142.    // imshow("Frame", frame);
  143.     //imshow("Fram2e", image_roi);
  144.     // Press  ESC on keyboard to exit
  145.  
  146.  
  147. // When everything done, release the video capture object
  148.  
  149.  
  150. // Closes all the frames
  151.  
  152.  
  153.     return 0;
  154. }
  155. void polarToCartesian(double rhoo, int thot, Point& p1, Point& p2) {
  156.     double thot1 = 0.0;
  157.     thot1 = (((double)thot - 90) * 3.1415) / 180.0;
  158.     int x0 = cvRound(rhoo * cos(thot1));
  159.     int y0 = cvRound(rhoo * sin(thot1));
  160.     p1.x = cvRound(x0 + 1000 * (-sin(thot1)));
  161.     p1.y = cvRound(y0 + 1000 * (cos(thot1)));
  162.     p2.x = cvRound(x0 - 1000 * (-sin(thot1)));
  163.     p2.y = cvRound(y0 - 1000 * (cos(thot1)));
  164. }
  165. void polarToCartesian1(double rhoo2, int thot2, Point& p3, Point& p4) {
  166.     double thot4 = 0.0;
  167.     thot4 = (((double)thot2 - 90) * 3.1415) / 180.0;
  168.     int x0 = cvRound(rhoo2 * cos(thot4));
  169.     int y0 = cvRound(rhoo2 * sin(thot4));
  170.     p3.x = cvRound(x0 + 1000 * (-sin(thot4)));
  171.     p3.y = cvRound(y0 + 1000 * (cos(thot4)));
  172.     p4.x = cvRound(x0 - 1000 * (-sin(thot4)));
  173.     p4.y = cvRound(y0 - 1000 * (cos(thot4)));
  174. }
  175.  
  176.  
  177.  
  178. bool sobelEdge(const cv::Mat image, cv::Mat& result, uchar threshold)
  179. {
  180.     CV_Assert(image.channels() == 1);
  181.  
  182.     cv::Mat sobelx = (cv::Mat_<float>(3, 3) << 1, 0, -1, 2, 0, -2, 1, 0, -1);
  183.     cv::Mat sobely = (cv::Mat_<float>(3, 3) << 1, 2, 1, 0, 0, 0, -1, -2, -1);
  184.  
  185.     result = cv::Mat::zeros(image.rows, image.cols, image.type());
  186.     double graMag = 0;
  187.  
  188.     for (int i = 1; i < image.rows - 1; i++)
  189.     {
  190.         for (int j = 1; j < image.cols - 1; j++)
  191.         {
  192.             float edgex = 0, edgey = 0;
  193.  
  194.             for (int k = -1; k < 2; k++)
  195.             {
  196.                 for (int p = -1; p < 2; p++)
  197.                 {
  198.                     edgex += (float)image.at<uchar>(k + i, p + j) * sobelx.at<float>(1 + k, 1 + p);
  199.                     edgey += (float)image.at<uchar>(k + i, p + j) * sobely.at<float>(1 + k, 1 + p);
  200.                 }
  201.             }
  202.             graMag = sqrt(pow(edgex, 2) + pow(edgey, 2));
  203.  
  204.             if (graMag > 255)
  205.                 graMag = 255;
  206.             result.at<uchar>(i - 1, j - 1) = (uchar)graMag;
  207.         }
  208.     }
  209.  
  210.      //imshow("ggg", result);
  211.     return true;
  212. }