Created
August 18, 2013 14:28
-
-
Save poutyface/6261923 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _patch_tracker_hpp_ | |
#define _patch_tracker_hpp_ | |
#include "opencv2/opencv.hpp" | |
using namespace std; | |
class PatchTracker { | |
public: | |
PatchTracker(cv::Mat &image, cv::Point point, cv::Size patchSize) | |
{ | |
mImage = image; | |
mPatchSize = patchSize; | |
mPoint = point; | |
if((int)(mPoint.y - mPatchSize.height * 0.5) < 0 || | |
(int)(mPoint.y - mPatchSize.height * 0.5) + mPatchSize.height >= mImage.rows || | |
(int)(mPoint.x - mPatchSize.width * 0.5) < 0 || | |
(int)(mPoint.x - mPatchSize.width * 0.5) + mPatchSize.width >= mImage.cols){ | |
mTemplate = cv::Mat::zeros(mPatchSize.height, mPatchSize.width, CV_8U); | |
} | |
else{ | |
mTemplate = mImage(cv::Range((int)(mPoint.y - mPatchSize.height*0.5), | |
(int)(mPoint.y - mPatchSize.height*0.5) + mPatchSize.height), | |
cv::Range((int)(mPoint.x - mPatchSize.width*0.5), | |
(int)(mPoint.x - mPatchSize.width*0.5) + mPatchSize.width)); | |
} | |
//cout << mTemplate << endl; | |
//cout << mTemplate.cols << "," << mTemplate.rows << endl; | |
} | |
int makePatch(cv::Mat_<double> &m) | |
{ | |
int width = mImage.cols; | |
int height = mImage.rows; | |
cv::Point2f dc; | |
dc.x = m.at<double>(0,0) * mPoint.x + m.at<double>(0,1) * mPoint.y + m.at<double>(0,2); | |
dc.y = m.at<double>(1,0) * mPoint.x + m.at<double>(1,1) * mPoint.y + m.at<double>(1,2); | |
int hw = mPatchSize.width * 0.5; | |
int hh = mPatchSize.height * 0.5; | |
cv::Point start; | |
start.x = (int)(dc.x - hw); | |
start.y = (int)(dc.y - hh); | |
cv::Point end; | |
end.x = start.x + mPatchSize.width; | |
end.y = start.y + mPatchSize.height; | |
if(start.x < 0 || start.y >= mImage.cols || end.x < 0 || end.y >= mImage.rows){ | |
cout << mImage.cols << "," << mImage.rows << start << "," << end << endl; | |
return mPatchSize.width * mPatchSize.height; | |
} | |
cv::Mat minv = m.inv(); | |
int count = 0; | |
cv::Mat patch = cv::Mat::zeros(mPatchSize.height, mPatchSize.width, CV_8U); | |
for(int yy=start.y, y=0; yy<end.y; yy++, y++){ | |
for(int xx=start.x, x=0; xx<end.x; xx++, x++){ | |
cv::Point p; | |
p.x = minv.at<double>(0,0) * xx + minv.at<double>(0,1) * yy + m.at<double>(0,2); | |
p.y = minv.at<double>(1,0) * xx + minv.at<double>(1,1) * yy + m.at<double>(1,2); | |
if(p.x < 0 || p.x >= mImage.cols || p.y < 0 || p.y >= mImage.rows){ | |
patch.at<uchar>(y, x) = 0; | |
count++; | |
} | |
else{ | |
patch.at<uchar>(y,x) = mImage.at<uchar>(p.y, p.x); | |
} | |
} | |
} | |
// cout << patch << endl; | |
// cv::imshow("patch", patch); | |
// cv::waitKey(0); | |
return count; | |
} | |
private: | |
cv::Mat mImage; | |
cv::Mat mTemplate; | |
cv::Mat mPatch; | |
cv::Size mPatchSize; | |
cv::Point mPoint; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment