วันพฤหัสบดีที่ 23 มีนาคม พ.ศ. 2560

C++ opencv Remapping

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace cv;

/// Global variables
Mat frame, dst;
Mat map_x, map_y;
char* remap_window = "Remap demo";
int ind = 0;

/// Function Headers
void update_map(void);

/**
* @function main
*/
int main(int argc, char** argv)
{

VideoCapture cap(0);
while (1)
{

cap >> frame;
namedWindow("Original", CV_WINDOW_AUTOSIZE);
imshow("Original", frame);

/// Create dst, map_x and map_y with the same size as src:
dst.create(frame.size(), frame.type());
map_x.create(frame.size(), CV_32FC1);
map_y.create(frame.size(), CV_32FC1);

// LLop for reducing half the size and centering
for (int j = 0; j < frame.rows; j++)
{
for (int i = 0; i < frame.cols; i++)
{

if (i > frame.cols*0.25 && i < frame.cols*0.75 && j > frame.rows*0.25 && j < frame.rows*0.75)
{
map_x.at<float>(j, i) = 2 * (i - frame.cols*0.25) + 0.5;
map_y.at<float>(j, i) = 2 * (j - frame.rows*0.25) + 0.5;
}
else
{
map_x.at<float>(j, i) = 0;
map_y.at<float>(j, i) = 0;
}
}
}
remap(frame, dst, map_x, map_y, CV_INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0));
namedWindow("reducing half the size and centering", CV_WINDOW_AUTOSIZE);
imshow("reducing half the size and centering", dst);

//Loop for upside down
for (int j = 0; j < frame.rows; j++)
{
for (int i = 0; i < frame.cols; i++)
{

map_x.at<float>(j, i) = i;
map_y.at<float>(j, i) = frame.rows - j;

}
}
remap(frame, dst, map_x, map_y, CV_INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0));
namedWindow("upside down", CV_WINDOW_AUTOSIZE);
imshow("upside down", dst);

//Loop for Reflecting x direction
for (int j = 0; j < frame.rows; j++)
{
for (int i = 0; i < frame.cols; i++)
{

map_x.at<float>(j, i) = frame.cols - i;
map_y.at<float>(j, i) = j;

}
}
remap(frame, dst, map_x, map_y, CV_INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0));
namedWindow("Reflecting x direction", CV_WINDOW_AUTOSIZE);
imshow("Reflecting x direction", dst);

//Loop for Reflecting both directions
for (int j = 0; j < frame.rows; j++)
{
for (int i = 0; i < frame.cols; i++)
{

map_x.at<float>(j, i) = frame.cols - i;
map_y.at<float>(j, i) = frame.rows - j;

}
}
remap(frame, dst, map_x, map_y, CV_INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0));
namedWindow("Reflecting both directions:", CV_WINDOW_AUTOSIZE);
imshow("Reflecting both directions:", dst);
if (waitKey(30) == 27)
break;
}
return 0;
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น