[PYTHON] I checked the options of copyMakeBorder of OpenCV

at first

Filtering processing (Blur, Filter) in OpenCV defines a specific area and processes the image (blurring / noise removal) by taking the sum of them. At this time, the area is expanded and calculated for the edge of the image, but I investigated how to expand it.

Operating environment

Python3,OpenCV

Enlarge with a constant value (BORDER_CONSTANT)

Extend with black (0, 0, 0)

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt

black = [0x00, 0x00, 0x00]
dimgray = [0x69, 0x69, 0x69]
gray = [0x80, 0x80, 0x80]
darkgray = [0xa9, 0xa9, 0xa9]
silver = [0xc0, 0xc0, 0xc0]
lighgray  = [0xd3, 0xd3, 0xd3]
gainsboro = [0xdc, 0xdc, 0xdc]
whiteSmoke = [0xf5, 0xf5, 0xf5]
white = [0xFF, 0xFF, 0xFF]

img = np.array([
                [black, white, black, white, black]
                ,[white, dimgray, white, dimgray, white]
                ,[gray, white, gray, white, gray]
                ,[white, darkgray, white, darkgray, white]
                ,[silver, white, silver, white, silver]
                ,[white, lighgray, white, lighgray, white]
                ,[gainsboro, white, gainsboro, white, gainsboro]
                ,[white, whiteSmoke, white, whiteSmoke, white]
                ])
#Extended pixel count
exPixelNum = 8

plt.subplot(121),plt.imshow(img),plt.imshow(img),plt.title('Original')
conBdImg = cv.copyMakeBorder(img, exPixelNum, exPixelNum, exPixelNum, exPixelNum, cv.BORDER_CONSTANT)
plt.subplot(122),plt.imshow(conBdImg),plt.title('BORDER_CONSTANT')
plt.show()
Screen Shot 2020-05-17 at 16.02.45.png

Multiple pixels on the opposite side (BORDER_WRAP)

#Original image (extended in black for comparison)
orgImg = cv.copyMakeBorder(img, exPixelNum, exPixelNum, exPixelNum, exPixelNum, cv.BORDER_CONSTANT)
plt.subplot(121),plt.imshow(orgImg),plt.title('Original')
plt.xticks([]), plt.yticks([])

#Wrap image
wrpBdImg = cv.copyMakeBorder(img, exPixelNum, exPixelNum, exPixelNum, exPixelNum, cv.BORDER_WRAP)
plt.subplot(122),plt.imshow(wrpBdImg),plt.title('BORDER_WRAP')
plt.xticks([]), plt.yticks([])
plt.show()
Screen Shot 2020-05-17 at 16.23.55.png

As shown in the figure below, you can see that the target image is repeatedly arranged.

BorderWrapDiff.png

Copy edge pixels (BORDER_REPLICATE)

#Replicate image
repBdImg = cv.copyMakeBorder(img, exPixelNum, exPixelNum, exPixelNum, exPixelNum, cv.BORDER_REPLICATE)
plt.subplot(122),plt.imshow(repBdImg),plt.title('BORDER_REPLICATE')
plt.xticks([]), plt.yticks([])
plt.show()
Screen Shot 2020-05-17 at 16.42.32.png

You can see that the end pixels are used as they are as shown in the figure below.

BorderReplace.png

Reflection (BORDER_REFLECT)

#Replicate image
#Reflect image
refBdImg = cv.copyMakeBorder(img, exPixelNum, exPixelNum, exPixelNum, exPixelNum, cv.BORDER_REFLECT)
plt.subplot(122),plt.imshow(refBdImg),plt.title('BORDER_REFLECT')
plt.xticks([]), plt.yticks([])
plt.show()
Screen Shot 2020-05-17 at 17.36.40.png

As shown in the figure below, you can see that the image is placed in reverse.

BorderReflect.png

Reflection (BORDER_REFLECT_101)

#Reflect image (does not repeat edge pixels)
# BORDER_The same processing is performed when DEFAULT is used.
ref101BdImg = cv.copyMakeBorder(img, exPixelNum, exPixelNum, exPixelNum, exPixelNum, cv.BORDER_REFLECT_101)
plt.subplot(122),plt.imshow(ref101BdImg),plt.title('BORDER_REFLECT_101')
plt.xticks([]), plt.yticks([])
plt.show()
Screen Shot 2020-05-17 at 17.47.13.png

Same reflection as BORDER_REFLECT, but does not repeat edge pixels The same processing is performed when BORDER_REFLECT_101 is set to BORDER_DEFAULT.

BorderReflect101.png

Recommended Posts

I checked the options of copyMakeBorder of OpenCV
I checked the contents of docker volume
I checked the list of shortcut keys of Jupyter
I checked the session retention period of django
I checked the processing speed of numpy one-dimensionalization
I tried using the image filter of OpenCV
I checked the output specifications of PyTorch's Bidirectional LSTM
I checked out the versions of Blender and Python
I checked the default OS and shell of docker-machine
I tried "gamma correction" of the image with Python + OpenCV
I investigated the mechanism of flask-login!
I tried using GrabCut of OpenCV
I checked the gift tax amount
I want to read the html version of "OpenCV-Python Tutorials" OpenCV 3.1 version
I want to check the position of my face with OpenCV!
I checked the usage status of the parking lot from satellite images.
I checked the image of Science University on Twitter with Word2Vec.
I tried the asynchronous server of Django 3.0
I summarized the folder structure of Flask
I didn't know the basics of Python
The Python project template I think of.
I read the implementation of golang channel
I checked the number of closed and opened stores nationwide by Corona
I tried "smoothing" the image with Python + OpenCV
I tried the pivot table function of pandas
I checked the library for using the Gracenote API
Check the operation of OpenCV3 installed by Anaconda
I tried cluster analysis of the weather map
I tried'Beauty'with OpenCV
I read the implementation of range (Objects / rangeobject.c)
I tried "differentiating" the image with Python + OpenCV
I solved the deepest problem of Hiroshi Yuki.
The behavior of signal () depends on the compile options
I tried to touch the API of ebay
I tried to correct the keystone of the image
Try the free version of Progate [Python I]
I tried "binarizing" the image with Python + OpenCV
I touched some of the new features of Python 3.8 ①
I want to crop the image along the contour instead of the rectangle [python OpenCV]
I read and implemented the Variants of UKR
I want to customize the appearance of zabbix
I checked the calendar deleted in Qiita Advent Calendar 2016
[Python + OpenCV] Whiten the transparent part of the image
I tried to predict the price of ETF
I tried to vectorize the lyrics of Hinatazaka46!
[SLAYER] I visualized the lyrics of thrash metal and checked the soul of steel [Word Cloud]
I tried to extract features with SIFT of OpenCV
I want to grep the execution result of strace
I tried to summarize the basic form of GPLVM
I tried the MNIST tutorial for beginners of tensorflow.
Optical Flow, the dynamics of images captured by OpenCV
[OpenCV / Python] I tried image analysis of cells with OpenCV
I followed the implementation of the du command (first half)
I compared the identity of the images by Hu moment
Maybe I overestimated the impact of ShellShock on CGI
I want to fully understand the basics of Bokeh
The performance of PHP was better than I expected
I examined the argument class_weight of Chainer's softmax_cross_entropy function.
I measured the performance of 1 million documents with mongoDB
I followed the implementation of the du command (second half)
I tried using the API of the salmon data project