- 操作系统:Windows 7 SP1
- 编译器:mingw64 g++ 8.1.0
- 编辑器:Emacs 30.1
- OpenCV:4.12.0
目标
使用 OpenCV 函数 copyMakeBorder() 设置边框
理论
- 在之前,学习了使用卷积对图像进行操作。如果点位于图像的边缘,该如何卷积呢?
- 大多数 OpenCV 函数的作用是将给定的图像复制到另一个稍大的图像上,然后自动填充边界。这样卷积可以毫无疑问的在所需的像素上进行
- 讨论填充边框的两种方法
a. BORDER_CONSTANT:用常量值
b. BORDER_REPLICATE:原始文件边缘的行或列被复制到额外的边框。这将在代码部分更清楚地看到。 - 这个程序有什么用?
- 加载图像
- 让用户选择在输入图像中使用那种边框
- 常量值边框:为整个边框应用常量值的填充。此值将每 0.5 秒随机更新一次。
- 复制的边框:边框将从原始图像边缘的像素值复制。
- 用户通过按“c”(常量)或“r”(复制)来选择任一选项
- 当用户按下 ESC 时,程序结束
源代码
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
// Declare the variables
Mat src, dst;
int top, bottom, left, right;
int borderType = BORDER_CONSTANT;
const char* window_name = "copyMakeBorder Demo";
RNG rng(12345);
int main( int argc, char** argv )
{
const char* imageName = argc >=2 ? argv[1] : "lena.jpg";
// Loads an image
src = imread( samples::findFile( imageName ), IMREAD_COLOR ); // Load an image
// Check if image is loaded fine
if( src.empty()) {
printf(" Error opening image\n");
printf(" Program Arguments: [image_name -- default lena.jpg] \n");
return -1;
}
// Brief how-to for this program
printf( "\n \t copyMakeBorder Demo: \n" );
printf( "\t -------------------- \n" );
printf( " ** Press 'c' to set the border to a random constant value \n");
printf( " ** Press 'r' to set the border to be replicated \n");
printf( " ** Press 'ESC' to exit the program \n");
namedWindow( window_name, WINDOW_AUTOSIZE );
// Initialize arguments for the filter
top = (int) (0.05*src.rows); bottom = top;
left = (int) (0.05*src.cols); right = left;
for(;;)
{
Scalar value( rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255) );
copyMakeBorder( src, dst, top, bottom, left, right, borderType, value );
imshow( window_name, dst );
char c = (char)waitKey(500);
if( c == 27 )
{ break; }
else if( c == 'c' )
{ borderType = BORDER_CONSTANT; }
else if( c == 'r' )
{ borderType = BORDER_REPLICATE; }
}
return 0;
}
解释说明
声明变量
// Declare the variables
Mat src, dst;
int top, bottom, left, right;
int borderType = BORDER_CONSTANT;
const char* window_name = "copyMakeBorder Demo";
RNG rng(12345);
特别关注 rng ,他是一个随机数生成器,用它来生成随机边缘颜色
加载图像
const char* imageName = argc >=2 ? argv[1] : "lena.jpg";
// Loads an image
src = imread( samples::findFile( imageName ), IMREAD_COLOR ); // Load an image
// Check if image is loaded fine
if( src.empty()) {
printf(" Error opening image\n");
printf(" Program Arguments: [image_name -- default lena.jpg] \n");
return -1;
}
创建窗口
namedWindow( window_name, WINDOW_AUTOSIZE );
初始化参数
定义边框的大小(顶部、底部、左侧和右侧)的参数。我们给它们一个 src 大小的 5% 的值。
// Initialize arguments for the filter
top = (int) (0.05*src.rows); bottom = top;
left = (int) (0.05*src.cols); right = left;
循环
程序在未按下 ESC 键时无限循环运行。如果用户按 ‘c’ 或 ‘r’,则 borderType 变量分别取 BORDER_CONSTANT 或 BORDER_REPLICATE 的值:
char c = (char)waitKey(500);
if( c == 27 )
{ break; }
else if( c == 'c' )
{ borderType = BORDER_CONSTANT; }
else if( c == 'r' )
{ borderType = BORDER_REPLICATE; }
随机颜色
在每次迭代中(0.5 秒后),随机边框颜色(值)都会更新…
Scalar value( rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255) );
在图像周围形成边框
最后,我们调用函数 copyMakeBorder() 来应用相应的填充:
copyMakeBorder( src, dst, top, bottom, left, right, borderType, value );
- src:源图片
- dst:目标图像
- top,bottom,left,right:图像两侧边框的长度(以像素为单位)。我们将它们定义为图像原始大小的 5%。
- borderType:定义应用的边框类型。对于此示例,它可以是常量或复制的。
- value:如果 borderType 为 BORDER_CONSTANT,则这是用于填充边框像素的值。
显示结果
imshow( window_name, dst );
结果
- 默认情况下,它以边框设置为 BORDER_CONSTANT 开始。因此,将显示一系列随机彩色边框。
- 如果按“r”,边框将成为边缘像素的复制品。
- 如果按“c”,随机彩色边框将再次出现
- 如果按“ESC”,程序将退出。