Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "opencv2/imgcodecs.hpp"
- #include "opencv2/highgui/highgui.hpp"
- #include "opencv2/imgproc/imgproc.hpp"
- #include <iostream>
- using namespace std; using namespace cv;
- //function template matching
- void tmpMatch(Mat camFrame, vector<Mat> tmpPlates, vector<Mat> result, int tmpNr)
- {
- matchTemplate(camFrame, tmpPlates[tmpNr], result[tmpNr], CV_TM_CCOEFF_NORMED);
- }
- int main()
- {
- //declare variables
- double minVal;
- double maxVal;
- Point minLoc;
- Point maxLoc;
- Point matchLoc;
- string adressTmpPlateFolder = "D:\\opencv\\opencvtry1\\opencvtry1";
- vector<string> tmpPlateFileNames = { "1.jpg" }; // template file names
- vector<Mat> tmpPlates;
- vector<Mat> result;
- Mat camFrame;
- vector<int> tmpWidth;
- vector<int> tmpHeight;
- for (int i = 0; i < tmpPlateFileNames.size(); ++i)
- {
- std::string tempString = adressTmpPlateFolder + tmpPlateFileNames[i];
- Mat tempP = imread(tempString);
- if (!tempP.empty()) cout << "succeded" << endl;
- tmpPlates.push_back(tempP);
- std::cout << tempString << std::endl; tempP.release();
- }
- // open the default camera
- cv::VideoCapture cap(0);
- // set screen properties
- cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
- cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
- cap.set(CAP_PROP_FPS, 30);
- for (auto i = 0; i < tmpPlates.size(); i++)
- { //tmpWidth.push_back(camFrame.cols - tmpPlates[tmpNr].cols + 1);
- tmpWidth.push_back(640 - tmpPlates[i].cols + 1);
- //tmpHeight.push_back(camFrame.rows - tmpPlates[tmpNr].rows + 1); tmpHeight.push_back(480 - tmpPlates[i].rows + 1);
- Mat result1(tmpHeight[i], tmpWidth[i], CV_32FC1); result.push_back(result1);
- }
- // check if it's all is wrong
- if (!cap.isOpened())
- { // check if we succeeded
- std::cout << ("Failure to open camera") << "\n";
- }
- // if it's all is good then:
- else {
- for (;;) {
- // get a new frame from camera
- cap >> camFrame;
- //GetPresentationAttribute();
- for (int tmpNr = 0; tmpNr < tmpPlates.size(); tmpNr++)
- { //set frame properties
- tmpMatch(camFrame, tmpPlates, result, tmpNr);
- //enhance the chance pow(result[tmpNr], 5, result[tmpNr]);
- // find the best match with minMaxLoc
- minMaxLoc(result[tmpNr], &minVal, &maxVal, &minLoc, &maxLoc, Mat());
- matchLoc = maxLoc;
- //create a rectangle to indicate most likely area
- rectangle(camFrame, matchLoc, Point(matchLoc.x + tmpPlates[tmpNr].cols, matchLoc.y + tmpPlates[tmpNr].rows), Scalar::all(0), 2, 8, 0);
- if (maxVal >= 0.05)
- { Mat imgPanelRoi(camFrame, Rect(matchLoc.x, matchLoc.y, tmpPlates[tmpNr].cols, tmpPlates[tmpNr].rows)); tmpPlates[tmpNr].copyTo(imgPanelRoi); }
- }
- //setOpenGlContext("CamStill");
- cout << matchLoc.x << endl;
- imshow("CamStill", camFrame);
- camFrame.release();
- //use the esc botton to quit
- if (waitKey(1) == 27) break;
- }
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement