Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kpykc/1639d0ae01207d747092 to your computer and use it in GitHub Desktop.
Save kpykc/1639d0ae01207d747092 to your computer and use it in GitHub Desktop.
#pragma once
#include <SDKDDKVer.h>
#include <Windows.h>
#include <stdio.h>
#include <iostream>
// for OpenCV2
#include <opencv2/gpu/gpu.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#ifdef _DEBUG
#pragma comment(lib, "opencv_core241d.lib")
#pragma comment(lib, "opencv_gpu241d.lib")
#pragma comment(lib, "opencv_imgproc241d.lib")
#pragma comment(lib, "opencv_highgui241d.lib")
#else
#pragma comment(lib, "opencv_core241.lib")
#pragma comment(lib, "opencv_gpu241.lib")
#pragma comment(lib, "opencv_imgproc241.lib")
#pragma comment(lib, "opencv_highgui241.lib")
#endif
// for boost
#include <boost/timer/timer.hpp>
#include <boost/format.hpp>
cv::Point2f make_dst_p(const int &i, const int &w, const int &h)
{
cv::Point2f p;
float d = (float)(i % (w*2 + h*2));
if (d < w) {
p = cv::Point2f(d, 0.0f);
}
else if (d < w + h) {
p = cv::Point2f(w - 1.0f, d - w);
}
else if (d < w*2 + h) {
p = cv::Point2f(w - (d - (w + h)), h - 1.0f);
}
else {
p = cv::Point2f(0.0f, h - (d - (w*2 + h)));
}
return p;
}
void make_dst_points(const int &i, const int &w, const int &h, cv::Point2f p[4])
{
p[0] = make_dst_p(i , w, h);
p[1] = make_dst_p(i + w , w, h);
p[2] = make_dst_p(i + w + h , w, h);
p[3] = make_dst_p(i + w * 2 + h, w, h);
}
int main(int argc, char* argv[])
{
cv::Mat src_img, dst_img;
cv::gpu::GpuMat src_gpu, dst_gpu;
src_img = cv::imread("../data/lena.jpg");
if (src_img.empty()) {
printf("error: can not load image...\n");
return -1;
}
src_gpu.upload(src_img);
dst_img.create(src_img.size(), src_img.type());
dst_gpu.upload(dst_img);
cv::Point2f src_p[4];
cv::Point2f dst_p[4];
float w = (float)src_img.cols;
float h = (float)src_img.rows;
float hw = w / 2.0f;
float hh = h / 2.0f;
// from points
src_p[0] = cv::Point2f(0.0f, 0.0f);
src_p[1] = cv::Point2f( w, 0.0f);
src_p[2] = cv::Point2f( w, h);
src_p[3] = cv::Point2f(0.0f, h);
// to points
dst_p[0] = cv::Point2f( hw, 0.0f);
dst_p[1] = cv::Point2f( w, hh);
dst_p[2] = cv::Point2f( hw, h);
dst_p[3] = cv::Point2f(0.0f, hh);
int count = 0;
boost::timer::cpu_timer timer;
timer.start();
while(true) {
make_dst_points(count, src_img.cols, src_img.rows, dst_p);
// create perspective transform matrix
cv::Mat trans_mat33 = cv::getPerspectiveTransform(src_p, dst_p); //CV_64F->double
// perspective transform operation using transform matrix
cv::gpu::warpPerspective(src_gpu, dst_gpu, trans_mat33, src_img.size(), cv::INTER_LINEAR);
src_gpu.download(src_img);
dst_gpu.download(dst_img);
cv::imshow("src_gpu", src_img);
cv::imshow("dst_gpu", dst_img);
int c = cv::waitKey(1);
if (c == 27) break;
count ++;
if (count % 100 == 99) {
boost::timer::cpu_times elapsed = timer.elapsed(); // nano seconds...
float t = (elapsed.wall / 1000.0f / 1000.0f / 1000.0f) / (float)100;
float fps = 1.0f / t ;
if (t > 0.001) {
std::cout << "t=" << t << ", fps=" << fps << std::endl;
}
timer.start();
}
}
return 0;
}
#pragma once
#include <SDKDDKVer.h>
#include <Windows.h>
#include <stdio.h>
#include <iostream>
// for OpenCV2
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#ifdef _DEBUG
#pragma comment(lib, "opencv_core241d.lib")
#pragma comment(lib, "opencv_imgproc241d.lib")
#pragma comment(lib, "opencv_highgui241d.lib")
#else
#pragma comment(lib, "opencv_core241.lib")
#pragma comment(lib, "opencv_imgproc241.lib")
#pragma comment(lib, "opencv_highgui241.lib")
#endif
// for boost
#include <boost/timer/timer.hpp>
#include <boost/format.hpp>
cv::Point2f make_dst_p(const int &i, const int &w, const int &h)
{
cv::Point2f p;
float d = (float)(i % (w*2 + h*2));
if (d < w) {
p = cv::Point2f(d, 0.0f);
}
else if (d < w + h) {
p = cv::Point2f(w - 1.0f, d - w);
}
else if (d < w*2 + h) {
p = cv::Point2f(w - (d - (w + h)), h - 1.0f);
}
else {
p = cv::Point2f(0.0f, h - (d - (w*2 + h)));
}
return p;
}
void make_dst_points(const int &i, const int &w, const int &h, cv::Point2f p[4])
{
p[0] = make_dst_p(i , w, h);
p[1] = make_dst_p(i + w , w, h);
p[2] = make_dst_p(i + w + h , w, h);
p[3] = make_dst_p(i + w * 2 + h, w, h);
}
int main(int argc, char* argv[])
{
cv::Mat src_img, dst_img;
src_img = cv::imread("../data/lena.jpg");
if (src_img.empty()) {
printf("error: can not load image...\n");
return -1;
}
dst_img.create(src_img.size(), src_img.type());
cv::Point2f src_p[4];
cv::Point2f dst_p[4];
float w = (float)src_img.cols;
float h = (float)src_img.rows;
float hw = w / 2.0f;
float hh = h / 2.0f;
// from points
src_p[0] = cv::Point2f(0.0f, 0.0f);
src_p[1] = cv::Point2f( w, 0.0f);
src_p[2] = cv::Point2f( w, h);
src_p[3] = cv::Point2f(0.0f, h);
// to points
dst_p[0] = cv::Point2f( hw, 0.0f);
dst_p[1] = cv::Point2f( w, hh);
dst_p[2] = cv::Point2f( hw, h);
dst_p[3] = cv::Point2f(0.0f, hh);
int count = 0;
boost::timer::cpu_timer timer;
timer.start();
while(true) {
make_dst_points(count, src_img.cols, src_img.rows, dst_p);
// create perspective transform matrix
cv::Mat trans_mat33 = cv::getPerspectiveTransform(src_p, dst_p); //CV_64F->double
// perspective transform operation using transform matrix
cv::warpPerspective(src_img, dst_img, trans_mat33, src_img.size(), cv::INTER_LINEAR);
cv::imshow("src_img", src_img);
cv::imshow("dst_img", dst_img);
int c = cv::waitKey(1);
if (c == 27) break;
count ++;
if (count % 100 == 99) {
boost::timer::cpu_times elapsed = timer.elapsed(); // nano seconds...
float t = (elapsed.wall / 1000.0f / 1000.0f / 1000.0f) / (float)100;
float fps = 1.0f / t ;
if (t > 0.001) {
std::cout << "t=" << t << ", fps=" << fps << std::endl;
}
timer.start();
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment