How to do zero-padding in one line with OpenCV

I'm sota, a local go apprentice. Since I am a beginner, I think there are many mistakes, but I would appreciate it if you could let me know in the comments.

I often implement padding in research and bytes, but every time I researched what to do with pillow and opencv, I used it for implementation for almost an hour, but as the title says, it can be implemented in one line with opencv. I understand it, so I will leave it as a memorandum.

cv2.copyMakeBorder()

Specifically, use cv2.copyMakeBorder (). How to use

output = cv2.copyMakeBorder(src, top, bottom, left, right, borderType, value)

is. --src is the input image --top, bottom, left, right are how many pixels are added to the top, bottom, left, and right of the image. -- borderType is a flag to specify the type of boundary to add. If you want so-called padding, select cv2.BORDER_CONSTANT. --There are also cv2.BORDER_REFLECT, cv2.BORDER_DEFAULT, cv2.BORDER_REPLICATE, cv2.BORDER_WRAP, etc., but they are a little different from padding, so please refer to the official document. http://labs.eecs.tottori-u.ac.jp/sd/Member/oyamada/OpenCV/html/py_tutorials/py_core/py_basic_ops/py_basic_ops.html --value specifies the color of the padded part. (0,0,0) is black, (255,255,255) is white, and so on.

Implementation example


import cv2

img = cv2.imread('./fig.png')

img_pad = cv2.copyMakeBorder(img, 50, 50, 50, 50, cv2.BORDER_CONSTANT, (0,0,0))

cv2.imwrite('./fig_pad.png', img_pad)

I'm padding safely. It was surprisingly easy considering the time and effort of creating a black image and pasting it while thinking about the location with slices each time. I will pad in this way in opencv from now on.

Recommended Posts

How to do zero-padding in one line with OpenCV
How to do hash calculation with salt in Python
[Python] How to do PCA in Python
How to convert 0.5 to 1056964608 in one shot
How to embed multiple embeds in one message with Discord.py
How to check ORM behavior in one file with django
How to do arithmetic with Django template
How to work with BigQuery in Python
How to do portmanteau test with python
How to check opencv version in python
How to do Server-Sent Events in Django
How to display legend marks in one with Python 2D plot
How to draw OpenCV images in Pygame
How to calculate "xx time" in one shot with Python timedelta
How to deal with memory leaks in matplotlib.pyplot
How to receive command line arguments in Python
How to send a message to LINE with curl
[REAPER] How to play with Reascript in Python
How to do multi-core parallel processing with python
How to crop an image with Python + OpenCV
How to deal with run-time errors in subprocess.call
How to use tkinter with python in pyenv
[Python] How to handle Japanese characters with openCV
How to drop Google Docs in one folder in a .txt file with python
How to convert / restore a string with [] in python
[Python] How to draw a line graph with Matplotlib
How to define Decorator and Decomaker in one function
Explain in detail how to make sounds with python
How to deal with pyenv initialization failure in fish 3.1.0
How to run tests in bulk with Python unittest
How to load files in Google Drive with Google Colaboratory
How to access with cache when reading_json in pandas
How to loop and play gif video with openCV
How to deal with Executing transaction: failed in Anaconda
How to get RGB and HSV histograms in OpenCV
How to start the code written in Atom with one command without starting teminal
How to update with SQLAlchemy?
How to cast with Theano
How to Alter with SQLAlchemy?
How to separate strings with','
How to RDP with Fedora31
How to develop in Python
How to Delete with SQLAlchemy?
How to install NPI + send a message to line with python
How to adapt multiple machine learning libraries in one shot
[In one line] Visualize like a lawn with just Pandas
[TensorFlow 2 / Keras] How to run learning with CTC Loss in Keras
How to output a document in pdf format with Sphinx
How to extract any appointment in Google Calendar with Python
[Python] How to write an if statement in one sentence.
[Django] How to give input values in advance with ModelForm
How to manipulate the DOM in an iframe with Selenium
How to do Bulk Update with PyMySQL and notes [Python]
How to specify command line arguments when debugging in PyCharm
[AWS] How to deal with "Invalid codepoint" error in CloudSearch
For beginners, how to deal with common errors in keras
How to create dataframes and mess with elements in pandas
Try HeloWorld in your own language (with How to & code)
How to log in to AtCoder with Python and submit automatically
How to do "Lending and borrowing JS methods (the one who uses apply)" in Python
How to write type hints for variables that are assigned multiple times in one line