Python Forum
Using boolean mask in Numpy for 3D
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using boolean mask in Numpy for 3D
#1
I can generate a 8 x 8 x 4 matrix as follows using Numpy:

px = np.random.randint(1,254, (8,8,4),dtype=np.uint8)

This gives me 64 groups where each group has 4 values. This would be a very small CMYK image.
I will go much larger, hence the need for Numpy.
I want to use Boolean Masking to manipulate each group. Now focusing on the forth group, I would expect
my masking to look something like the following:

px[px[:, :, 3] >160] = 3 #(Here)

I saw something like this in 2D and I have tried to expand it to 3D. The above I would like it to do the following:
For every row and column in the 8 x 8 array, if the fourth element has a value greater than 160, assign a value of 3 to that element. Now, if I try this...

print([px[:, :, 3] >160])

It will give me a 8 x 8 True/False array that corresponds to the fourth element conditions as I would expect.

How does one write the line labeled (Here) so it will modify only the fourth element as I described?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I mask Hex value wiht 0xFF? korenron 2 4,456 Nov-23-2021, 09:13 AM
Last Post: Gribouillis
  Saving a mask as a png file in opencv DanTheMan 1 5,661 Jan-31-2020, 09:20 AM
Last Post: ThiefOfTime
  Subnet Mask Ranges ab52 0 1,812 Mar-11-2019, 10:39 AM
Last Post: ab52
  Create a List for Boolean Mask. leoahum 4 3,866 Oct-09-2018, 02:38 PM
Last Post: leoahum
  python script to get wildcard mask output in the following format techrichit 0 3,820 Aug-10-2018, 11:01 PM
Last Post: techrichit
  Mask superclass __init__ fig0 5 3,820 Jan-11-2018, 06:59 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020