[英]Convert Image to CMYK and split the channels in OpenCV
I am trying to find a way how to convert an image into CMYK in OpenCV.我试图找到一种方法,如何在 OpenCV 中将图像转换为 CMYK。 By default the images are BGR I think, so converting to a Gray image would be img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
but there is no cv2.COLOR_BGR2CMYK
.我认为默认图像是 BGR,因此转换为灰色图像将是img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
但没有cv2.COLOR_BGR2CMYK
。 I found out with PIL
you can do it, but no way in OpenCV?我发现用PIL
你可以做到,但在 OpenCV 中没有办法?
I need to split then basically like (C, M, Y, K) = cv2.split(image)
.我需要拆分然后基本上像(C, M, Y, K) = cv2.split(image)
。 Is there a way like that?有这样的方法吗?
In Pillow I can do like that在枕头里我可以这样做
im = Image.fromarray(np.array(image))
im = im.convert('CMYK')
c_im, m_im, y_im, k_im = im.split()
You can either import PIL
and convert the image from BGR to CMYK, or do it in openCV in a more "analytical" way.您可以导入PIL
并将图像从 BGR 转换为 CMYK,或者以更“分析”的方式在 openCV 中进行。
From the known relations between BGR and CMYK, you can write a code like this:从已知的 BGR 和 CMYK 的关系,可以写出这样的代码:
import cv2
import numpy as np
bgr = cv2.imread('your_image.jpg') #your bgr image
bgrdash = bgr.astype(np.float)/255.
K = 1 - np.max(bgrdash, axis=2)
C = (1-bgrdash[...,2] - K)/(1-K)
M = (1-bgrdash[...,1] - K)/(1-K)
Y = (1-bgrdash[...,0] - K)/(1-K)
So that you can have your CMYK splitted channels.这样您就可以拥有 CMYK 分割通道。 To convert your BGR to CMYK:要将 BGR 转换为 CMYK:
CMYK = (np.dstack((C,M,Y,K)) * 255).astype(np.uint8)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.