OpenCV 【二十】给图像添加边界
生活随笔
收集整理的這篇文章主要介紹了
OpenCV 【二十】给图像添加边界
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
1原理
2 代碼
3 運行結果
1原理
-
前一節我們學習了圖像的卷積操作。一個很自然的問題是如何處理卷積邊緣。當卷積點在圖像邊界時會發生什么,如何處理這個問題?
-
大多數用到卷積操作的OpenCV函數都是將給定圖像拷貝到另一個輕微變大的圖像中,然后自動填充圖像邊界(通過下面示例代碼中的各種方式)。這樣卷積操作就可以在邊界像素安全執行了(填充邊界在操作完成后會自動刪除)。
-
本文檔將會探討填充圖像邊界的兩種方法:
-
BORDER_CONSTANT: 使用常數填充邊界 (i.e. 黑色或者 )
-
BORDER_REPLICATE: 復制原圖中最臨近的行或者列。
-
-
本程序做什么?
-
裝載圖像
-
由用戶決定使用哪種填充方式。有兩個選項:
-
常數邊界: 所有新增邊界像素使用一個常數,程序每0.5秒會產生一個隨機數更新該常數值。
-
復制邊界: 復制原圖像的邊界像素。
用戶可以選擇按 ‘c’ 鍵 (常數邊界) 或者 ‘r’ 鍵 (復制邊界)
-
-
當用戶按 ‘ESC’ 鍵,程序退出。
-
-
下面是本教程的源碼, 你也可以從 這里 下載
2 代碼
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
?
using namespace cv;
//src = imread("C:\\Users\\guoqi\\Desktop\\ch7\\4.jpg", 1);
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
?
using namespace cv;
?
/// 全局變量
Mat src, dst;
int top, bottom, left, right;
int borderType;
Scalar value;
char* window_name = "copyMakeBorder Demo";
RNG rng(12345);
?
/** @函數 main */
int main(int argc, char** argv)
{
?int c;
?/// 裝載圖像src = imread("C:\\Users\\guoqi\\Desktop\\ch7\\4.jpg", 1);
?if (!src.data){return -1;printf(" No data entered, please enter the path to an image file \n");}
?/// 使用說明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, CV_WINDOW_AUTOSIZE);
?/// 初始化輸入參數top = (int)(0.05*src.rows); bottom = (int)(0.05*src.rows);left = (int)(0.05*src.cols); right = (int)(0.05*src.cols);dst = src;
?imshow(window_name, dst);
?while (true){c = waitKey(500);
?if ((char)c == 27){break;}else if ((char)c == 'c'){borderType = BORDER_CONSTANT;}else if ((char)c == 'r'){borderType = BORDER_REPLICATE;}
?value = Scalar(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);}
?return 0;
}
?
3 運行結果
?
總結
以上是生活随笔為你收集整理的OpenCV 【二十】给图像添加边界的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 澳典m6家用投影仪使用指南?
- 下一篇: 【C++】多线程与并发【一】