OpenCV Python cv2.mixChannels() [英] OpenCV Python cv2.mixChannels()

查看:440
本文介绍了OpenCV Python cv2.mixChannels()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将其从C ++转换为Python,但是它给出了不同的色调结果.

I was trying to convert this from C++ to Python, but it is giving different hue results.

在C ++中:

/// Transform it to HSV
cvtColor( src, hsv, CV_BGR2HSV );

/// Use only the Hue value
hue.create( hsv.size(), hsv.depth() );
int ch[] = { 0, 0 };
mixChannels( &hsv, 1, &hue, 1, ch, 1 );

我在Python中尝试过:

I tried this in Python:

# Transform it to HSV
hsv = cv2.cvtColor(src, cv2.COLOR_BGR2HSV)

# Use only the Hue value
hue = np.zeros(hsv.shape, dtype=np.uint8)
ch = [0] * 2
cv2.mixChannels(hsv, hue, ch)

推荐答案

当您查看

When you look in the documentation, you can see the C++ functions taking as arguments arrays (or vectors) of Mat as input and output.

C++: void mixChannels(const Mat* src, size_t nsrcs, Mat* dst, size_t ndsts, const int* fromTo, size_t npairs)

C++: void mixChannels(const vector<Mat>& src, vector<Mat>& dst, const int* fromTo, size_t npairs)

类似地,在Python中,您需要提供源和目标的np.array列表.

Similarly, in Python you need to provide lists of np.array for both source and destination.

import cv2
import numpy as np
img = cv2.imread('cage.png')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# Baseline for comparison
h,_,_ = cv2.split(hsv)

hue = np.zeros(hsv.shape, dtype=np.uint8)
cv2.mixChannels([hsv], [hue], [0,0])

print np.array_equal(h, hue[:,:,0])

控制台输出

>python mix.py
True

这篇关于OpenCV Python cv2.mixChannels()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆