How can i calculate cmyk values from rgb in matlab?

I have an image with .jpg format , and i got the values of (R,G,B,C,M,Y,K) for all pixels . but I want to know how to calculate cmyk values as calculated, for example :
the values of first pixel(0,0) were as follows :
R=22
G=32
B=42
C=167
M=130
Y=95
K=199
So ,what is the relationship between 22, and 167 or 32 and 130 ?rgb.png

2 Comments

Slightly off-topic: you really shouldn't put your own files in the installation directory of matlab. Use another folder on your computer.
I would also strongly recommend not using Windows 7 anymore, unless you are working on a computer from an organization that has an agreement with Microsoft that ensures continuing updates.
@RIk: Well spotted!
@ahmad Al sarairah: as Rik wrote, do NOT save your personal files in the installation directory of any application. With MATLAB the simplest is to use the default Startup directory (which is under your <user> directory).

Sign in to comment.

 Accepted Answer

People who use simple formulae like the one in the other answer are people who do not understand CMYK.
Typically, if you want to convert from RGB to CMYK it's because you want to send some image to a print shop. Once printed you want the image to look exactly (in terms of colour) as your original image. Unfortunately, the look is going to depend on the actual printer. One printer will generate slightly different colour than another for the input. Therefore, people who manage these things have created what is called ICC profiles, which is basically a file that tells you exactly what the colours are going to look like. If you don't take these into account then the red in your original RGB image may look pink with one printer and orange on another.
In order to convert RGB to CMYK you need the ICC profile of the RGB device that has been used to generate the image (your monitor) and the ICC profile of the printer (which the print shop will give you). Once you've got these, it is trivial to transform RGB to CMYK in matlab with the applycform function:
inprof = iccread(yourmonitoriccprofile);
outprof = iccread(theprinterprofile);
rgb_to_cmyk = makecform('icc', inprof, outprof);
cmyk_image = applycform(rgb_image, rgb_to_cmyk);
See matlab's documentation on profile-based colour conversion.
Note that converting RGB to CMYK without taking profile into account (eg with naive formulae) is a complete waste of time. You may as well give the RGB file directly to the print shop which will do a better job of printing it correctly.

20 Comments

Thank you mr.kalyan
I have used the mathematical equations to calculate the value of cyan of the first pixel as follows :
R'=22/255=0.086,
G'=G/255=32/255=0.125
B'=B/255=42/255=0.165
K = 1-max(R', G', B ')
=1-max(0.086,0.125,0.165 )
=1-0.165
=0.835
C = (1-R'-K) / (1-K )
=(1-0.086-0.835)/(1-0.835 )
=-0.079/0.165
=0.479
But the cyan value for the first pixel was 167 , and the calculated value was 0.479 .So what is the difference between these two values?
One uses a scale of 0-1 to represent intensities, the other a scale of 0-255. It doesn't look like you even understand colour representation, so I would encourage you to read about it.
As I've explained, any formula that you may find online that converts RGB to CMYK without using colour profile is completely missing the point of CMYK. For a start RGB and CMYK don't cover the same gamut, so some RGB colours may not be able to be rendered as CMYK and vice-versa. Again, there isn't one equation to convert RGB to CMYK (not everything you find online is correct). The actual formula depends on the devices.
I will also point you to the wikipedia article on CMYK which clearly states: "Since RGB and CMYK spaces are both device-dependent spaces, there is no simple or general conversion formula that converts between them".
thanks for the help
Well, now I want to convert 100 images from RGB to CMYK using matlab code .
First obvious question: why do you want to convert to CMYK?
Second question: have you got ICC profiles for both colour spaces?
The conversion itself is trivial, if you do.
I need to extract RGB and CMYK values for all these image then i want to combine these values with glcm features to do some classification . I don’t have ICC .
Why do you think that CMYK would be in any way useful for classification? Hint: it won't be at all.
The project requires CMYK and RGB values.
We're going in circles here. You need to review your project requirements. CMYK is a colour space used for printing and printing only. It's completely pointless to convert to CMYK if the intent is not printing. If the intent is printing, then you can't do the conversion without the colour profiles of the printer, paper, and creation media.
Are you sure you're not confusing with L*a*b or HSV or some other colour space?
I have extracted HSL values using matlab code from these images ,and I want the same thing to extract cmyk values . I do not intend to print.
Then you can't get CMYK values, at least ones that mean anything. Sure, you can find some formulae online that give you some CMYK values for a given RGB input. They'd be completely arbitrary values.
The below are 3 equally valid transformations of the same RGB input into CMYK:
rgb = cat(3, 0.2, 0.4, 0.6); %arbitrary rgb value for demo.
cform{1} = makecform('srgb2cmyk', 'RenderingIntent', 'Perceptual'); %conversion from SRGB to some unknown CMYK with perceptual intent
cform{2} = makecform('srgb2cmyk', 'RenderingIntent', 'AbsoluteColorimetric'); %same conversion using a different rendering intent
inprof = iccread('srgb.icm'); outprof = iccread('USSheetfedCoated.icc');
cform{3} = makecform('icc', inprof, outprof); %conversion from SRGB of a CRT to CMYK of a particular device
results = cellfun(@(c) reshape(applycform(rgb, c), 1, []), cform, 'UniformOutput', false); %apply each method to the RGB imput
%pretty display
array2table(vertcat(ans{:}), 'VariableNames', num2cell('CMYK'), 'RowNames', compose('Method_%d', 1:3))
results in:
ans =
3×4 table
C M Y K
_______ _______ _________ ________
Method_1 0.74487 0.43236 0.040909 0.18479
Method_2 0.88421 0.50463 0.0058442 0.030091
Method_3 0.85098 0.52549 0.1451 0.015686
3 different valid conversions, 3 different CMYK values for the same input.
Guillaume speaks the truth here. CMYK does not define any kind of specfic color, unless you know the printer the colors would be printed on, and you knew the inks/pigments that would be used, etc.
Even worse, for any given RGB code values, the CMYK will not be unique! That is, any RGB code values would result in what would generally be a curvilinear path through the CMYK code value space, all of those CMYK code values would in theory produce the same color on output.
Not all such CMYK combinations would be as desirable as some of the others. Thus on printers that use inks, too much ink on the paper can result in a waterlogged mess.
Next, there are issues with converting RGB to CMYK, in terms of gamut. Not all RGB code values have any combination of CMYK that will produce that color. For example, a super bright almost fluorescent looking green or yellow may well be out of the CMYK gamut. This was much more true on older devices, where I recall the HP deskwriter C, printing on plain paper, had a device gamut that was incredibly tiny. Better papers and inks did incredible things in this respect.
The problem with out of gamut RGB colors is then you need to choose which CMYK combination is the best possible match for the given color.
I could point out at least a few distinct such algorithms for gamut mapping, though it has been many years since I worked in that area.
It is easier to go the other way, thus for a given CMYK, you can at least build models & mappings to predict the "color" (pick your favorite color space) that would result for a given CMYK, printed on a specific device/paper combination. At least that mapping will be theoretically unique in that direction, although the modeling will generally use a variety of interesting techniques. (It would also involve writing a book to describe those modeling techniques, something I essentially did in my past career.)
Thank you . you talked about three different ways of converting from RGB to CMYK and the three different ways ,and the three different methods showed three different results So which method is the most suitable to use according to your experience?
It doesn't sound like you're getting it. I could come up with even more code variations which would all output different CMYK values, each corresponding to a different device (printer) and thus each equally suitable for that particular device. Converting to CMYK without a device profile is meaningless.
In any case, if you're not intending to print you don't need CMYK. You are wasting your time if you think that it will be useful for GLMC.
When I used the following command I got an error :
outprof = iccread('USSheetfedCoated.icc');
Error using iccread (line 45)
The profile was not found. Check the file or path name.
what is this error ?, and what is the soultion ?
Which device profiles are available depends on your system. You can list the ones that are installed with:
dir(fullfile(iccroot, '*.*'))
However it doesn't seem like you've yet understood the point we're making. You're wasting your time trying to convert to CMYK. Each profile available on your system will give you a different CMYK value for a given RGB input. CMYK is meaningless without the details of the matching rendering device.
what do you mean by rendering device?. I understood the point I made, but I want to convert the set of images that I have into CMYK and HSL and Lab LCH, XYZ to complete my project.
"I understood the point but I want to convert the set of images that I have into CMYK"
I'm sorry but if you had understood the point, you would no longer want to convert RGB into CMYK. It's pointless, it's meaningless. I don't know hiow many times I can say it.
You can convert to HSL, L*a*b* , XYZ with no problems. They're all additive colour models that more or less represent the same thing as RGB simply on different axis.
CMYK is device dependent. CMYK without a particular device doesn't mean anything. A device can be anything that uses some sort of pigment to render an image, a printer, a painting machine, a weaving machine even. a particular CMYK value on device A is a different colour on device B.
Thank you , but i have another questiion about this toic :
How can i use "imwrite" function to wtite the converted data to a file?. because i used this function but I got this error :
Error using writejpg (line 46)
Data with 4 components not supported for JPEG files.
You're still persisting with this idea of CMYK, which is not going to work!
While the JPEG format does support CMYK, not many software implement that support. In particular, matlab does not support it. In matlab, as far as I know, the only format that supports CMYK is tiff.
Again, you're wasting your time.

Sign in to comment.

More Answers (1)

How can i solve this problem? :
Cannot display summaries of variables with more than 524288 elements.

1 Comment

This is unrelated to your original question (and certainly not an answer to it) so you should probably start a new question.
Why is it a problem? Yes, the variable editor won't display matrices with that many elements. If it's an image you're trying to look at, imshow or imtool would be a better visualisation tool than the variable editor.

Sign in to comment.

Products

Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!