简体   繁体   中英

copy a matrix of array into another one with a mask using putmask with numpy

I'd like to copy each element whch are a matrix of array of 3 elements to another but I want to mask to filter this array [0,0,0] and not all the single elements of the array... (see description with the pictures: this is actually a picture with RGB values for each dots)

for instance call:

np.putmask(dst_slice, src_slice != 0, src_slice)

this matrix is actually an array of RGB value such as [0,0,0],[1,2,3].... ]

but in my case, I don't want to filter with the "0" but with "[0,0,0]" and keep good performances.

thanks for any help!

I will clarify a bit and I apologize if it was not clear. I've added a picture to illustrate what I want to do: those matrix are actually RGB pictures where the black dot with array [0,0,0] are read as transparent in the layer and not copied then. If I have this valie [0,0,1] then the array should be copied because it has "1" in his value.

I've tried those 2 but none of this works because the 0 is read as an element of the array of array and the array itself is not the leaf to be compare for the mask

np.copyto(dst_slice, src_slice, where=src_slice != 0)

np.putmask(dst_slice, src_slice != 0, src_slice)

-> ideally, I'd like to use the right filter/mask to be able to filet [0,0,0] and not 0 with embdedded functions such as "copyto" or "putmask" to copy matrix element to another knowing my element are arrays and not integers.

regarding the performance, in my program it's really tricky because it's a flow of pistures to be read so there is no way I could use list or parsing anything without array or numpy direct "copy" matrix method

here is a sample to illustrate: for instance if I had [0,0,12] it should be copied and keep the "0" in the matrix below, instead, it gives [22,7,12]

Only [0,0,0] and only this should not be considered for the copy since it's seen as transparent

EDIT:I'VE FOUND THE SOLUTION thanks for the help, I've called:

np.copyto(dst_slice, src_slice, where=np.any(src_slice,axis=2,keepdims=True))

description of the sample

If I understand you correctly, the goal is to copy non-zero 3D points from the source array of a shape (n, 3) to the destination. While there is no description of what good performance is, I think this code will do the job quite well:

key = src_slice.any(1)
dst_slice[key] = src_slice[key]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM