【发布时间】:2020-03-19 15:03:48
【问题描述】:
我是图像处理的新手,正在尝试编写用于腐蚀和膨胀的自定义方法。然后我尝试将我的结果与 OpenCV 腐蚀和膨胀函数结果进行比较。我给输入图像填充一个零,然后将内核与填充图像重叠。这是我的功能:
import numpy as np
import matplotlib.pyplot as plt
def operation(image, kernel, padding=0, operation=None):
if operation:
img_operated = image.copy() #this will be the image
"""
The add_padding function below will simply add padding to the image, so the new array with one padding will
look like ->
[[0,0,0,0,0,0,0,0],
[0,0,0,1,1,1,1,0],
[0,0,0,1,1,1,1,0],
[0,1,1,1,1,1,1,0],
[0,1,1,1,1,1,1,0],
[0,1,1,1,1,0,0,0],
[0,1,1,1,1,0,0,0],
[0,0,0,0,0,0,0,0]]
)
"""
image = add_padding(image, padding)
print("Image is \n", image)
print("kernel is \n",kernel)
print("="*40)
vertical_window = padded.shape[0] - kernel.shape[0] #final vertical window position
horizontal_window = padded.shape[1] - kernel.shape[1] #final horizontal window position
print("Vertical Window limit: {}".format(vertical_window))
print("Horizontal Window limit: {}".format(horizontal_window))
print("="*40)
#start with vertical window at 0 position
vertical_pos = 0
values = kernel.flatten() #to compare with values with overlapping element for erosion
#sliding the window vertically
while vertical_pos <= (vertical_window):
horizontal_pos = 0
#sliding the window horizontally
while horizontal_pos <= (horizontal_window):
dilation_flag = False
erosion_flag = False
index_position = 0
#gives the index position of the box
for i in range(vertical_pos, vertical_pos+kernel.shape[0]):
for j in range(horizontal_pos, horizontal_pos+kernel.shape[0]):
#First Case
if operation == "erosion":
if padded[i,j] == values[index_position]:
erosion_flag = True
index_position += 1
else:
erosion_flag = False
break
#Second Case
elif operation == "dilation":
#if we find 1, then break the second loop
if padded[i][j] == 1:
dilation_flag = True
break
else:
return "Operation not understood!"
#if opertion is erosion and there is no match found, break the first 'for' loop
if opr == "erosion" and erosion_flag is False:
break
#if operation is dilation and we find a match, then break the first 'for' loop
if opr == "dilation" and dilation_flag is True:
img_operated[vertical_pos, horizontal_pos] = 1
break
#Check whether erosion flag is true after iterating over one complete overlap
if operation == "erosion" and erosion_flag is True:
img_operated[vertical_pos, horizontal_pos] = 1
elif operation == "erosion" and erosion_flag is False:
img_operated[vertical_pos, horizontal_pos] = 0
#increase the horizontal window position
horizontal_pos += 1
#increase the vertical window position
vertical_pos += 1
return img_operated
return "Operation Required!"
array = np.array([[0,0,1,1,1,1],
[0,0,1,1,1,1],
[1,1,1,1,1,1],
[1,1,1,1,1,1],
[1,1,1,1,0,0],
[1,1,1,1,0,0]], dtype=np.uint8)
kernel = np.array ([[0, 1, 0],
[1, 1, 1],
[0, 1, 0]], dtype = np.uint8)
#image will be padded with one zeros around
result_erosion = operation(array, kernel, 1, "erosion")
result_dilation = operation(array, kernel, 1, "dilation")
#CV2 Erosion and Dilation
cv2_erosion = cv2.erode(array, kernel, iterations=1)
cv2_dilation = cv2.dilate(array, kernel, iterations=1)
膨胀结果匹配,但腐蚀结果不匹配。我不确定为什么会这样。是因为一些填充问题吗? OpenCV 是否填充图像?还是我错误地执行了腐蚀方法?这是结果的图像:
【问题讨论】:
-
index_position 背后的魔法是什么?为什么它不用于扩张?我想这就是问题的根源
-
侵蚀与膨胀正好相反。膨胀以假开始,如果任何像素为 1,则将其设置为真。侵蚀将从 true 开始,如果任何像素为 0,则将其设置为 false。此外,您可能希望使用 1 来填充腐蚀。
-
@tstanisl 我假设对于侵蚀,我们需要与内核完全匹配。 'values' 数组包含内核的所有值。每次 for 循环运行时,我都会检查图像中的值是否与内核中相同位置的值相同。 'index_position' 变量只是给我内核在某个位置的值(从 0 开始,可以一直到 8)
-
@CrisLuengo 我对你关于侵蚀的解释有点困惑。我假设的是,如果与内核完全匹配,我用 1 替换该位置的新图像中的值,否则我用 0 替换该值。你能详细说明“如果有像素”是什么意思吗为 0,然后将其设置为 false'。谢谢。
标签: python numpy opencv image-processing image-morphology