Skip to content

Instantly share code, notes, and snippets.

@tomhartley
Created June 6, 2015 10:42
Show Gist options
  • Save tomhartley/4e4f0a1d07a11199bbe5 to your computer and use it in GitHub Desktop.
Save tomhartley/4e4f0a1d07a11199bbe5 to your computer and use it in GitHub Desktop.
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "chilitags/chilitags.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int scaleImage(Mat input, Mat &output);
char window_name1[] = "Unprocessed Image";
char window_name2[] = "Processed Image";
int main( int argc, char** argv )
{
/// Load the source image
// Simple parsing of the parameters related to the image acquisition
int xRes = 1280;
int yRes = 1024;
int cameraIndex = 0;
if (argc > 2) {
xRes = std::atoi(argv[1]);
yRes = std::atoi(argv[2]);
}
if (argc > 3) {
cameraIndex = std::atoi(argv[3]);
}
// The source of input images
cv::VideoCapture capture(cameraIndex);
if (!capture.isOpened())
{
std::cerr << "Unable to initialise video capture." << std::endl;
return 1;
}
#ifdef OPENCV3
capture.set(cv::CAP_PROP_FRAME_WIDTH, xRes);
capture.set(cv::CAP_PROP_FRAME_HEIGHT, yRes);
#else
capture.set(CV_CAP_PROP_FRAME_WIDTH, xRes);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, yRes);
#endif
cv::Mat inputImage;
// The tag detection happens in the Chilitags class.
chilitags::Chilitags chilitags;
cv::namedWindow("DisplayChilitags");
// Main loop, exiting when 'q is pressed'
for (; 'q' != (char) cv::waitKey(1); ) {
// Capture a new image.
capture.read(inputImage);
Mat out;
scaleImage(inputImage,out);
imshow("LiveTrack", out);
}
waitKey();
return 0;
}
Point2f getPos(int fidnum,int cornum) {
Size fiddist = Size(1235,790); //image size 1135, 690
Size fidsize = Size(100,100);
//taking the lower right of the top left fiducial as (0,0)
float fidx = (fidnum==1 || fidnum==2)*fiddist.width;
float fidy = (fidnum==2 || fidnum==3)*fiddist.height;
float cornerx = (cornum==0 || cornum == 3)*-fidsize.width;
float cornery = (cornum==0 || cornum == 1)*fidsize.height;
return Point2f(fidx+cornerx,fidy+cornery);
}
int scaleImage(Mat input, Mat &output) {
//So far, A4 sheet will be tags 8,9,10,11 [OFFSET = 2]
int offset; //A4 = 2
int tagCount = 0;
Mat_<Point2f> mats[4];
int exists[4] = {0,0,0,0};
for (const auto &tag : chilitags::Chilitags().find(input)) {
tagCount+=1;
int id = tag.first;
offset = id/4;
id = (id-8)/2;
Mat_<Point2f> corners(tag.second);
mats[id] = corners;
exists[id] = 1;
}
if (tagCount==0) {
output = input.clone();
cout << "FUCK IT ALL" << endl;
return -1;
}
//first quality points vs second quality points.
int numpts = 0;
Point2f src_p[4];
Point2f dst_p[4];
for (int i = 0; i<4; i++) {
//hunt for first quality pts
if (exists[i]) {
src_p[numpts]=mats[i](2); //lower right point
dst_p[numpts]=getPos(i,2);
numpts+=1;
}
}
int curFid = 0;
while (numpts!=4) { //loop over each fiducial
if (exists[curFid]) {
for (int i = 0;i<4;i==1?i++:i+=2) {
//loop over the non-primary points [everything but 2]
src_p[numpts]=mats[curFid](i);
dst_p[numpts]=getPos(curFid,i);
numpts+=1;
if (numpts==4) {
break;
}
}
}
curFid+=1;
}
for (int i = 0; i<4; i++) {
cout << i << " -- " << src_p[i].x << " -- " << src_p[i].y << endl;
}
Mat trans = getPerspectiveTransform(src_p,dst_p);
int myradius=5;
warpPerspective(input,output,trans,Size(1135.0f,690.0f));
return offset;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment