OpenCV Tutorials —— Adding borders to your images

扩展图像边界:

      1. BORDER_CONSTANT: Pad the image with a constant value (i.e. black or
  1. BORDER_REPLICATE: The row or column at the very edge of the original is replicated to the extra border.

 

copyMakeBorder( src, dst, top, bottom, left, right, borderType, value );

The arguments are:

  1. 1,src: Source image
  2. 2,dst: Destination image
  3. 3,top, bottom, left, right: Length in pixels of the borders at each side of the image. We define them as being 5% of the original size of the image.
  4. 4,borderType: Define what type of border is applied. It can be constant or replicate for this example.
  5. 5,value: If borderType is BORDER_CONSTANT, this is the value used to fill the border pixels.

 

Code

#include "stdafx.h"

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>

using namespace cv;

/// Global Variables
Mat src, dst;
int top, bottom, left, right;
int borderType;
Scalar value;
char* window_name = "copyMakeBorder Demo";
RNG rng(12345);

/** @function main  */
int main( int argc, char** argv )
{

	int c;

	/// Load an image
	src = imread( "img2.jpg" );

	if( !src.data )
	{ return -1;
	printf(" No data entered, please enter the path to an image file \n");
	}

	/// 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");

	/// Create window
	namedWindow( window_name, CV_WINDOW_AUTOSIZE );

	/// Initialize arguments for the filter
	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);	// 0.5 秒

		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;
}
时间: 2024-10-01 00:23:08

OpenCV Tutorials —— Adding borders to your images的相关文章

OpenCV Tutorials &mdash;&mdash; Adding (blending) two images using OpenCV

An interesting dyadic (two-input) operator is thelinear blend operator:   #include <cv.h> #include <highgui.h> #include <iostream> using namespace cv; int main( int argc, char** argv ) { double alpha = 0.5; double beta; double input; Mat

OpenCV Tutorials &mdash;&mdash; Adding a Trackbar to our applications

int createTrackbar(const string& trackbarname, const string& winname, int* value, int count, TrackbarCallbackonChange=0, void* userdata=0) Parameters: trackbarname – Name of the created trackbar.  滑动条名称 winname – Name of the window that will be us

学习opencv tutorials

1.opencv里头动态库和静态库的区别 lib是动态库,staticlib是静态库. 这是opencv tutorials中对动态库和静态库的说明.动态库是在runtime时候才load的库文件.而静态库文件会在你build的时候build-in inside your exe file.优点是可以避免误删,缺点是应用程序变大,加载时间也会变长. 2.  Visual Studio中solution和project的关系 在VS中,一个solution中可以包含多个project. 3.  两

OpenCV Tutorials &mdash;&mdash; Hough Line Transform

霍夫直线变换 -- 用于检测图像中的直线 利用图像空间和Hough参数空间的点--直线对偶性,把图像空间中的检测问题转换到参数空间,通过在参数空间进行简单的累加统计,然后在Hough参数空间中寻找累加器峰值的方法检测直线 Standard and Probabilistic Hough Line Transform OpenCV implements two kind of Hough Line Transforms: The Standard Hough Transform It consis

OpenCV Tutorials &mdash;&mdash; Feature Matching with FLANN

Extractors of keypoint descriptors in OpenCV have wrappers with a common interface that enables you to easily switch between different algorithms solving the same problem.   DescriptorExtractor::compute Computes the descriptors for a set of keypoints

OpenCV Tutorials &mdash;&mdash; Creating a video with OpenCV

写video 需要用到 VideoWriter  视频文件可看作一个容器 视频的类型由视频文件的后缀名来指定   Due to this OpenCV for video containers supports only the avi extension, its first version. A direct limitation of this is that you cannot save a video file larger than 2 GB. Furthermore you ca

OpenCV Tutorials &mdash;&mdash; Image Moments

图像矩 Moments moments(InputArray array, bool binaryImage=false ) Parameters: array – Raster image (single-channel, 8-bit or floating-point 2D array) or an array ( or ) of 2D points (Point or Point2f ). binaryImage – If it is true, all non-zero image pi

OpenCV Tutorials &mdash;&mdash; Hough Circle Transform

Hough 圆变换 和 Hough 直线变换原理相同,只是参数空间不同 : In the line detection case, a line was defined by two parameters . In the circle case, we need three parameters to define a circle: where define the center position (gree point) and is the radius, which allows us

OpenCV Tutorials &mdash;&mdash; Sobel Derivatives

图像边缘 -- 像素灰度值变换剧烈的点 You can easily notice that in an edge, the pixel intensity changes in a notorious way. A good way to expresschanges is by using derivatives. A high change in gradient indicates a major change in the image.   To be more graphical,