用OpenCVSharp 4.5 跑一遍 OpenCV官方教程
原官方教程链接:https://docs.opencv.org/4.5.0/d1/da0/tutorial_remap.html
核心函数:remap
using System;
using OpenCvSharp;
namespace ConsoleApp1
{
class tutorial17 : ITutorial
{
static Mat map_x, map_y;
static int ind;
void update_map()
{
for (int i = 0; i < map_x.Rows; i++)
{
for (int j = 0; j < map_x.Cols; j++)
{
switch (ind)
{
case 0:
if (j > map_x.Cols * 0.25 && j < map_x.Cols * 0.75 && i > map_x.Rows * 0.25 && i < map_x.Rows * 0.75)
{
map_x.At<float>(i, j) = 2 * (j - map_x.Cols * 0.25f) + 0.5f;
map_y.At<float>(i, j) = 2 * (i - map_x.Rows * 0.25f) + 0.5f;
}
else
{
map_x.At<float>(i, j) = 0;
map_y.At<float>(i, j) = 0;
}
break;
case 1:
map_x.At<float>(i, j) = (float)j;
map_y.At<float>(i, j) = (float)(map_x.Rows - i);
break;
case 2:
map_x.At<float>(i, j) = (float)(map_x.Cols - j);
map_y.At<float>(i, j) = (float)i;
break;
case 3:
map_x.At<float>(i, j) = (float)(map_x.Cols - j);
map_y.At<float>(i, j) = (float)(map_x.Rows - i);
break;
default:
break;
} // end of switch
}
}
ind = (ind + 1) % 4;
}
public void Run()
{
using (Mat src = new Mat("I:\\csharp\\images\\chicky_512.png", ImreadModes.Color))
using (Mat dst = new Mat(src.Size(), src.Type()))
{
map_x = new Mat(src.Size(), MatType.CV_32FC1);
map_y = new Mat(src.Size(), MatType.CV_32FC1);
Cv2.NamedWindow("remap demo", WindowFlags.AutoSize);
ind = 0;
for (; ; )
{
update_map();
Cv2.Remap(src, dst, map_x, map_y, InterpolationFlags.Linear, BorderTypes.Constant, new Scalar(0, 0, 0));
Cv2.ImShow("remap demo", dst);
char c = (char)Cv2.WaitKey(1000);
if (c == 27)
{
break;
}
}
}
}
}
}