Загрузка
Prev Tutorial: Making your own linear filters!
Next Tutorial: Sobel Derivatives
| Original author | Ana Huamán |
| Compatibility | OpenCV >= 3.0 |
In this tutorial you will learn how to:
In this tutorial, we will briefly explore two ways of defining the extra padding (border) for an image:
This will be seen more clearly in the Code section.
Let the user choose what kind of padding use in the input image. There are two options:
The user chooses either option by pressing 'c' (constant) or 'r' (replicate)
The tutorial code's is shown lines below.
You can also download it from here
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
// Declare the variables
Mat src, dst;
int top, bottom, left, right;
int borderType = BORDER_CONSTANT;
const char* window_name = "copyMakeBorder Demo";
RNG rng(12345);
int main( int argc, char** argv )
{
const char* imageName = argc >=2 ? argv[1] : ;
= ( ::( ), );
( .()) {
();
();
-;
}
( );
( );
( );
( );
( );
( , );
= () (*.); = ;
= () (*.); = ;
(;;)
{
( .(, ), .(, ), .(, ) );
( , , , , , , , );
( , );
= ()();
( == )
{ ; }
( == )
{ = ; }
( == )
{ = ; }
}
;
}You can also download it from here
import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import java.util.Random;
class CopyMakeBorderRun {
public void run(String[] args) {
// Declare the variables
Mat src, dst = new Mat();
int top, bottom, left, right;
int borderType = Core.BORDER_CONSTANT;
String = ;
;
= ((. > ) ? [] : );
= .(, .);
( .() ) {
..();
..();
.(-);
}
..( +
+
+
+
+
);
.( , . );
= () (*.()); = ;
= () (*.()); = ;
( ) {
= ();
= ( .(),
.(), .() );
.( , , , , , , , );
.( , );
= () .();
= .();
( == )
{ ; }
( == )
{ = .;}
( == )
{ = .;}
}
.();
}
}
{
([] ) {
.(.);
().();
}
}You can also download it from here
"""
@file copy_make_border.py
@brief Sample code that shows the functionality of copyMakeBorder
"""
import sys
from random import randint
import cv2 as cv
def main(argv):
borderType = cv.BORDER_CONSTANT
window_name = "copyMakeBorder Demo"
imageName = argv[0] if len(argv) > 0 else 'lena.jpg'
# Loads an image
src = cv.imread(cv.samples.findFile(imageName), cv.)
#
:
()
()
-
(
)
.(, .)
= ( * .[]) # [] =
=
= ( * .[]) # [] =
=
:
= [(, ), (, ), (, )]
= .(, , , , , , , )
.(, )
= .()
== :
== : # = ()
= .
== : # = ()
= .
== :
(.[:])First we declare the variables we are going to use:
// Declare the variables
Mat src, dst;
int top, bottom, left, right;
int borderType = BORDER_CONSTANT;
const char* window_name = "copyMakeBorder Demo";
RNG rng(12345); // Declare the variables
Mat src, dst = new Mat();
int top, bottom, left, right;
int borderType = Core.BORDER_CONSTANT;
String window_name = "copyMakeBorder Demo";
Random rng; borderType = cv.BORDER_CONSTANT
window_name = "copyMakeBorder Demo"Especial attention deserves the variable rng which is a random number generator. We use it to generate the random border color, as we will see soon.
As usual we load our source image src:
const char* imageName = argc >=2 ? argv[1] : "lena.jpg";
// Loads an image
src = imread( samples::findFile( imageName ), IMREAD_COLOR ); // Load an image
// Check if image is loaded fine
if( src.empty()) {
printf(" Error opening image\n");
printf(" Program Arguments: [image_name -- default lena.jpg] \n");
return -1;
} String imageName = ((args.length > 0) ? args[0] : "../data/lena.jpg");
// Load an image
src = Imgcodecs.imread(imageName, Imgcodecs.IMREAD_COLOR);
// Check if image is loaded fine
if( src.empty() ) {
System.out.println("Error opening image!");
System.out.println("Program Arguments: [image_name -- default ../data/lena.jpg] \n");
System.exit(-1);
} imageName = argv[0] if len(argv) > 0 else 'lena.jpg'
# Loads an image
src = cv.imread(cv.samples.findFile(imageName), cv.IMREAD_COLOR)
# Check if image is loaded fine
if src is None:
print ('Error opening image!')
print ('Usage: copy_make_border.py [image_name -- default lena.jpg] \n')
return -1After giving a short intro of how to use the program, we create a window:
namedWindow( window_name, WINDOW_AUTOSIZE ); HighGui.namedWindow( window_name, HighGui.WINDOW_AUTOSIZE ); cv.namedWindow(window_name, cv.WINDOW_AUTOSIZE)Now we initialize the argument that defines the size of the borders (top, bottom, left and right). We give them a value of 5% the size of src.
// Initialize arguments for the filter
top = (int) (0.05*src.rows); bottom = top;
left = (int) (0.05*src.cols); right = left; // Initialize arguments for the filter
top = (int) (0.05*src.rows()); bottom = top;
left = (int) (0.05*src.cols()); right = left; top = int(0.05 * src.shape[0]) # shape[0] = rows
bottom = top
left = int(0.05 * src.shape[1]) # shape[1] = cols
right = leftThe program runs in an infinite loop while the key ESC isn't pressed. If the user presses 'c' or 'r', the borderType variable takes the value of BORDER_CONSTANT or BORDER_REPLICATE respectively:
char c = (char)waitKey(500);
if( c == 27 )
{ break; }
else if( c == 'c' )
{ borderType = BORDER_CONSTANT; }
else if( c == 'r' )
{ borderType = BORDER_REPLICATE; } char c = (char) HighGui.waitKey(500);
c = Character.toLowerCase(c);
if( c == 27 )
{ break; }
else if( c == 'c' )
{ borderType = Core.BORDER_CONSTANT;}
else if( c == 'r' )
{ borderType = Core.BORDER_REPLICATE;} c = cv.waitKey(500)
if c == 27:
break
elif c == 99: # 99 = ord('c')
borderType = cv.BORDER_CONSTANT
elif c == 114: # 114 = ord('r')
borderType = cv.BORDER_REPLICATEIn each iteration (after 0.5 seconds), the random border color (value) is updated...
Scalar value( rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255) ); rng = new Random();
Scalar value = new Scalar( rng.nextInt(256),
rng.nextInt(256), rng.nextInt(256) ); value = [randint(0, 255), randint(0, 255), randint(0, 255)]This value is a set of three numbers picked randomly in the range \([0,255]\).
Finally, we call the function copyMakeBorder() to apply the respective padding:
copyMakeBorder( src, dst, top, bottom, left, right, borderType, value ); Core.copyMakeBorder( src, dst, top, bottom, left, right, borderType, value); dst = cv.copyMakeBorder(src, top, bottom, left, right, borderType, None, value)We display our output image in the image created previously
imshow( window_name, dst ); HighGui.imshow( window_name, dst ); cv.imshow(window_name, dst)After compiling the code above, you can execute it giving as argument the path of an image. The result should be:
Below some screenshot showing how the border changes color and how the BORDER_REPLICATE option looks: