简体   繁体   中英

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. 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 . I found out with PIL you can do it, but no way in OpenCV?

I need to split then basically like (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.

From the known relations between BGR and CMYK, you can write a code like this:

 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. To convert your BGR to CMYK:

 CMYK = (np.dstack((C,M,Y,K)) * 255).astype(np.uint8)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM