Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Box inside an image – find what is not contained in that box and replace with zeroes (numpy)

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading