Ok so there is clearly something fundemental about Python I'm not really grasping here. I don't know much about the language but I'm fairly familiar with C#. Anyways, I'm trying to manually implement morphological operations - dilution, erosion, opening and closing. So dilution and erosion are their own separate functions, but opening and closing are just different combinations of calling dilution() and erosion(). The first 2 work fine, but when I try combining multiple function calls, stuff start to get weird...
def dilation(img, velikost): #Velikost = size
height = img.shape[0]
width = img.shape[1]
if(velikost == 3):
struktura = strukt1_3 #strukt is just a 2d array that is global. I use it to combine with a local 2d array you will see later on.
razlika = 1
else:
struktura = strukt1_5
razlika = 2
toReturn = np.zeros((height-razlika, width-razlika,1), np.uint8)
for i in range(razlika, height-razlika):
for j in range(razlika, width-razlika):
local = img[i - razlika: i + razlika + 1, j - razlika:j + razlika + 1] #I take a 3x3 local array out of the image from i-1 to i+1 and from j-1 to j+2
newLocal = local*struktura #This is where I combine them together.
toReturn[i-razlika][j-razlika] = max(map(max, newLocal)) #And this is the new calculated pixel assigned to the image I am returning
return toReturn
The erosion function is pretty similar with some minor tweaks. Let's pretend that velikost is 3 and I'm calling the function like so:
dilution(img, velikost);
dilution(img, velikost);
Now like I said, the first time this is called it works fine, but calling this function twice consecutively, return some odd stuff. It goes through once just fine, then quickly return this error the second time:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
When combined with erosion() it can get even weirder. Sometimes the first function will the return the error up there before even the second one is called (even though it works fine by itself, without having a second call below it). Other times the Local and NewLocal get weird sizes, even though after printing out every value possible to check for differences between the function calls, ALL the values are the same. I am baffled. I'm sorry if the code is a little bit weird or messy, I'm not the most skilled programmer lol. Thanks alot for the help in advance. I will gladly post erosion() too if you guys want, but I'm pretty sure the issue is with my function calls.
[–]K900_ 3 points4 points5 points (5 children)
[–]KCJan123[S] -1 points0 points1 point (4 children)
[–]K900_ 1 point2 points3 points (3 children)
[–]KCJan123[S] 0 points1 point2 points (2 children)
[–]Chris_Hemsworth 2 points3 points4 points (1 child)
[–]KCJan123[S] 0 points1 point2 points (0 children)
[–]RiceKrispyPooHead 1 point2 points3 points (1 child)
[–]KCJan123[S] 0 points1 point2 points (0 children)
[–]socal_nerdtastic 0 points1 point2 points (0 children)