Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.13 KB | None | 0 0
  1. // HittaValle.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. #include <opencv2/core/core.hpp>
  7. #include <opencv2/highgui/highgui.hpp>
  8. #include <opencv2/opencv.hpp>
  9. #include <opencv2\ml.hpp>
  10. #include <opencv2\ml\ml.hpp>
  11.  
  12. #include<iostream>
  13. #include<conio.h>           // may have to modify this line if not using Windows
  14. #include <vector>
  15. #include <limits.h>
  16.  
  17.  
  18. void GenerateDataset(cv::Mat &dataset, cv::Mat &labels);
  19.  
  20. inline bool FileExists(std::string name)
  21. {
  22.     struct stat buffer;
  23.     return (stat(name.c_str(), &buffer) == 0);
  24. }
  25.  
  26. cv::Ptr<cv::ml::SVM> GetTrainedSVM(cv::Mat data, cv::Mat labels)
  27. {
  28.     cv::Ptr<cv::ml::SVM> model = cv::ml::SVM::create();
  29.  
  30.     model->setType(cv::ml::SVM::C_SVC);
  31.     model->setKernel(cv::ml::SVM::LINEAR);
  32.     model->setTermCriteria(cv::TermCriteria(cv::TermCriteria::MAX_ITER, 100, 1e-6));
  33.  
  34.     cv::Ptr<cv::ml::TrainData> traindata = cv::ml::TrainData::create(data, cv::ml::ROW_SAMPLE, labels);
  35.  
  36.     model->train(traindata);
  37.  
  38.     int stopper = 0;
  39.  
  40.     return model;
  41. }
  42.  
  43. cv::Mat Preprocess(cv::Mat sample)
  44. {
  45.     cv::Mat processedSample;
  46.  
  47.     cv::cvtColor(sample, processedSample, CV_BGR2GRAY);
  48.     cv::GaussianBlur(processedSample, processedSample, cv::Size(5, 5), 1.0f);
  49.  
  50.  
  51.     processedSample = processedSample.reshape(1,1);
  52.     processedSample.convertTo(processedSample, CV_32F);
  53.  
  54.     return processedSample;
  55. }
  56.  
  57. bool PredictImage(cv::Ptr<cv::ml::StatModel> model, std::string sImagePath, int iDim)
  58. {
  59.     cv::Mat image = cv::imread(sImagePath);
  60.  
  61.     int iWidth = image.size().width;
  62.     int iHeight = image.size().height;
  63.  
  64.     const int iStride = 8;
  65.     cv::Mat subSample = cv::Mat(iDim, iDim, CV_8U);
  66.  
  67.     std::vector<cv::Point> vMajorityVote;
  68.  
  69.     for (int x = 0; x < iWidth - iDim - iStride; x += iStride)
  70.     {
  71.         for (int y = 0; y < iHeight - iDim - iStride; y += iStride)
  72.         {
  73.             int iOffsX = x;
  74.             int iOffsY = y;
  75.             cv::Rect subRect = cv::Rect(iOffsX, iOffsY, iDim, iDim);
  76.  
  77.             image(subRect).copyTo(subSample);
  78.  
  79.             if (model->predict(Preprocess(subSample)) == 1)//predicted sample to be wally
  80.             {
  81.                 vMajorityVote.push_back(cv::Point(x, y));
  82.             }
  83.         }
  84.     }
  85.     std::cout << "predictions done" << std::endl;
  86.  
  87.     int iSmallestMagnitude = INT_MAX;
  88.     int iMajorIndex = 0;
  89.     for (int i = 0; i < vMajorityVote.size(); ++i)
  90.     {
  91.         int iMagComulator = 0;
  92.         for (cv::Point p : vMajorityVote)
  93.         {
  94.             int iX = vMajorityVote[i].x - p.x;
  95.             int iY = vMajorityVote[i].y - p.y;
  96.             iMagComulator += iX * iX + iY * iY;
  97.         }
  98.         if (iMagComulator < iSmallestMagnitude)
  99.         {
  100.             iSmallestMagnitude = iMagComulator;
  101.             iMajorIndex = i;
  102.         }
  103.     }
  104.  
  105.     cv::circle(image, cv::Point(vMajorityVote[iMajorIndex].x + iDim / 2, vMajorityVote[iMajorIndex].y + iDim / 2), iDim - iDim / 3, cv::Scalar(128, 128, 255), 5);
  106.    
  107.     cv::imshow("Where is Wally?", image);
  108.     cv::waitKey(0);
  109.  
  110.     return true;
  111. }
  112.  
  113. void Valledate(float* pAns, float* pResult, int iSize)
  114. {
  115.     int iTruePositives = 0;
  116.     int iTrueNegatives = 0;
  117.     int iFalsePositives = 0;
  118.     int iFalseNegatives = 0;
  119.  
  120.     for (int i = 0; i < iSize; ++i)
  121.     {
  122.         int iAns = pAns[i];
  123.         int iRes = pResult[i];
  124.         if (iAns == 0.0f)
  125.         {
  126.             if (iRes == 0.0f)
  127.                 ++iTrueNegatives;
  128.             else
  129.                 ++iFalseNegatives;
  130.         }
  131.         else
  132.         {
  133.             if (iRes == 1.0f)
  134.                 ++iTruePositives;
  135.             else
  136.                 ++iFalsePositives;
  137.         }
  138.     }
  139.  
  140.     std::cout << "True Positives : " + std::to_string(iTruePositives) << std::endl <<
  141.         "True Negatives : " + std::to_string(iTrueNegatives) << std::endl <<
  142.         "False Positives : " + std::to_string(iFalsePositives) << std::endl <<
  143.         "False Negatives : " + std::to_string(iFalseNegatives) << std::endl;
  144.  
  145.     return;
  146. }
  147.  
  148. int main() {
  149.  
  150.     int iDim = 128;
  151.    
  152.     const int iSize = iDim * iDim;//nr of pixels
  153.    
  154.     float* pAns = new float[iSize];
  155.     float* pResult = new float[iSize];
  156.  
  157.     std::string sImagePath = "../Resources/original-images/8.jpg";
  158.    
  159.     cv::Mat data;
  160.     cv::Mat labels;
  161.     GenerateDataset(data, labels);
  162.     std::cout << "dataset done" << std::endl;
  163.     cv::Ptr<cv::ml::SVM> model = GetTrainedSVM(data, labels);
  164.     std::cout << "Trained SVM" << std::endl;
  165.     PredictImage(model, sImagePath, iDim);
  166.  
  167.  
  168.  
  169.    
  170.    
  171.     //std::system("pause");
  172.  
  173.     delete pAns;
  174.     delete pResult;
  175.    
  176.     return(0);
  177.  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement