[Python] Using OpenCV with Python (Basic)

Install (Anaconda)

To install the latest OpenCV 3.1 on Anaconda as of February 2016, get it from the following site.

conda install --channel https://conda.anaconda.org/menpo opencv3

Import and check the version.

In [1]: import cv2

In [2]: print cv2.__version__
3.1.0

Load image

Read the image with cv2.imread. At this time, the image is BGR. Please note that if you try to display with matplotlib as it is, the color will be strange.

When you read an image with sckit kimage or scipy, it becomes RGB.

The type of the loaded image is numpy array.

In [5]: I = cv2.imread('Lenna.bmp')

In [6]: I = cv2.imread('lena512color.tiff')

In [7]: type(I)
Out[7]: numpy.ndarray

For PIL

In [8]: from PIL import Image

In [9]: I = Image.open('Lenna.bmp')

In [10]: type(I)
Out[10]: PIL.BmpImagePlugin.BmpImageFile

For Scikit-image

In [11]: from skimage import io

In [12]: I = io.imread('Lenna.bmp')

In [13]: type(I)
Out[13]: numpy.ndarray

In [14]: Icv = cv2.imread('Lenna.bmp')

In [15]: print Icv[0,0,:]
[125 137 226]

In [16]: print I[0,0,:]
[226 137 125]

For Scypi

In [17]: from scipy import misc

In [18]: I = misc.imread('Lenna.bmp')

In [19]: type(I)
Out[19]: numpy.ndarray

In [20]: print I[0,0,:]
[226 137 125]

For matplotlib

In [21]: from matplotlib import image

In [22]: I = image.imread('Lenna.bmp')

In [23]: type(I)
Out[23]: numpy.ndarray

In [24]: print I[0,0,:]
[226 137 125]

Save the image

If you want to save the image with opencv, use imwrite.

imwrite(filename, img[, params])

In [26]: cv2.imwrite('output.png', I)
Out[26]: True

However, when saving with OpenCV, the colors must be arranged in the order of BGR in Interleaved format. If you save it as it is, it will be as follows.

output.png

Display image

Use imshow to display the image.

imshow(winname, mat)

import cv2

I = cv2.imread('./data/SIDBA/Lenna.bmp')

cv2.namedWindow('window')
cv2.imshow('window', I)
cv2.waitKey(0)
cv2.destroyAllWindows()

However, if you do this with interactive ipython, ipython will die.

When doing with ipython, it is good to execute startWindowThread first as shown below.

In [7]: I = cv2.imread('./data/SIDBA/Lenna.bmp')

In [8]: cv2.startWindowThread()
Out[8]: 1

In [9]: cv2.namedWindow('window')

In [10]: cv2.imshow('window', I)

In [11]: cv2.waitKey(0)
Out[11]: 10

In [12]: cv2.destroyAllWindows()

However, if you want to display an image, it is easier to use matplotlib unless you have a specific reason.

In [12]: import matplotlib.pyplot as plt
    ...: import cv2
    ...: 
    ...: I = cv2.imread('./data/SIDBA/Lenna.bmp')
    ...: 
    ...: plt.imshow(cv2.cvtColor(I, cv2.COLOR_BGR2RGB))
    ...: plt.show()

範囲を選択_036.png

Color conversion

If you read the image with opencv, the image is BGR, so you need to convert it to RGB to display it with matplotlib. In such a case, use cvtColor.

import numpy as np
import cv2
import matplotlib.pyplot as plt

I = cv2.imread('./data/SIDBA/Lenna.bmp')

J = cv2.cvtColor(I, cv2.COLOR_BGR2RGB)

plt.imshow(J)
plt.show()

Recommended Posts

[Python] Using OpenCV with Python (Basic)
Using OpenCV with Python @Mac
[Python] Using OpenCV with Python (Image Filtering)
[Python] Using OpenCV with Python (Image transformation)
Basic study of OpenCV with Python
Binarization with OpenCV / Python
OpenCV basic code (python)
"Apple processing" with OpenCV3 + Python3
[S3] CRUD with S3 using Python [Python]
BASIC authentication with Python bottle
Using Quaternion with Python ~ numpy-quaternion ~
Image editing with python OpenCV
Camera capture with Python + OpenCV
Face detection with Python + OpenCV
[Python] [SQLite3] Operate SQLite with Python (Basic)
Send using Python with Gmail
Try projective transformation of images using OpenCV with Python
Complement python with emacs using company-jedi
Shining life with Python and OpenCV
Harmonic mean with Python Harmonic mean (using SciPy)
Neural network with OpenCV 3 and Python 3
Using Rstan from Python with PypeR
Scraping with Selenium in Python (Basic)
Easy Python + OpenCV programming with Canopy
Try face recognition with python + OpenCV
Cut out face with Python + OpenCV
1. Statistics learned with Python 1-1. Basic statistics (Pandas)
Face recognition with camera with opencv3 + python2.7
Load gif images with Python + OpenCV
Notes on using rstrip with python.
Find image similarity with Python + OpenCV
Use OpenCV with Python 3 in Window
Draw an illustration with Python + OpenCV
Track baseball balls with Python + OpenCV
Graph Based Segmentation with Python + OpenCV
When using MeCab with virtualenv python
Precautions when using six with Python 2.5
Draw arrows (vectors) with opencv / python
Notes for using OpenCV on Windows10 Python 3.8.3.
[AWS] Using ini files with Lambda [Python]
Face detection with Python + OpenCV (rotation invariant)
FizzBuzz with Python3
Scraping with Python
Getting Started with python3 # 1 Learn Basic Knowledge
Statistics with python
Python2.7 + CentOS7 + OpenCV3
Behind the flyer: Using Docker with Python
Scraping with Python
Python with Go
Try using the camera with Python's OpenCV
Socket communication using socketserver with python now
Capturing images with Pupil, python and OpenCV
Twilio with Python
Integrate with Python
Start using Python
Try using Python with Google Cloud Functions
Play with 2016-Python
I tried non-photorealistic rendering with Python + opencv
AES256 with python
Check stock prices with slackbot using python
Image processing with Python & OpenCV [Tone Curve]