[Python] Accessing and cropping image pixels using OpenCV (for beginners)

Introduction

I will explain how to crop an image using OpenCV. This is a continuation of Last time, so if you have any questions in this article, please have a look.

environment

MacOS Mojave Python 3.7

Image used

Use the image below (neko.jpg). neko.jpg

Check the size of the image

You can check the size of the image with the following code.

python


#Loading the library
import cv2
import matplotlib.pyplot as plt

img = cv2.imread("neko.jpg ")
print(img.shape)

When I run the above code, I get the following results:

(900, 1600, 3)

This means 900 pixels vertically, 1600 pixels horizontally, and 3 channels. In the case of a color image, the number of channels is 3 because it is GBR. In grayscale, the number of channels is 1 because it is only the degree of whiteness.

Image coordinates

In Opencv, the coordinate axes of the image are as follows. 座標.png

When the position is (x, y), the origin (0,0) is at the upper left as shown in the figure. When dealing with images, please note that the downward direction is the positive direction of the y-axis. The size of this image is 900 pixels vertically and 1600 pixels horizontally, so the x and y coordinates of the pixels at the end of the image are 899 and 1599, respectively. The reason why it does not become 900 or 1600 is that it counts from the origin 0.

Access to image coordinates

If you specify the coordinates of the image as follows, you can get the color value of that position.

python


img = cv2.imread("neko.jpg ")
print(img[450,800])

output


[153 161 190]

This indicates that the color at the coordinate (450,800) position is (R, G, B) = (153,161,190). Also, you can specify multiple coordinates by doing the following.

print(img[450:650,800:1000])
[[[153 161 190]
  [153 161 190]
  [152 160 189]
  ...
  [169 178 205]
  [169 178 205]
  [169 178 205]]

 [[152 160 189]
  [152 160 189]
  [153 161 190]
  ...
  [169 178 205]
  [169 178 205]
  [169 178 205]]

 [[151 159 188]
  [152 160 189]
  [154 162 191]
  ...
  [171 178 205]
  [172 179 206]
  [172 179 206]]

 ...
Omitted below. .. ..

Think of the ":" in [] as the same as the "~" we usually use to represent a range of numbers. In the above example, the color information in the range from coordinates (450,800) to (649,999) is output. 座標_位置指定.png

Cut at any position

If you put the specified range of the image in another object, you can get the image with the position cut out.

python


#Loading the library
import cv2
import matplotlib.pyplot as plt

img = cv2.imread("neko.jpg ")
#Img the specified pixel_Substitute for trim
img_trim = img[450:650,800:1000]
#Change color
img_trim = cv2.cvtColor(img_trim, cv2.COLOR_BGR2RGB)
#display
plt.imshow(img_trim)
plt.show()

切り取り画像.png

If the above image is output, it is successful! (Since it is a tatami mat, it is difficult to tell if it is cut out ... lol) Next time, I'd like to explain the work of kneading, such as scaling and flipping images: blush:

I'm on Twitter

If you would like to do various things such as information on future articles and questions and answers, please follow me ...! : relieved: https://twitter.com/ryuji33722052

Recommended Posts

[Python] Accessing and cropping image pixels using OpenCV (for beginners)
OpenCV for Python beginners
Rotate and scale the image before cropping [python] [OpenCV]
[Python] Using OpenCV with Python (Image Filtering)
[Python] Using OpenCV with Python (Image transformation)
[Image processing] Edge detection using Python and OpenCV makes Poo naked!
Notes for using OpenCV on Windows10 Python 3.8.3.
Implementation and explanation using XGBoost for beginners
Reading and creating a mark sheet using Python OpenCV (Tips for reading well)
Head orientation estimation using Python and OpenCV + dlib
Causal reasoning and causal search with Python (for beginners)
I tried object detection using Python and OpenCV
python textbook for beginners
Let's analyze Covid-19 (Corona) data using Python [For beginners]
Rock-paper-scissors poi in Python for beginners (answers and explanations)
Initial settings for using Python3.8 and pip on CentOS8
Searching for pixiv tags and saving illustrations using Python
Extendable skeletons for Vim using Python, Click and Jinja2
[Introduction for beginners] Reading and writing Python CSV files
Python # How to check type and type for super beginners
Learning flow for Python beginners
Image editing with python OpenCV
[Python] Using OpenCV with Python (Basic)
Try a similar search for Image Search using the Python SDK [Search]
OpenCV3 installation for Python3 @macOS
Python application: Data cleansing # 3: Use of OpenCV and preprocessing of image data
How to learn TensorFlow for liberal arts and Python beginners
CNN (1) for image classification (for beginners)
Python3 environment construction (for beginners)
Python #function 2 for super beginners
Basic Python grammar for beginners
100 Pandas knocks for Python beginners
Python for super beginners Python #functions 1
[Python] Introduction to graph creation using coronavirus data [For beginners]
Python #list for super beginners
~ Tips for beginners to Python ③ ~
Shoot time-lapse from a PC camera using Python and OpenCV
Using OpenCV with Python @Mac
This and that for using Step Functions with CDK + Python
Automatic image interpolation with OpenCV and Python (Fast Marching Method, Navier-Stokes)
[Python machine learning] Recommendation of using Spyder for beginners (as of August 2020)
Pydroid 3 --I tried OpenCV and TensorFlow options for IDE for Python 3 (Android)
Create a striped illusion with gamma correction for Python3 and openCV3
Create a simple scheduled batch using Docker's Python Image and parse-crontab
I just erased the object using image repair (inpaint) (OpenCV: Python)
Build and try an OpenCV & Python environment in minutes using Docker
Aligning scanned images of animated video paper using OpenCV and Python
[Explanation for beginners] OpenCV face detection mechanism and practice (detect MultiScale)
Dump, restore and query search for Python class instances using mongodb
Environment construction of python and opencv
Image capture of firefox using python
Neural network with OpenCV 3 and Python 3
Judgment of backlit image using OpenCV
Python Exercise for Beginners # 2 [for Statement / While Statement]
Authentication using tweepy-User authentication and application authentication (Python)
Python for super beginners Python # dictionary type 1 for super beginners
[Python] Using OpenCV with Python (Edge Detection)
Personal notes for python image processing
Python #index for super beginners, slices
Recommended container image for Python applications
<For beginners> python library <For machine learning>