0

I need to black out part of the image. to do so I tried this code:

img2[0:0, 640:150] = [0, 0, 0]
img2[0:490, 640:640] = [0, 0, 0]

but it does not seem to be working. The image is a numpy array.

So my questions are:

  1. why does my image img2 look the same before and after the execution of these rows?
  2. I need to black out everything except a rectangle. i wanted to do so by drawing 4 rectangles on the outside. can this also be done by saying one time what I do NOT want to blacken? so basically the inverse of the range?
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
sharkyenergy
  • 3,842
  • 10
  • 46
  • 97

1 Answers1

1

I think, You need to know about slicing (link_1, link_2). If you select correct slicing only one assignment with 0 is enough.

>>> img_arr = np.random.rand(5,3,3)
>>> img_arr[1:3, 0:2, 0:3] = 0
# Or
>>> img_arr[1:3, :2, :] = 0
>>> img_arr
array([[[0.19946098, 0.42062458, 0.51795564],
        [0.0957362 , 0.26306843, 0.24824746],
        [0.63398966, 0.44752899, 0.37449257]],

       [[0.        , 0.        , 0.        ],
        [0.        , 0.        , 0.        ],
        [0.49413734, 0.07294475, 0.8341346 ]],

       [[0.        , 0.        , 0.        ],
        [0.        , 0.        , 0.        ],
        [0.18410631, 0.77498275, 0.42724167]],

       [[0.60114116, 0.73999382, 0.76348436],
        [0.49114468, 0.18131404, 0.01817003],
        [0.51479338, 0.41674903, 0.80151682]],

       [[0.67634706, 0.56007131, 0.68486408],
        [0.35607505, 0.51342861, 0.75062432],
        [0.44943936, 0.10768226, 0.62945455]]])
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
  • thank you, I understood the answer to my question 1), but I do not understand the answer to question 2). Also I dont seem to be able to understand why [1:3, 0:2, 0:3] = 0 produces the same as [1:3, :2, :] = 0. could you please elaborate a bit? thank you! – sharkyenergy Jun 15 '22 at 11:08
  • 1
    @sharkyenergy, Question_2 is about `object detection` or `semantic segmentation` and It needs to be read about it on the internet or taken a course. **Why `[0:2] == [ :2]` Or `[0:3] == [ : ]`?** Because in slicing we have [start:end: steps] and the start of array is `0` and in the example that I send end of the array is `3` so we can skip them and don't write them. – I'mahdi Jun 15 '22 at 11:14
  • 1
    clear thank you! as for question 2, let me rephrase it: i want to paint all black except one specific slice. to do so i need to paint 4 individual slices black. is it possible to pick a slice inverted? so define the slice I do NOT want to select? – sharkyenergy Jun 15 '22 at 11:50