Yuita A. Sari

“If you want to be a writer, you must do two things : read a lot and write a lot. There's no way around these two things that I'm aware of, no shortcut.” ― Stephen King

[Matlab] How to Retrieve Color from Segmented Image?

Leave a comment

Back to code, yeah! Still in the simple code. Yep, simple code that is still quite make several people not clearly enough hove to solve it. At the moment, we will discuss about how to retrieve or return the color of segmented image. This topic is still related to image processing or computer vision, so if you are not in that specific interest area, perhaps it is not too useful. However if you are one of the people who has high curiosity, it is not close opportunity to give inquire if there is anything you want to know.

In this case, for instance  I have an image as shown below. Do you know what is that? (Yep, tomato :D). Segmentation is mostly happened in the first step in image processing. We have to recognize the object of an image to be processed in the next phase. Otsu thresholding was used in this case, and you can modified of Otsu thresholding as you wish in order to upgrade the quality of the segmentation. As we know, there are a lot of algorithms nowadays that compete to achieve the best segmentation, and it is always an open case. Well, you can follow this code right now to try.

tom

% Copyright by Yuita Arum Sari
% This function is able to retrieve or return original color from the
% foreground of an image
% Input : file name of the image (full path, if applicable)

function retrieveColor(fileImage)

% read image
I = imread(fileImage);

%segmented image with Otsu thresholding
level = graythresh(I);
BW = im2bw(I,level);

% Retrieve color from segmented image
dim = size(BW);

% initialization of an image
retrieved = zeros (dim);
[a b]= size(BW);
retrieved = imresize(I,[a b]);

Bw = ~BW; % inverse black and white
R = retrieved(:,:,1).* uint8(Bw);
G = retrieved(:,:,2).* uint8(Bw);
B = retrieved(:,:,3).* uint8(Bw);

% retrieved color
retrieved(:,:,1) = R;
retrieved(:,:,2) = G;
retrieved(:,:,3) = B;

figure
subplot(1,3,1), imshow(I), title ('Original Image');
subplot(1,3,2), imshow(Bw), title ('Segmented Image');
subplot(1,3,3), imshow(retrieved), title ('Retrieved Color');

Have you finished to code it? Alright, you can event copy paste this function to try. Then, you can run code by this code

retrieveColor('tom.jpg')

Screen Shot 2014-05-29 at 12.44.03 pm

From picture above, you can see original image, segmented image, and retrieved color, respectively. If you want to deal with gaining teh RGB value, you can add by your own :

function [R,G,B]= retrieveColor(fileImage)

Well, is it easy?😀 Do you want anything to share related this post? I am welcoming of your comment. Thank you!  :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s