E
E
Elisey Vlasenko2020-07-06 09:02:47
Python
Elisey Vlasenko, 2020-07-06 09:02:47

How to understand [:,:0] in python?

on the site https://realpython.com/pysimplegui-python/ somewhere far below there is a piece of code

Code:

while True:

        event, values = window.read(timeout=20)

        if event == "Exit" or event == sg.WIN_CLOSED:

            break


        ret, frame = cap.read()


        if values["-THRESH-"]:

            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB)[:, :, 0]

            frame = cv2.threshold(

                frame, values["-THRESH SLIDER-"], 255, cv2.THRESH_BINARY

            )[1]

        elif values["-CANNY-"]:

            frame = cv2.Canny(

                frame, values["-CANNY SLIDER A-"], values["-CANNY SLIDER B-"]

            )

        elif values["-BLUR-"]:

            frame = cv2.GaussianBlur(frame, (21, 21), values["-BLUR SLIDER-"])

        elif values["-HUE-"]:

            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

            frame[:, :, 0] += int(values["-HUE SLIDER-"])

            frame = cv2.cvtColor(frame, cv2.COLOR_HSV2BGR)

        elif values["-ENHANCE-"]:

            enh_val = values["-ENHANCE SLIDER-"] / 40

            clahe = cv2.createCLAHE(clipLimit=enh_val, tileGridSize=(8, 8))

            lab = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB)

            lab[:, :, 0] = clahe.apply(lab[:, :, 0])

            frame = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)


        imgbytes = cv2.imencode(".png", frame)[1].tobytes()

        window["-IMAGE-"].update(data=imgbytes)


    window.close()


main()

and I can't figure out how code lines 104 and 110 work
frame[:, :, 0] += int(values["-HUE SLIDER-"]) # 104
lab[:, :, 0] = clahe.apply(lab[:, :, 0]) # 110

What the [:. :. 0]?? I see this for the first time.
What should be the array (or not the array) for this to work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Pankov, 2020-07-06
@homus32

I will supplement the answer of @sergey-gornostaev a little.
This is a slice, or rather even two.
The fact is that in python objects can support the __getitem__ protocol, this method is called when the object is used with square brackets on the right. The value in square brackets will be passed as an argument to this method.
There are two nuances here:
In a normal python expression, a comma means that the result of the expression is a tuple. That. what is calculated between the commas will be the elements of the tuple: The second nuance is that in square brackets, python supports a special kind of "syntactic sugar" called slices.
x=1,2,3
You really should read about slices in a book. In general, a slice has three arguments, all optional. If there are three arguments, then the cut "under sugar" looks like this: 3:10:2- "from the third inclusive to the tenth, exclusively with step 2".
Slice 3::2- means "from the third inclusive to the last inclusive with a step of 2"
The step can be omitted, by default it is 1 and then the second colon is not needed. You can also leave out the beginning of the slice (such as first). So :means slice "from start inclusive to end inclusive with step 1".
Such slices can be specified separated by commas.
In fact, a frame is a three-dimensional array. The first two dimensions are the height and width, the third are the color components.
In your example, the operation is performed on all pixels from top to bottom, across the entire width but in a specific channel.
Really, read the book. Such things need to be mastered sequentially, and not in one fell swoop.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question