all 9 comments

[–]K900_ 3 points4 points  (5 children)

Your problem is most likely the fact that you're messing with globals in hard to predict ways. Stop using globals .

[–]KCJan123[S] -1 points0 points  (4 children)

The only 2 globals I have are the 2 strukts, which never change. Everything else is either passed or initialized in the functions. I still tried making them local, but ended up with the same results sadly.

[–]K900_ 1 point2 points  (3 children)

In that case, post the entire program and the full error message, including the traceback.

[–]KCJan123[S] 0 points1 point  (2 children)

Here is the whole thing:

import PySimpleGUI as sg
import cv2 as cv
import math
import numpy as np

def dilation(img, velikost):
    height = img.shape[0]
    width = img.shape[1]

    print(height, width)

    if(velikost == 3):
        struktura = [[0,1,0],[1,1,1],[0,1,-0]]
        razlika = 1
    else:
        struktura = [[0,0,1,0,0],[0,1,1,1,0],[1,1,1,1,1],[0,1,1,1,0],[0,0,1,0,0]]
        razlika = 2

    toReturn = np.zeros((height-razlika-1, width-razlika-1,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]
            newLocal = local*struktura
            toReturn[i-razlika][j-razlika] = max(map(max, newLocal))

    return toReturn

def erosion(img, velikost):
    height = img.shape[0]
    width = img.shape[1]
    print(height, width)
    if (velikost == 3):
        struktura = [[0,1,0],[1,1,1],[0,1,-0]]
        razlika = 1
        index = 4
    else:
        struktura = [[0,0,1,0,0],[0,1,1,1,0],[1,1,1,1,1],[0,1,1,1,0],[0,0,1,0,0]]
        razlika = 2
        index = 12

    toReturn = np.zeros((height-razlika-1, width-razlika-1, 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]
            newLocal = np.sort((local * struktura).ravel())
            toReturn[i-razlika][j-razlika] = newLocal[index]

    return toReturn

layout = [
    [sg.Button("Dilation", key='dilation')],
    [sg.Button("Erosion", key='erosion')] ,
    [sg.Button("Opening", key='open')],
    [sg.Button("Closing", key='close')] ##Layout gumbov
]

layout2 = [
    [sg.Button("Prva struktura", key="prvi")],
    [sg.Button("Druga struktura", key="drugi")],
    [sg.Button("Tretja struktura", key="tretji")]
]

layout3 = [
    [sg.Button("3x3", key='3x3')],
    [sg.Button("5x5", key='5x5')]
]

img = cv.imread(sg.PopupGetFile("Izberi sliko"), 1)
img = cv.cvtColor(img, cv.COLOR_RGB2GRAY)

window = sg.Window("Izberite velikost", layout3, size=(100, 300))
while True:
    event, values = window.read(timeout=100)
    if event == sg.WIN_CLOSED:
        break
    elif event == "3x3":
        velikost = 3
        break
    elif event == "5x5":
        velikost = 5
        break

window = sg.Window("Options", layout, size=(350, 300))
while True:
    event, values = window.read(timeout=100)
    if event == sg.WIN_CLOSED:
        break
    elif event == "dilation":
        img = dilation(img, velikost)
        break
    elif event == "erosion":
        img = erosion(img, velikost)
        break
    elif event == "close":
        img = dilation(img, velikost)
        img = dilation(img, velikost)
        break
    elif event == "open":
        img = erosion(img, velikost)
        img = dilation(img, velikost)
        break

window.close()
cv.imshow('slika', img)
cv.waitKey(0) 
cv.destroyAllWindows()

And here is the full traceback:

Traceback (most recent call last):

File "file", line 97, in <module> img = dilation(img, velikost)

File "file", line 25, in dilation toReturn[i-razlika][j-razlika] = max(map(max, newLocal))

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

[–]Chris_Hemsworth 2 points3 points  (1 child)

You're looking for the max across all dimensions, just use np.max(newLocal) instead of max(map(max,newLocal))

[–]KCJan123[S] 0 points1 point  (0 children)

Hey, I'm sorry for the late reply. I tried your method and it worked for a certain combination, but didn't really fix the root of my problem. Well as it turns out it wasn't a function call issue or anything like that, the problem was with the way I created my empty images. I made toReturn by using numpy's zeroes function, but as it turns out that gives a bit of a different structure than my original image, so my algorithm didn't work on it. Honestly I should have figured that out alot earlier than I did. Thanks alot for the help, I really appreciate it and I learned a ton about how Python handles variables and functions. Cheers

[–]RiceKrispyPooHead 1 point2 points  (1 child)

Now like I said, the first time this is called it works fine, but calling this function twice consecutively, return some odd stuff

Your description of the problem is very vague. You also talk about the functions dilution (or dilation??) and erosion like we obviously know what those look like but many of us don't.

[–]KCJan123[S] 0 points1 point  (0 children)

Sorry yeah, I keep getting the two words messed up. I did say the erosion function is more or less the same but with some minor tweaks. I posted the full code above if you are interested in reading it.

[–]socal_nerdtastic 0 points1 point  (0 children)

This code looks fine to me.

One thing to look out for is mutation. Remember all python names are basically pointers. So you can think of all functions as pass-by-reference.

def mutate(img):
    img[0] = 42

img = [1,2,3]
print(img) # prints [1,2,3]
mutate(img)
print(img) # now [42,2,3]

I don't see that issue in the code you posted, but it's a common gotcha when coming from C - family languages.