Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. #include "opencv2/imgcodecs.hpp"
  2. #include "opencv2/highgui/highgui.hpp"
  3. #include "opencv2/imgproc/imgproc.hpp"
  4. #include <iostream>
  5. using namespace std; using namespace cv;
  6. //function template matching
  7. void tmpMatch(Mat camFrame, vector<Mat> tmpPlates, vector<Mat> result, int tmpNr)
  8. {
  9. matchTemplate(camFrame, tmpPlates[tmpNr], result[tmpNr], CV_TM_CCOEFF_NORMED);
  10. }
  11. int main()
  12. {
  13. //declare variables
  14. double minVal;
  15. double maxVal;
  16. Point minLoc;
  17. Point maxLoc;
  18. Point matchLoc;
  19. string adressTmpPlateFolder = "D:\\opencv\\opencvtry1\\opencvtry1";
  20. vector<string> tmpPlateFileNames = { "1.jpg" }; // template file names
  21. vector<Mat> tmpPlates;
  22. vector<Mat> result;
  23. Mat camFrame;
  24. vector<int> tmpWidth;
  25. vector<int> tmpHeight;
  26. for (int i = 0; i < tmpPlateFileNames.size(); ++i)
  27. {
  28. std::string tempString = adressTmpPlateFolder + tmpPlateFileNames[i];
  29. Mat tempP = imread(tempString);
  30. if (!tempP.empty()) cout << "succeded" << endl;
  31. tmpPlates.push_back(tempP);
  32. std::cout << tempString << std::endl; tempP.release();
  33. }
  34. // open the default camera
  35. cv::VideoCapture cap(0);
  36. // set screen properties
  37. cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
  38. cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
  39. cap.set(CAP_PROP_FPS, 30);
  40.  
  41. for (auto i = 0; i < tmpPlates.size(); i++)
  42. { //tmpWidth.push_back(camFrame.cols - tmpPlates[tmpNr].cols + 1);
  43. tmpWidth.push_back(640 - tmpPlates[i].cols + 1);
  44. //tmpHeight.push_back(camFrame.rows - tmpPlates[tmpNr].rows + 1); tmpHeight.push_back(480 - tmpPlates[i].rows + 1);
  45. Mat result1(tmpHeight[i], tmpWidth[i], CV_32FC1); result.push_back(result1);
  46. }
  47. // check if it's all is wrong
  48. if (!cap.isOpened())
  49. { // check if we succeeded
  50. std::cout << ("Failure to open camera") << "\n";
  51. }
  52. // if it's all is good then:
  53. else {
  54. for (;;) {
  55. // get a new frame from camera
  56. cap >> camFrame;
  57. //GetPresentationAttribute();
  58. for (int tmpNr = 0; tmpNr < tmpPlates.size(); tmpNr++)
  59. { //set frame properties
  60. tmpMatch(camFrame, tmpPlates, result, tmpNr);
  61. //enhance the chance pow(result[tmpNr], 5, result[tmpNr]);
  62. // find the best match with minMaxLoc
  63. minMaxLoc(result[tmpNr], &minVal, &maxVal, &minLoc, &maxLoc, Mat());
  64. matchLoc = maxLoc;
  65. //create a rectangle to indicate most likely area
  66. rectangle(camFrame, matchLoc, Point(matchLoc.x + tmpPlates[tmpNr].cols, matchLoc.y + tmpPlates[tmpNr].rows), Scalar::all(0), 2, 8, 0);
  67. if (maxVal >= 0.05)
  68. { Mat imgPanelRoi(camFrame, Rect(matchLoc.x, matchLoc.y, tmpPlates[tmpNr].cols, tmpPlates[tmpNr].rows)); tmpPlates[tmpNr].copyTo(imgPanelRoi); }
  69. }
  70. //setOpenGlContext("CamStill");
  71. cout << matchLoc.x << endl;
  72. imshow("CamStill", camFrame);
  73. camFrame.release();
  74. //use the esc botton to quit
  75. if (waitKey(1) == 27) break;
  76. }
  77. return 0;
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement