opencv学习笔记(五)

这段程序来自OpenCV教程,使用copyMakeBorder函数来给图像加上边框:

[cpp]  view plain copy
  1. #include <opencv2/core/core.hpp>  
  2. #include <opencv2/highgui/highgui.hpp>  
  3. #include <opencv2/imgproc/imgproc.hpp>  
  4. #include <iostream>  
  5.   
  6. using namespace std;  
  7. using namespace cv;  
  8.   
  9.   
  10.   
  11.   
  12. int main()  
  13. {  
  14.     Mat image,result;  
  15.     //获得边界的类型  
  16.     int borderType = BORDER_REPLICATE;  
  17.     //输入字符  
  18.     int c;  
  19.   
  20.     image = imread("D:/picture/img.tif");  
  21.     if(!image.data)  
  22.     {  
  23.         printf("fail to load image\n");  
  24.         return -1;  
  25.     }  
  26.     //使用说明  
  27.     cout<<"使用说明:"<<endl;  
  28.     cout<<"按键c设置边界为随机的颜色"<<endl;  
  29.     cout<<"按键r设置边界为原图的颜色"<<endl;  
  30.     cout<<"按键按键ESC退出程序"<<endl;  
  31.   
  32.     //颜色随机值  
  33.     Scalar value;  
  34.     //随机数种子  
  35.     RNG rng(12345);  
  36.     //初始化参数:边框的粗细  
  37.     int top = (int) (0.05*image.rows);  
  38.     int bottom = (int) (0.05*image.rows);  
  39.     int left = (int) (0.05*image.cols);  
  40.     int right = (int) (0.05*image.cols);  
  41.   
  42.     result = image;  
  43.     //imshow("显示结果",result);  
  44.       
  45.     while(true)  
  46.     {  
  47.         c = waitKey(500);  
  48.         if((char)c == 27)  
  49.         {  
  50.             break;  
  51.         }  
  52.         else if((char)c == 'c')  
  53.         {  
  54.             //用常数添加边框(255:白色)  
  55.             borderType = BORDER_CONSTANT;   
  56.         }  
  57.         else if((char)c == 'r')  
  58.         {  
  59.             //复制原图像的边界像素  
  60.             borderType = BORDER_REPLICATE;  
  61.         }  
  62.   
  63.         //设置随机颜色  
  64.         value = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));  
  65.         //在图像周围形成边界:输入图像、输出图像(大小为源图像+边界)、上、下、左、右、边界类型、颜色  
  66.         copyMakeBorder(image,result,top,bottom,left,right,borderType,value);  
  67.         imshow("显示结果",result);  
  68.     }  
  69.   
  70. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值