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.
Python3,OpenCV
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()
#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()
As shown in the figure below, you can see that the target image is repeatedly arranged.
#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()
You can see that the end pixels are used as they are as shown in the figure below.
#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()
As shown in the figure below, you can see that the image is placed in reverse.
#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()
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.
Recommended Posts