Memo of pixel position operation for image data in Python (numpy, cv2)

background

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]].

I want to process only a specific channel

For example, you want to set 1 only for G channel

img[:, :, 1] = 1

will do.

I want to broadcast alpha to RGB

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.

Judging and substituting pixel elements, etc.

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.

Divide the image into subregions (tiles)

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)

Other

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

Memo of pixel position operation for image data in Python (numpy, cv2)
Check the operation of Python for .NET in each environment
Pixel manipulation of images in Python
[Python] Operation memo of pandas DataFrame
Summary of various for statements in Python
Python data structure and operation (Python learning memo ③)
Image sending / receiving memo in Python (Flask)
CG image quality evaluation memo in Python
How to send a visualization image of data created in Python to Typetalk
Cloud Pak for Data object operation example in Python (WML client, project_lib)
[Python] Reason for dtype "int32" in Numpy (Windows environment) (as of September 2020)
Get the key for the second layer migration of JSON data in python
Data analysis in Python Summary of sources to look at first for beginners
Unattended operation of Google Spreadsheets (etc.) in Python
[Python] Swapping rows and columns in Numpy data
Real-time visualization of thermography AMG8833 data in Python
Display candlesticks for FX (forex) data in Python
Image processing? The story of starting Python for
The story of reading HSPICE data in Python
Code for checking the operation of Python Matplotlib
A well-prepared record of data analysis in Python
[Python] Numpy memo
Basic story of inheritance in Python (for beginners)
Use data class for data storage of Python 3.7 or higher
Tool to make mask image for ETC in Python
Summary of tools needed to analyze data in Python
Full-width and half-width processing of CSV data in Python
Create your own Big Data in Python for validation
Power BI visualization of Salesforce data entirely in Python
List of Python libraries for data scientists and data engineers
Not being aware of the contents of the data in python
List of Python code used in big data analysis
Let's use the open data of "Mamebus" in Python
Display a histogram of image brightness values in python
Ssh connection memo using ProxyCommand of ssh_config in Python
Image format in Python
[Python] Sorting Numpy data
Python decorator operation memo
How to change python version of Notebook in Watson Studio (or Cloud Pak for Data)
Basic summary of data manipulation in Python Pandas-Second half: Data aggregation
[Memo] The mystery of cumulative assignment statements in Python functions
Sample image of Python API server for EC2 (public AMI)
[For beginners] Summary of standard input in Python (with explanation)
Python application: Data cleansing # 3: Use of OpenCV and preprocessing of image data
[Understand in the shortest time] Python basics for data analysis
How to develop in a virtual environment of Python [Memo]
[Homology] Count the number of holes in data with Python
[Scientific / technical calculation by Python] Basic operation of arrays, numpy
Summary of Hash (Dictionary) operation support for Ruby and Python
A memo of writing a basic function in Python using recursion
Comparison of data frame handling in Python (pandas), R, Pig
I wrote the basic operation of Numpy in Jupyter Lab.
Google search for the last line of the file in Python
Handle Ambient data in Python
Python memo (for myself): Array
Python for Data Analysis Chapter 4
Display UTM-30LX data in Python
Search for strings in Python
Python code memo for yourself
Equivalence of objects in Python
Techniques for sorting in Python