Reanimation
Reanimation

Reputation: 3346

Convert image to a Chromaticity image

I'm trying to write a program that takes an image and converts it to a chromaticity image in MATLAB.

As I understand it, to achieve chromaticity, you must calculate r,g,b from R,G,B, by:

equations

So, I've done this (or at least attempted it) but it just returns a black image...

Here's my program:

    img = double(imread('ref_images/foliage.jpg'));
    %temp
    chrom_Img = zeros(size(img, 1), size(img, 2));

    %split channels
    R_ = img(:,:,1);
    G_ = img(:,:,2);
    B_ = img(:,:,3);

    %sum RGB channels (R+G+B)
    s_RGB = R_ + G_ + B_;

    %calc r g b with r = R/sum etc
    r_ = R_./s_RGB;
    g_ = G_./s_RGB;
    b_ = B_./s_RGB;

    %assign to chrom_Img
    %chrom_Img = cat(3, r_, g_, b_); %also tried this
    chrom_Img(:,:,1) = r_;
    chrom_Img(:,:,2) = g_;
    chrom_Img(:,:,3) = b_;

What am I missing or implementing incorrect for this to be happening?

To see an example image, see here.

Upvotes: 2

Views: 1734

Answers (1)

chappjc
chappjc

Reputation: 30589

Your problem is data type. When dealing with integers any fraction less than one is zero:

>> uint8(10)/uint8(100)
ans =
    0

When you do r_ = R_./s_RGB; everything goes to zero.

Also, instead of the manual sum (s_RGB = R_ + G_ + B_;), you can do:

s_RGB = sum(double(img),3);

This will sum over the third dimension.

Convert img to double up front and it should work fine:

chrom_Img = bsxfun(@rdivide,double(img),sum(double(img),3));

Result (based on this image):

enter image description here

Upvotes: 4

Related Questions