- #include "opencv2/opencv.hpp"
- #include <iostream>
- #include "opencv2/imgcodecs.hpp"
- #include "opencv2/highgui.hpp"
- #include "opencv2/imgproc.hpp"
- #include <chrono>
- using namespace std::chrono;
- using namespace std;
- using namespace cv;
- void polarToCartesian(double rhoo, int thot, Point& p1, Point& p2);
- void polarToCartesian1(double rhoo2, int thot2, Point& p3, Point& p4);
- bool sobelEdge(const cv::Mat image, cv::Mat& result, uchar threshold);
- int main() {
- Mat inputImage = imread("f000901.png");
- imshow("Display Image", inputImage);
- // Mat inputImage1 = imread("prestrojavanje00221_9.png");
- // imshow("Display Image", inputImage1);
- /*
- */
- Mat frame1, mask1, mask2, res, res1, res2, sobelh, tmp, sobelv, absobelh, absobelv, canny, sobel, sobel2, sobel3; //1600
- double theta1 = 0, temp1 = 0, temp2 = 0, rhoo = 0, rhoo2 = 0;
- uchar pixel_value2 = 0;
- uchar pixel_value3 = 0;
- vector<vector<int>> votes(856, vector<int>(180, 0));
- int rho = 0, naj = votes[0][0], naj2 = votes[0][0], thot = 0, thot2 = 0, tw = 180, thh = 856;
- //cout << "height" << frame.;
- // cout << "width" << endl;
- // transpose(frame, frame);
- // flip(frame, frame, +1);
- auto start = high_resolution_clock::now();
- Rect region_of_interest = Rect(140, 170, 360, 180);;//ROI 75, 170, 490, 200 g 40, 245, 410, 120
- Mat image_roi = inputImage(region_of_interest);// roi na sliku za hsv i filtriranje
- Mat image_roi1 = inputImage(region_of_interest); // roi na sliku za cannya
- cvtColor(image_roi1, image_roi1, cv::COLOR_BGR2GRAY);
- GaussianBlur(image_roi1, image_roi1, Size(3, 3), 0);
- cvtColor(image_roi, image_roi, cv::COLOR_BGR2HSV);
- Scalar lowerby = Scalar(15, 30, 50);
- Scalar upperby = Scalar(45, 255, 255);
- Scalar lowerbw = Scalar(0, 0, 100); // 0 0 100
- Scalar upperbw = Scalar(150, 150, 255); // 150 150 255
- inRange(image_roi, lowerby, upperby, mask1);
- inRange(image_roi, lowerbw, upperbw, mask2);
- //imshow("res", image_roi);
- res = mask1 + mask2;
- //res = mask2;
- imshow("res", res);
- image_roi1.copyTo(sobel);
- sobelEdge(image_roi1, sobel, 255);
- inRange(sobel, 180, 256, sobel);
- imshow("sobel", sobel);
- bitwise_and(res, res, tmp, sobel); // spajanje slika
- imshow("fusion", tmp);// spojena slika
- //Rect region_of_interest = Rect(140, 170, 360, 180);;//ROI 75, 170, 490, 200
- //Mat image_roi = frame(region_of_interest);// roi na sliku za hsv i filtriranje
- //Mat image_roi1 = frame(region_of_interest); // roi na sliku za cannya
- for (int rows = 0; rows < tmp.rows; rows++)
- {
- for (int cols = 0; cols < tmp.cols; cols++)
- {
- pixel_value2 = (int)tmp.at<unsigned char>(rows, cols);
- if (pixel_value2 > 0)
- {
- for (int theta = 0; theta < tw; theta++)
- {
- if (theta > 20 && 160 > theta) // 42 138
- {
- theta1 = (((double)theta - 90.0) * 3.1415) / 180.0;
- temp1 = cos(theta1);
- temp2 = sin(theta1);
- rho = cvRound(cols * temp1 + rows * temp2 + 428);
- if (rho > 480 && rho < 721)
- votes[rho][theta]++;
- }
- }
- }
- }
- }
- for (int i = 0; i < thh; i += 2)
- {
- for (int theta = 0; theta < tw; theta += 2)
- {
- if (votes[i][theta] > naj && votes[i][theta] >= 40) /*&& theta > 46 && 60> theta && votes[i][theta]>=15*/
- {
- naj = votes[i][theta];
- rhoo = i - 428.0;
- thot = theta;
- }
- }
- }
- for (int i = 0; i < thh; i += 2)
- {
- for (int theta = 0; theta < tw; theta += 2)
- {
- 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*/
- {
- naj2 = votes[i][theta];
- rhoo2 = i - 428.0;
- thot2 = theta;
- }
- }
- }
- Point p1, p2;
- Point p3, p4;
- if (thot >= 92 && 104 >= thot || thot2 >= 92 && 104 >= thot2)
- {
- if (rhoo > 159 && 263 > rhoo || rhoo2 >= 159 && 263 >= rhoo2)
- printf("prestrojavanje\n");
- // printf("rho %lf\n", rhoo);
- // printf("theta %d\n", thot);
- // printf("rho2 %lf\n", rhoo2);
- // printf("theta2 %d\n", thot2);
- }
- printf("rho %lf\n", rhoo);
- printf("theta %d\n", thot);
- printf("rho2 %lf\n", rhoo2);
- printf("theta2 %d\n", thot2);
- polarToCartesian(rhoo, thot, p1, p2);
- polarToCartesian1(rhoo2, thot2, p3, p4);
- //printf("naj %d", naj);
- //printf("naj2 %d", naj2);
- line(tmp, p1, p2, Scalar(255, 255, 0), 3, LINE_AA);
- line(tmp, p3, p4, Scalar(255, 255, 0), 3, LINE_AA);
- auto stop = high_resolution_clock::now();
- imshow("output", tmp);
- imwrite("f000901 - output40.jpg", tmp);
- waitKey();
- // return 0;
- auto duration = duration_cast<milliseconds>(stop - start);
- // To get the value of duration use the count()
- // member function on the duration object
- cout << duration.count() << endl;
- // imshow("Frame2", image_roi1);
- // imshow("Frame3", image_roi);
- // Display the resulting frame
- // imshow("Frame", frame);
- //imshow("Fram2e", image_roi);
- // Press ESC on keyboard to exit
- // When everything done, release the video capture object
- // Closes all the frames
- return 0;
- }
- void polarToCartesian(double rhoo, int thot, Point& p1, Point& p2) {
- double thot1 = 0.0;
- thot1 = (((double)thot - 90) * 3.1415) / 180.0;
- int x0 = cvRound(rhoo * cos(thot1));
- int y0 = cvRound(rhoo * sin(thot1));
- p1.x = cvRound(x0 + 1000 * (-sin(thot1)));
- p1.y = cvRound(y0 + 1000 * (cos(thot1)));
- p2.x = cvRound(x0 - 1000 * (-sin(thot1)));
- p2.y = cvRound(y0 - 1000 * (cos(thot1)));
- }
- void polarToCartesian1(double rhoo2, int thot2, Point& p3, Point& p4) {
- double thot4 = 0.0;
- thot4 = (((double)thot2 - 90) * 3.1415) / 180.0;
- int x0 = cvRound(rhoo2 * cos(thot4));
- int y0 = cvRound(rhoo2 * sin(thot4));
- p3.x = cvRound(x0 + 1000 * (-sin(thot4)));
- p3.y = cvRound(y0 + 1000 * (cos(thot4)));
- p4.x = cvRound(x0 - 1000 * (-sin(thot4)));
- p4.y = cvRound(y0 - 1000 * (cos(thot4)));
- }
- bool sobelEdge(const cv::Mat image, cv::Mat& result, uchar threshold)
- {
- CV_Assert(image.channels() == 1);
- cv::Mat sobelx = (cv::Mat_<float>(3, 3) << 1, 0, -1, 2, 0, -2, 1, 0, -1);
- cv::Mat sobely = (cv::Mat_<float>(3, 3) << 1, 2, 1, 0, 0, 0, -1, -2, -1);
- result = cv::Mat::zeros(image.rows, image.cols, image.type());
- double graMag = 0;
- for (int i = 1; i < image.rows - 1; i++)
- {
- for (int j = 1; j < image.cols - 1; j++)
- {
- float edgex = 0, edgey = 0;
- for (int k = -1; k < 2; k++)
- {
- for (int p = -1; p < 2; p++)
- {
- edgex += (float)image.at<uchar>(k + i, p + j) * sobelx.at<float>(1 + k, 1 + p);
- edgey += (float)image.at<uchar>(k + i, p + j) * sobely.at<float>(1 + k, 1 + p);
- }
- }
- graMag = sqrt(pow(edgex, 2) + pow(edgey, 2));
- if (graMag > 255)
- graMag = 255;
- result.at<uchar>(i - 1, j - 1) = (uchar)graMag;
- }
- }
- //imshow("ggg", result);
- return true;
- }