added color conversion

This commit is contained in:
sccolbert
2009-10-24 15:08:26 +02:00
parent c2b98b259a
commit 25e9ec82df
6 changed files with 5903 additions and 2231 deletions
File diff suppressed because it is too large Load Diff
+4 -2
View File
@@ -17,6 +17,7 @@ np.import_array()
# dtype itself....
UINT8 = np.dtype('uint8')
INT8 = np.dtype('int8')
UINT16 = np.dtype('uint16')
INT16 = np.dtype('int16')
INT32 = np.dtype('int32')
FLOAT32 = np.dtype('float32')
@@ -25,6 +26,7 @@ FLOAT64 = np.dtype('float64')
cdef int IPL_DEPTH_SIGN = 0x80000000
cdef int IPL_DEPTH_8U = 8
cdef int IPL_DEPTH_8S = (IPL_DEPTH_SIGN | 8)
cdef int IPL_DEPTH_16U = 16
cdef int IPL_DEPTH_16S = (IPL_DEPTH_SIGN | 16)
cdef int IPL_DEPTH_32S = (IPL_DEPTH_SIGN | 32)
cdef int IPL_DEPTH_32F = 32
@@ -33,8 +35,8 @@ cdef int IPL_DEPTH_64F = 64
# I'd like a better to associate the IPL data type flag to the proper numpy
# types without using a dictionary.
_ipltypes = {UINT8: IPL_DEPTH_8U, INT8: IPL_DEPTH_8S, INT16: IPL_DEPTH_16S,
INT32: IPL_DEPTH_32S, FLOAT32: IPL_DEPTH_32F,
_ipltypes = {UINT8: IPL_DEPTH_8U, INT8: IPL_DEPTH_8S, UINT16: IPL_DEPTH_16U,
INT16: IPL_DEPTH_16S, INT32: IPL_DEPTH_32S, FLOAT32: IPL_DEPTH_32F,
FLOAT64: IPL_DEPTH_64F}
-2
View File
@@ -128,8 +128,6 @@ CV_Luv2RGB = 59
CV_HLS2BGR = 60
CV_HLS2RGB = 61
CV_COLORCVT_MAX = 100
#########################
# Calibration Constants #
#########################
File diff suppressed because it is too large Load Diff
+134
View File
@@ -17,6 +17,91 @@ from _libimport import cv
# setup numpy tables for this module
np.import_array()
#-------------------------------------------------------------------------------
# Useful global stuff
#-------------------------------------------------------------------------------
# a dict for cvCvtColor to get the appropriate types and shapes without
# if statements all over the place (this way is faster, cause the dict is
# created at import time)
# the order of list arguments is:
# [in_channels, out_channels, [input_dtypes]]
# out type is always the same as in type
_cvtcolor_dict = {CV_BGR2BGRA: [3, 4, [UINT8, UINT16, FLOAT32]],
CV_RGB2RGBA: [3, 4, [UINT8, UINT16, FLOAT32]],
CV_BGRA2BGR: [4, 3, [UINT8, UINT16, FLOAT32]],
CV_RGBA2RGB: [4, 3, [UINT8, UINT16, FLOAT32]],
CV_BGR2RGBA: [3, 4, [UINT8, UINT16, FLOAT32]],
CV_RGB2BGRA: [3, 4, [UINT8, UINT16, FLOAT32]],
CV_RGBA2BGR: [4, 3, [UINT8, UINT16, FLOAT32]],
CV_BGRA2RGB: [4, 3, [UINT8, UINT16, FLOAT32]],
CV_BGR2RGB: [3, 3, [UINT8, UINT16, FLOAT32]],
CV_RGB2BGR: [3, 3, [UINT8, UINT16, FLOAT32]],
CV_BGRA2RGBA: [4, 4, [UINT8, UINT16, FLOAT32]],
CV_RGBA2BGRA: [4, 4, [UINT8, UINT16, FLOAT32]],
CV_BGR2GRAY: [3, 1, [UINT8, UINT16, FLOAT32]],
CV_RGB2GRAY: [3, 1, [UINT8, UINT16, FLOAT32]],
CV_GRAY2BGR: [1, 3, [UINT8, UINT16, FLOAT32]],
CV_GRAY2RGB: [1, 3, [UINT8, UINT16, FLOAT32]],
CV_GRAY2BGRA: [1, 4, [UINT8, UINT16, FLOAT32]],
CV_GRAY2RGBA: [1, 4, [UINT8, UINT16, FLOAT32]],
CV_BGRA2GRAY: [4, 1, [UINT8, UINT16, FLOAT32]],
CV_RGBA2GRAY: [4, 1, [UINT8, UINT16, FLOAT32]],
CV_BGR2BGR565: [3, 2, [UINT8]],
CV_RGB2BGR565: [3, 2, [UINT8]],
CV_BGR5652BGR: [2, 3, [UINT8]],
CV_BGR5652RGB: [2, 3, [UINT8]],
CV_BGRA2BGR565: [4, 2, [UINT8]],
CV_RGBA2BGR565: [4, 2, [UINT8]],
CV_BGR5652BGRA: [2, 4, [UINT8]],
CV_BGR5652RGBA: [2, 4, [UINT8]],
CV_GRAY2BGR565: [1, 2, [UINT8]],
CV_BGR5652GRAY: [2, 1, [UINT8]],
CV_BGR2BGR555: [3, 2, [UINT8]],
CV_RGB2BGR555: [3, 2, [UINT8]],
CV_BGR5552BGR: [2, 3, [UINT8]],
CV_BGR5552RGB: [2, 3, [UINT8]],
CV_BGRA2BGR555: [4, 2, [UINT8]],
CV_RGBA2BGR555: [4, 2, [UINT8]],
CV_BGR5552BGRA: [2, 4, [UINT8]],
CV_BGR5552RGBA: [2, 4, [UINT8]],
CV_GRAY2BGR555: [1, 2, [UINT8]],
CV_BGR5552GRAY: [2, 1, [UINT8]],
CV_BGR2XYZ: [3, 3, [UINT8, UINT16, FLOAT32]],
CV_RGB2XYZ: [3, 3, [UINT8, UINT16, FLOAT32]],
CV_XYZ2BGR: [3, 3, [UINT8, UINT16, FLOAT32]],
CV_XYZ2RGB: [3, 3, [UINT8, UINT16, FLOAT32]],
CV_BGR2YCrCb: [3, 3, [UINT8, UINT16, FLOAT32]],
CV_RGB2YCrCb: [3, 3, [UINT8, UINT16, FLOAT32]],
CV_YCrCb2BGR: [3, 3, [UINT8, UINT16, FLOAT32]],
CV_YCrCb2RGB: [3, 3, [UINT8, UINT16, FLOAT32]],
CV_BGR2HSV: [3, 3, [UINT8, FLOAT32]],
CV_RGB2HSV: [3, 3, [UINT8, FLOAT32]],
CV_BGR2Lab: [3, 3, [UINT8, FLOAT32]],
CV_RGB2Lab: [3, 3, [UINT8, FLOAT32]],
CV_BayerBG2BGR: [1, 3, [UINT8]],
CV_BayerGB2BGR: [1, 3, [UINT8]],
CV_BayerRG2BGR: [1, 3, [UINT8]],
CV_BayerGR2BGR: [1, 3, [UINT8]],
CV_BayerBG2RGB: [1, 3, [UINT8]],
CV_BayerGB2RGB: [1, 3, [UINT8]],
CV_BayerRG2RGB: [1, 3, [UINT8]],
CV_BayerGR2RGB: [1, 3, [UINT8]],
CV_BGR2Luv: [3, 3, [UINT8, FLOAT32]],
CV_RGB2Luv: [3, 3, [UINT8, FLOAT32]],
CV_BGR2HLS: [3, 3, [UINT8, FLOAT32]],
CV_RGB2HLS: [3, 3, [UINT8, FLOAT32]],
CV_HSV2BGR: [3, 3, [UINT8, FLOAT32]],
CV_HSV2RGB: [3, 3, [UINT8, FLOAT32]],
CV_Lab2BGR: [3, 3, [UINT8, FLOAT32]],
CV_Lab2RGB: [3, 3, [UINT8, FLOAT32]],
CV_Luv2BGR: [3, 3, [UINT8, FLOAT32]],
CV_Luv2RGB: [3, 3, [UINT8, FLOAT32]],
CV_HLS2BGR: [3, 3, [UINT8, FLOAT32]],
CV_HLS2RGB: [3, 3, [UINT8, FLOAT32]]}
###################################
# opencv function declarations
###################################
@@ -143,6 +228,11 @@ ctypedef void (*cvIntegralPtr)(IplImage*, IplImage*, IplImage*, IplImage*)
cdef cvIntegralPtr c_cvIntegral
c_cvIntegral = (<cvIntegralPtr*><size_t>ctypes.addressof(cv.cvIntegral))[0]
# cvCvtColor
ctypedef void (*cvCvtColorPtr)(IplImage*, IplImage*, int)
cdef cvCvtColorPtr c_cvCvtColor
c_cvCvtColor = (<cvCvtColorPtr*><size_t>ctypes.addressof(cv.cvCvtColor))[0]
# cvCalibrateCamera2
ctypedef void (*cvCalibrateCamera2Ptr)(CvMat*, CvMat*, CvMat*,
CvSize, CvMat*, CvMat*, CvMat*, CvMat*, int)
@@ -1029,6 +1119,50 @@ def cvIntegral(np.ndarray src, square_sum=False, tilted_sum=False):
return out
def cvCvtColor(np.ndarray src, int code):
validate_array(src)
assert_dtype(src, [UINT8, UINT16, FLOAT32])
try:
conversion_params = _cvtcolor_dict[code]
except KeyError:
print 'unknown conversion code'
raise
cdef int src_channels = <int>conversion_params[0]
cdef int out_channels = <int>conversion_params[1]
src_dtypes = conversion_params[2]
assert_nchannels(src, src_channels)
assert_dtype(src, src_dtypes)
cdef np.ndarray out
# the out array can be 2, 3, or 4 channels so we need shapes that
# can handle either
cdef np.npy_intp out_shape2[2]
cdef np.npy_intp out_shape3[3]
out_shape2[0] = src.shape[0]
out_shape2[1] = src.shape[1]
out_shape3[0] = src.shape[0]
out_shape3[1] = src.shape[1]
if out_channels == 1:
out = new_array(2, out_shape2, src.dtype)
else:
out_shape3[2] = <np.npy_intp>out_channels
out = new_array(3, out_shape3, src.dtype)
cdef IplImage srcimg
cdef IplImage outimg
populate_iplimage(src, &srcimg)
populate_iplimage(out, &outimg)
c_cvCvtColor(&srcimg, &outimg, code)
return out
def cvCalibrateCamera2(np.ndarray object_points, np.ndarray image_points,
np.ndarray point_counts, image_size):
@@ -186,6 +186,20 @@ class TestIntegral(OpenCVTest):
cvIntegral(self.lena_RGB_U8, True, True)
class TestCvtColor(OpenCVTest):
@opencv_skip
def test_cvCvtColor(self):
cvCvtColor(self.lena_RGB_U8, CV_RGB2BGR)
cvCvtColor(self.lena_RGB_U8, CV_RGB2BGRA)
cvCvtColor(self.lena_RGB_U8, CV_RGB2HSV)
cvCvtColor(self.lena_RGB_U8, CV_RGB2BGR565)
cvCvtColor(self.lena_RGB_U8, CV_RGB2BGR555)
cvCvtColor(self.lena_RGB_U8, CV_RGB2GRAY)
cvCvtColor(self.lena_GRAY_U8, CV_GRAY2BGR)
cvCvtColor(self.lena_GRAY_U8, CV_GRAY2BGR565)
cvCvtColor(self.lena_GRAY_U8, CV_GRAY2BGR555)
class TestFindChessboardCorners(object):
@opencv_skip
def test_cvFindChessboardCorners(self):