Home > Enterprise >  Box inside an image - find what is not contained in that box and replace with zeroes (numpy)
Box inside an image - find what is not contained in that box and replace with zeroes (numpy)

Time:02-01

I am trying to replace, with zeroes, all elements of array that don't exist in seconds array. I got an array of RGBA image:

 [[101 120 115 255]
  [101 120 115 255]
  [101 120 115 255]
  ...
  [113 127 124 255]
  [112 126 123 255]
  [109 123 120 255]]

Then i got a variables X, Y and Height and Width of box that is contained inside that picture. With that variables, I am trying to replace everything that is not inside that box to white.

I got coordinates X, Y, Width, Height of white box inside black box (look at the picture below) and i want to replace everything that is not in that bounding box with 0. How could I do it?

enter image description here

CodePudding user response:

Create a matrix of 0, put 1 inside the "white box" indices, and multiply them. This will result in all other indices containing 0.

tmp = np.zeros_like(yourArr)
tmp[X:X   width,Y:Y   height] = 1
yourArr = yourArr * tmp
  •  Tags:  
  • Related