gpt4 book ai didi

python - 自定义腐 eclipse 结果与 OpenCV 腐 eclipse 不匹配

转载 作者:行者123 更新时间:2023-12-02 07:49:35 25 4
gpt4 key购买 nike

我是图像处理新手,正在尝试编写一种用于腐 eclipse 和膨胀的自定义方法。然后我尝试将我的结果与 OpenCV 腐 eclipse 和膨胀函数结果进行比较。我对输入图像进行一处零填充,然后将内核与填充图像重叠。这是我的功能:

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)

膨胀结果匹配,但腐 eclipse 结果不匹配。我不知道为什么会这样。是因为某些填充问题吗? OpenCV 是否填充图像?或者我错误地实现了侵 eclipse 方法?这是结果的图像:

final results

最佳答案

您的代码存在两个问题:

  1. 您没有检查内核的值。对于膨胀来说,这并不重要,但您会看到不同输入图像的差异。

  2. 侵 eclipse 很困惑。正如我在评论中提到的,侵 eclipse 是扩张的完全逻辑逆。您可以将腐 eclipse 视为背景的膨胀:erosion(image) == ~dilation(~image)(~ 是图像的逻辑否定)。因此,您应该能够使用与膨胀完全相同的代码和逻辑来进行侵 eclipse ,但检查是否在内核中看到背景像素 (0),在这种情况下,您将输出中的该像素设置为背景 (0)。要复制 OpenCV 侵 eclipse 的结果,填充必须与前景 (1) 一起使用。

这是更正后的代码。我使用 OpenCV 编写了一个 add_padding 函数,因为它在 OP 中丢失了。代码可以显着简化,例如通过对两个操作使用单个标志;通过在函数顶部仅检查一次操作字符串,并设置一个变量,该变量的值是 0 或 1,以便在比较输入和修改输出时使用;并使用 for 循环而不是 while 循环来迭代图像。我会将这些更改留给感兴趣的读者。

import numpy as np
import matplotlib.pyplot as plt
import cv2

def add_padding(image, padding, value):
return cv2.copyMakeBorder(image, padding, padding, padding, padding, cv2.BORDER_CONSTANT, value=value)

def operation(image, kernel, padding=0, operation=None):
if operation:
img_operated = image.copy() #this will be the image

padding_value = 0 # <<< ADDED
if operation == "erosion": # <<< ADDED
padding_value = 1 # <<< ADDED
padded = add_padding(image, padding, padding_value) # <<< MODIFIED

vertical_window = padded.shape[0] - kernel.shape[0] #final vertical window position
horizontal_window = padded.shape[1] - kernel.shape[1] #final horizontal window position

#start with vertical window at 0 position
vertical_pos = 0

#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

#gives the index position of the box
for i in range(kernel.shape[0]): # <<< MODIFIED
for j in range(kernel.shape[1]): # <<< MODIFIED
if kernel[i][j] == 1: # <<< ADDED
#First Case
if operation == "erosion":
#if we find 0, then break the second loop
if padded[vertical_pos+i][horizontal_pos+j] == 0: # <<< MODIFIED
erosion_flag = True # <<< MODIFIED
break
#Second Case
elif operation == "dilation":
#if we find 1, then break the second loop
if padded[vertical_pos+i][horizontal_pos+j] == 1: # <<< MODIFIED
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 operation == "erosion" and erosion_flag: # <<< MODIFIED
img_operated[vertical_pos, horizontal_pos] = 0 # <<< ADDED
break

#if operation is dilation and we find a match, then break the first 'for' loop
if operation == "dilation" and dilation_flag: # <<< FIXED
img_operated[vertical_pos, horizontal_pos] = 1
break

# !!! Removed unnecessary checks here

#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)

关于python - 自定义腐 eclipse 结果与 OpenCV 腐 eclipse 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59012906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com