[..., :: -1]
: cry:BGR -> RGB
It seems that cv2 processes it as a BGR image by default. It is necessary to sort it to RGB when saving or displaying it with matplotlib.
The array is (H, W, C).
[:,:, :: -1]
swaps the elements of the C dimension (channel).
https://stackoverflow.com/questions/4661557/pil-rotate-image-colors-bgr-rgb
:
is slice ((start, end, step)
) and points to all elements of that dimension. (None, None, 1)
(?) Or (0, n-1, 1)
equivalent.
:: -1
is also slice, and(0, n-1, -1)
goes one by one from end to start.
In the case of RGB (3 elements), it is the indexing of [2, 1, 0]
.
So you can sort with BGR-> RGB.
...
ʻImg [..., :: -1] `case. From a first-timer, it's too mysterious to understand at all.
...
has low searchability and is difficult.
https://www.scivision.dev/numpy-image-bgr-to-rgb/
It was an abbreviation for ...
= :,:
. Is it like selecting all elements in each dimension?
It's like [..., :: -1]
= [:,:, :: -1]
= [:,:, [2, 1, 0]]
.
For example, you want to set 1 only for G channel
img[:, :, 1] = 1
will do.
Maybe just numpy.
When you have images of rgb (3 channels) and alpha (1 channel), you cannot simply multiply by rgb * alpha
.
You need to broadcast (duplicate) the value of alpha to RGB.
If you specify None
asfg * alpha [:,:, None]
, it will be broadcast.: Thinking:
https://stackoverflow.com/questions/51091560/numpy-indexing-using-none-for-pairwise-operations
The behavior is to increase the dimension by one as None = np.newaxis (difficult to understand ...)
It seems that ast (syntax tree) is properly considered,
Something like bg * (1 --alpha [:, :, None])
and then RGB and multiplication work fine too.
for i in range(len(pixels)):
pixels[i] = pixels[i] < 0.5 ? 0 : pixels[i]
I want to do something like this.
pixels[pixels < 0.5] = 0
Then, it will process with all pixels.
If you have the shape (image size and number of channels), you can use other ndarrays.
e.g.
alpha[trimap_np[:, :, 0] == 1] = 0
In the alpha image, set the value of the pixel position where the R channel of trimap_np is 1 to 0.
fg[alpha == 1] = image_np[alpha == 1]
T.B.W.
In slice notation, you can specify every 4 pixels with step, but you can not specify every 4 pixels, so you can specify start, end with slide (start, end, 1)
in for statement etc., or numpy , Use a function that divides into subregions around cv2.
(By the way, numpy.tile is a function that repeats and arranges images in a tile shape, so it cannot be used for division)
Articles that explain visually
Numpy’s indexing and slicing notation explained visually https://medium.com/@buch.willi/numpys-indexing-and-slicing-notation-explained-visually-67dc981c22c1
Recommended Posts