OpenCVの関数では、入力のデータの型に制約がついているのがある。
pythonでcv2.関数()を実行しているとき、入力データの型や出力のデータ深さのddepthの指定を間違えてエラーを生じることがある。エラーメッセージの中で、cv2.CV_8Uなどの値は、整数で表示されてしまっているので、入力データの型はどうしておけばよかったのか、出力のデータ深さのddepthの指定はどうすればよかったのかと困惑することがある。
同様に、C++でcv::関数()のソースコードからビルドにするときにも失敗することがある。
その同じ間違いを繰り返さないためにメモを作ろうと思う。
dilate(...)
dilate(src, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]]) -> dst
function | CV_8U | CV_8S | CV_16U | CV_16S | CV_32S | CV_32F | CV_64F |
---|---|---|---|---|---|---|---|
値 | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
np.dtype | uint8 | int8 | uint16 | int16 | int32 | float32 | float64 |
cv2.dilate() | o | x | o | o | x | o | o |
cv2.erode() | o | x | o | o | x | o | o |
connectedComponentsWithStats() input | o | x | x | x | x | x | x |
connectedComponentsWithStats() labels | x | x | o | x | o | x | x |
connectedComponentsWithStats() stats | x | x | x | x | o | x | x |
connectedComponentsWithStats() centroids | x | x | x | x | x | x | o |
function | CV_8U | CV_8S | CV_16U | CV_16S | CV_32S | CV_32F | CV_64F |
---|---|---|---|---|---|---|---|
np.dtype | uint8 | int8 | uint16 | int16 | int32 | float32 | float64 |
cv2.Sobel() src | o | x | o | o | x | o | o |
cv2.Sobel() dst | o | x | o | o | x | o | o |
cv2.blur() src | o | x | o | o | o | o | o |
GaussianBlur() src | o | x | o | o | x | o | o |
filter2D() src | o | x | o | o | x | o | o |
cartToPolar() src | x | x | x | x | x | o | o |
polarToCart() src | x | x | x | x | x | o | o |
magnitude() src | x | x | x | x | x | o | o |
phase() src | x | x | x | x | x | o | o |
cv2.sqrt() src | x | x | x | x | x | o | o |
cvtColor() | o | x | o | x | x | o | x |
組み合わせについてはまだ十分にはチェックしていない。
Sobel(...)
Sobel(src, ddepth, dx, dy[, dst[, ksize[, scale[, delta[, borderType]]]]]) -> dst
Sobelの組み合わせ
入力画像srcの型:縦軸
出力画像の型を決めるddepth: 横軸
CV_8U | CV_8S | CV_16U | CV_16S | CV_32S | CV_32F | CV_64F | |
---|---|---|---|---|---|---|---|
np.uint8 | o | x | o | o | x | o | o |
np.int8 | x | x | x | x | x | x | x |
np.uint16 | o | x | o | o | x | o | o |
np.int16 | o | x | o | o | x | o | o |
np.int32 | x | x | x | x | x | x | x |
np.float32 | o | x | o | o | x | o | o |
np.float64 | o | x | o | o | x | x | o |
cv2.filter2D(np.array(orgImg, dtype=npt), ddepth, kernel)の組み合わせ
入力画像srcの型:縦軸
出力画像の型を決めるddepth: 横軸
CV_8U | CV_8S | CV_16U | CV_16S | CV_32S | CV_32F | CV_64F | |
---|---|---|---|---|---|---|---|
np.uint8 | o | x | o | o | x | o | o |
np.int8 | x | x | x | x | x | x | x |
np.uint16 | x | x | o | x | x | o | o |
np.int16 | x | x | x | o | x | o | o |
np.int32 | x | x | x | x | x | x | x |
np.float32 | x | x | x | x | x | o | x |
np.float64 | x | x | x | x | x | x | o |
function | CV_8U | CV_8S | CV_16U | CV_16S | CV_32S | CV_32F | CV_64F |
---|---|---|---|---|---|---|---|
np.dtype | uint8 | int8 | uint16 | int16 | int32 | float32 | float64 |
cv2.threshold() src | o | o | x | x | x | o | x |
cv2.floodFill() src | o | o | x | x | x | o | x |
function | CV_8U | CV_8S | CV_16U | CV_16S | CV_32S | CV_32F | CV_64F |
---|---|---|---|---|---|---|---|
np.dtype | uint8 | int8 | uint16 | int16 | int32 | float32 | float64 |
cv2.Canny() src | o | o | x | x | x | x | x |
cv2.HoughLines() src 2値画像 | o | ? | x | x | x | x | x |
cv2.HoughCircles() src 2値画像 | o | ? | x | x | x | x | x |
# -*- coding: utf-8 -*-
u""" OpenCVのcv::MatのtypeをPythonでチェックする
"""
import cv2
print cv2.CV_8U
print cv2.CV_8S
print cv2.CV_16U
print cv2.CV_16S
print cv2.CV_32S
print cv2.CV_32F
print cv2.CV_64F