完整代码:
#include <iostream> #include <string> #include <opencv2/opencv.hpp> #include <opencv2/imgproc/types_c.h> using namespace std; using namespace cv; #ifndef P17 #define P17 16 #endif int main() { std::string path = "../fei.JPG"; cv::Mat img = cv::imread(path, 5); string str_input = "input image"; string str_output = "output image"; if(img.empty()) { std::cout << "open file failed" << std::endl; return -1; } #if P17 //边缘处理 namedWindow("input", WINDOW_AUTOSIZE); namedWindow("output", WINDOW_AUTOSIZE); imshow("input", img); int top = (int)(0.05*img.rows); int bottom = (int)(0.05*img.rows); int left = (int)(0.05*img.cols); int right = (int)(0.05*img.cols); int border_type = BORDER_WRAP; RNG rng(12345); int c = 0; Mat dest; Scalar color; while(true) { c = waitKey(500); if((char)c == 27) { break; }else if((char)c == 'r') { border_type = BORDER_REFLECT; }else if((char)c == 'c') { border_type = BORDER_CONSTANT; } color = Scalar(rng.uniform(0,255),rng.uniform(0,255),rng.uniform(0,255)); copyMakeBorder(img, dest,top, bottom,left,right,border_type, color); imshow("output",dest); } #endif cv::waitKey(0); cv::destroyAllWindows(); return 0; }效果图: