日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

OpenCV 【二十】给图像添加边界

發布時間:2023/11/27 生活经验 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OpenCV 【二十】给图像添加边界 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

1原理

2 代碼

3 運行結果


1原理

  1. 前一節我們學習了圖像的卷積操作。一個很自然的問題是如何處理卷積邊緣。當卷積點在圖像邊界時會發生什么,如何處理這個問題?

  2. 大多數用到卷積操作的OpenCV函數都是將給定圖像拷貝到另一個輕微變大的圖像中,然后自動填充圖像邊界(通過下面示例代碼中的各種方式)。這樣卷積操作就可以在邊界像素安全執行了(填充邊界在操作完成后會自動刪除)。

  3. 本文檔將會探討填充圖像邊界的兩種方法:

    1. BORDER_CONSTANT: 使用常數填充邊界 (i.e. 黑色或者 )

    2. BORDER_REPLICATE: 復制原圖中最臨近的行或者列。

  1. 本程序做什么?

    • 裝載圖像

    • 由用戶決定使用哪種填充方式。有兩個選項:

      1. 常數邊界: 所有新增邊界像素使用一個常數,程序每0.5秒會產生一個隨機數更新該常數值。

      2. 復制邊界: 復制原圖像的邊界像素。

      用戶可以選擇按 ‘c’ 鍵 (常數邊界) 或者 ‘r’ 鍵 (復制邊界)

    • 當用戶按 ‘ESC’ 鍵,程序退出。

  2. 下面是本教程的源碼, 你也可以從 這里 下載

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 【二十】给图像添加边界的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。