mirror of
https://github.com/opencv/opencv.git
synced 2026-07-30 15:53:03 +04:00
Tutorial Make Border
This commit is contained in:
@@ -11,9 +11,10 @@
|
||||
using namespace cv;
|
||||
|
||||
//![variables]
|
||||
// Declare the variables
|
||||
Mat src, dst;
|
||||
int top, bottom, left, right;
|
||||
int borderType;
|
||||
int borderType = BORDER_CONSTANT;
|
||||
const char* window_name = "copyMakeBorder Demo";
|
||||
RNG rng(12345);
|
||||
//![variables]
|
||||
@@ -23,65 +24,61 @@ RNG rng(12345);
|
||||
*/
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
//![load]
|
||||
String imageName("../data/lena.jpg"); // by default
|
||||
if (argc > 1)
|
||||
{
|
||||
imageName = argv[1];
|
||||
}
|
||||
src = imread( imageName, IMREAD_COLOR ); // Load an image
|
||||
//![load]
|
||||
const char* imageName = argc >=2 ? argv[1] : "../data/lena.jpg";
|
||||
|
||||
if( src.empty() )
|
||||
{
|
||||
printf(" No data entered, please enter the path to an image file \n");
|
||||
return -1;
|
||||
// Loads an image
|
||||
src = imread( 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 ../data/lena.jpg] \n");
|
||||
return -1;
|
||||
}
|
||||
//![load]
|
||||
//![load]
|
||||
|
||||
/// 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");
|
||||
// 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, WINDOW_AUTOSIZE );
|
||||
//![create_window]
|
||||
//![create_window]
|
||||
namedWindow( window_name, WINDOW_AUTOSIZE );
|
||||
//![create_window]
|
||||
|
||||
//![init_arguments]
|
||||
/// 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);
|
||||
//![init_arguments]
|
||||
//![init_arguments]
|
||||
// Initialize arguments for the filter
|
||||
top = (int) (0.05*src.rows); bottom = top;
|
||||
left = (int) (0.05*src.cols); right = left;
|
||||
//![init_arguments]
|
||||
|
||||
dst = src;
|
||||
imshow( window_name, dst );
|
||||
for(;;)
|
||||
{
|
||||
//![update_value]
|
||||
Scalar value( rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255) );
|
||||
//![update_value]
|
||||
|
||||
for(;;)
|
||||
{
|
||||
//![check_keypress]
|
||||
char c = (char)waitKey(500);
|
||||
if( c == 27 )
|
||||
{ break; }
|
||||
else if( c == 'c' )
|
||||
{ borderType = BORDER_CONSTANT; }
|
||||
else if( c == 'r' )
|
||||
{ borderType = BORDER_REPLICATE; }
|
||||
//![check_keypress]
|
||||
//![copymakeborder]
|
||||
copyMakeBorder( src, dst, top, bottom, left, right, borderType, value );
|
||||
//![copymakeborder]
|
||||
|
||||
//![update_value]
|
||||
Scalar value( rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255) );
|
||||
//![update_value]
|
||||
//![display]
|
||||
imshow( window_name, dst );
|
||||
//![display]
|
||||
|
||||
//![copymakeborder]
|
||||
copyMakeBorder( src, dst, top, bottom, left, right, borderType, value );
|
||||
//![copymakeborder]
|
||||
//![check_keypress]
|
||||
char c = (char)waitKey(500);
|
||||
if( c == 27 )
|
||||
{ break; }
|
||||
else if( c == 'c' )
|
||||
{ borderType = BORDER_CONSTANT; }
|
||||
else if( c == 'r' )
|
||||
{ borderType = BORDER_REPLICATE; }
|
||||
//![check_keypress]
|
||||
}
|
||||
|
||||
//![display]
|
||||
imshow( window_name, dst );
|
||||
//![display]
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* @file CopyMakeBorder.java
|
||||
* @brief Sample code that shows the functionality of copyMakeBorder
|
||||
*/
|
||||
|
||||
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) {
|
||||
|
||||
//! [variables]
|
||||
// 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;
|
||||
//! [variables]
|
||||
|
||||
//! [load]
|
||||
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);
|
||||
}
|
||||
//! [load]
|
||||
|
||||
// Brief how-to for this program
|
||||
System.out.println("\n" +
|
||||
"\t copyMakeBorder Demo: \n" +
|
||||
"\t -------------------- \n" +
|
||||
" ** Press 'c' to set the border to a random constant value \n" +
|
||||
" ** Press 'r' to set the border to be replicated \n" +
|
||||
" ** Press 'ESC' to exit the program \n");
|
||||
|
||||
//![create_window]
|
||||
HighGui.namedWindow( window_name, HighGui.WINDOW_AUTOSIZE );
|
||||
//![create_window]
|
||||
|
||||
//! [init_arguments]
|
||||
// Initialize arguments for the filter
|
||||
top = (int) (0.05*src.rows()); bottom = top;
|
||||
left = (int) (0.05*src.cols()); right = left;
|
||||
//! [init_arguments]
|
||||
|
||||
while( true ) {
|
||||
//! [update_value]
|
||||
rng = new Random();
|
||||
Scalar value = new Scalar( rng.nextInt(256),
|
||||
rng.nextInt(256), rng.nextInt(256) );
|
||||
//! [update_value]
|
||||
|
||||
//! [copymakeborder]
|
||||
Core.copyMakeBorder( src, dst, top, bottom, left, right, borderType, value);
|
||||
//! [copymakeborder]
|
||||
//! [display]
|
||||
HighGui.imshow( window_name, dst );
|
||||
//! [display]
|
||||
|
||||
//![check_keypress]
|
||||
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;}
|
||||
//![check_keypress]
|
||||
}
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
public class CopyMakeBorder {
|
||||
public static void main(String[] args) {
|
||||
// Load the native library.
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
new CopyMakeBorderRun().run(args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
"""
|
||||
@file copy_make_border.py
|
||||
@brief Sample code that shows the functionality of copyMakeBorder
|
||||
"""
|
||||
import sys
|
||||
from random import randint
|
||||
import cv2
|
||||
|
||||
|
||||
def main(argv):
|
||||
## [variables]
|
||||
# First we declare the variables we are going to use
|
||||
borderType = cv2.BORDER_CONSTANT
|
||||
window_name = "copyMakeBorder Demo"
|
||||
## [variables]
|
||||
## [load]
|
||||
imageName = argv[0] if len(argv) > 0 else "../data/lena.jpg"
|
||||
|
||||
# Loads an image
|
||||
src = cv2.imread(imageName, cv2.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 ../data/lena.jpg] \n')
|
||||
return -1
|
||||
## [load]
|
||||
# Brief how-to for this program
|
||||
print ('\n'
|
||||
'\t copyMakeBorder Demo: \n'
|
||||
' -------------------- \n'
|
||||
' ** Press \'c\' to set the border to a random constant value \n'
|
||||
' ** Press \'r\' to set the border to be replicated \n'
|
||||
' ** Press \'ESC\' to exit the program ')
|
||||
## [create_window]
|
||||
cv2.namedWindow(window_name, cv2.WINDOW_AUTOSIZE)
|
||||
## [create_window]
|
||||
## [init_arguments]
|
||||
# Initialize arguments for the filter
|
||||
top = int(0.05 * src.shape[0]) # shape[0] = rows
|
||||
bottom = top
|
||||
left = int(0.05 * src.shape[1]) # shape[1] = cols
|
||||
right = left
|
||||
## [init_arguments]
|
||||
while 1:
|
||||
## [update_value]
|
||||
value = [randint(0, 255), randint(0, 255), randint(0, 255)]
|
||||
## [update_value]
|
||||
## [copymakeborder]
|
||||
dst = cv2.copyMakeBorder(src, top, bottom, left, right, borderType, None, value)
|
||||
## [copymakeborder]
|
||||
## [display]
|
||||
cv2.imshow(window_name, dst)
|
||||
## [display]
|
||||
## [check_keypress]
|
||||
c = cv2.waitKey(500)
|
||||
|
||||
if c == 27:
|
||||
break
|
||||
elif c == 99: # 99 = ord('c')
|
||||
borderType = cv2.BORDER_CONSTANT
|
||||
elif c == 114: # 114 = ord('r')
|
||||
borderType = cv2.BORDER_REPLICATE
|
||||
## [check_keypress]
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
||||
Reference in New Issue
Block a user