0

I'm in the process of learning some ML concepts using OpenCV, and I have a piece of python code that I was given to translate into c++. I have a very basic knowledge of python, and I've run into some syntax that I can't seem to find the meaning for.

I have a variable being passed into a method (whole method not shown) that is coming from the result of cv2.imread(), so an image. In c++, it's of type Mat:

def preprocess_image(img, side = 96):
    min_side = min(img.shape[0], img.shape[1])
    img = img[:min_side, :min_side * 2]

I have a couple questions:

  1. What does the syntax ":min_side" do?
  2. What is that line doing in terms of the image?
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
rallen911
  • 148
  • 9
  • I've never seen that usage before, but it looks like slice notation. – RufusVS Oct 29 '21 at 03:14
  • This is a slicing syntax that does not work with base python but [does work with numpy arrays](https://stackoverflow.com/questions/9972391/python-array-slice-with-comma). It is basically a way of slicing a multi-dimensional list. – 0x263A Oct 29 '21 at 03:21
  • 2
    To be more specific to my "slice notation" comment above, it is not a : preceding a variable name, but a : separating a start and end index for a list. When the start index is not explicit, it is the start of list. By the same reason, a colon after an identifier means end of list. a colon alone means the entire list. – RufusVS Oct 29 '21 at 03:26
  • this should be familiar to anyone who's ever used slicing in plain python, with numpy, or with opencv. it is explained in all the basic tutorials that cover these uses. – Christoph Rackwitz Oct 29 '21 at 09:02

3 Answers3

1

The line:

img = img[:min_side, :min_side * 2]

is cropping the image so that the resulting image is min_side in height and min_side * 2 in width. The colon preceding a variable name is python's slicing syntax. Observe:

arr = [1, 2, 3, 4, 5, 6]
length = 4
print(arr[:length])

Output:

[1, 2, 3, 4]
Red
  • 26,798
  • 7
  • 36
  • 58
1

:min_side is a shorthand for 0:min_side i.e it produces a slice the object from the start to min_side. For example:

 f = [2, 4, 5, 6, 8, 9]
 f[:3]  # returns [2,4,5]

img = img[:min_side, :min_side *2] produces a crop of the image (which is a numpy array) from 0 to min_side along the height and from 0 to min_side * 2 along the width. Therefore the resulting image would be one of width min_side * 2 and height min_side .

1

I am assuming the input of the image is a Matrix. In Python the image is generally read as numpy matrix

1.What does the syntax ":min_side" do?

  • It "Slice" the List/Array or basically in this case, a Matrix.

2.What is that line doing in terms of the image?

  • It "crops" the 2D Array(Basically a Matrix/Image)

A simple example of slicing:

x = np.array([[0, 1, 2],[3, 4, 5], [6, 7, 8]])
print(x)

out:

array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

Performing Slicing on this Matrix(Image):

x[:2, :3]

output after Slicing:

array([[0, 1, 2],
       [3, 4, 5]])

A good source to read more about it would be straight from the source: https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.indexing.html