Steve on Image Processing

Concepts, algorithms & MATLAB

All about pixel colors: Grayscale and binary images 137

Posted by Steve Eddins,

In my previous postings on this topic, I've discussed the basic image display models in MATLAB - truecolor and indexed. The Image Processing Toolbox has conventions for two additional image display models: grayscale and binary.


Grayscale image display

If you pass a single argument to the toolbox's two main image display functions (imtool and imshow), they'll interpret the input as a grayscale image. Here's an illustration using a simple sinusoid:

theta = linspace(0, 2*pi, 256);
I = repmat((-cos(2*theta) + 1)/2, [256 1]);
h = imshow(I);                % Save the handle for use below.

As far as MATLAB itself is concerned, this is a scaled indexed image being displayed in a figure with a grayscale colormap installed. Here are the key properties that have been set to control the image display:

get(h, 'CDataMapping')
ans =


The toolbox convention is that, for floating-point images, 0 is displayed as black and 1 is displayed as white.

get(gca, 'CLim')
ans =

     0     1

map = get(gcf, 'Colormap');
map(1:5, :)                   % Display the first few colormap colors
ans =

         0         0         0
    0.0039    0.0039    0.0039
    0.0078    0.0078    0.0078
    0.0118    0.0118    0.0118
    0.0157    0.0157    0.0157

imshow (and imtool) handle all these details for you.

Controlling the grayscale display range

imshow and imtool allow you to override the conventional display range and specify your own black and white values. You do this by providing a second input argument, a two-element vector containing the black and white values. In the call to imshow below, 0.4 (and any lower value) gets displayed as black. The value 0.6 (and any higher value) gets displayed as white.

imshow(I, [0.4 0.6])

Binary image display

The other Image Processing Toolbox image display model is the binary image. If you supply a single input argument that is logical, then imtool and imshow (as well as many other toolbox functions) interpret that input as a binary image.

bw = imread('text.png');
ans =


h = imshow(bw);

Relevant toolbox documentation

The Image Processing Toolbox Users Guide has a section called "Image Types in the Toolbox." This section describes the image types binary, indexed, grayscale, and truecolor, and it explains important dynamic range conventions as well. This is essential information for you to know in order to use the toolbox effectively.

Get the MATLAB code

Published with MATLAB® 7.2


Comments are closed.

137 CommentsOldest to Newest

yingcai zheng replied on : 1 of 137

Hi Steve,

I got a 3D volume data. I ‘slice’ a vertical crosssection out of the vol. data. I have a colormap so that the sliced image is displayed in color. How can I show the image using truecolor? i.e., get RGB value for each pixel. The reason to do this is to add a ‘hue’ value on the RGB. The hue is like an error bar, the bright spot means small error. dark spot means large error. thanks for your answer.


Steve replied on : 2 of 137

Yingcai – Take each value from your data set, scale it appropriately to get the colormap index, and then get the RGB colors for that data set value from the colormap. Look at this post to see how the scaling works.

Steve replied on : 4 of 137

Mustapha – 1. Convert to grayscale first (ind2gray or rgb2gray). 2. Find the pixel values corresponding to the structure. 3. Call mean.

I = rgb2gray(rgb);
structure_pixels = I(structure_mask);
Ron replied on : 5 of 137

Hi Steve,

I need vertical sine on 480×480 tif file.
How can I do it ?
the frequency should be variable also the gray level of backround and sine line.

Thanks in advance,

Hekmat replied on : 7 of 137

Hi Steve
I have an image of a animal brain with a small tumor in the brain. I want to retain the entire image in gray scale and colormap only the tumor region in the brain with colorbar ranging from 0 to 100. How to proceed with this?

Steve replied on : 8 of 137

Hekmat – I’ll assume you have three images: a grayscale image (I), an indexed image (x,map), and a mask image indicating the tumor location (mask). Then you might proceed like this:

% Initialize red, green, and blue components to be
% the same as the grayscale image.
red = I;
green = I:
blue = I;

% Convert the indexed color image to rgb.  
% Convert it also to uint8, assuming that's what
% the grayscale image is.
rgb = im2uint8(ind2rgb(x,map));

% Within the masked region, assign pixels
% from the color image to the red, green,
% and blue components.
red(mask) = rgb(:,:,1);
green(mask) = rgb(:,:,2);
blue(mask) = rgb(:,:,3);

% Combine the red, green, and blue components
% into an RGB image.
rgb_out = cat(3, red, green, blue);
Pradeepan replied on : 10 of 137

Hi Steve
I am taking a picture on a CMOS camera that is being transmitted wireless to the PC through COM port 4 and the data I am receiving are pixel values (8 bits for one pixel). I am not sure what format it is (JPEG or BMP?), but I’m wondering if MATLAB can read these values and save it as a picture format

Thank You

Pradeepan A.

Naven replied on : 11 of 137


I am also facing same sort of thing were i am taking image from cmos data. i have that image data in my memory what i am received is an 8 bit data. I wan to implement connected component on the data.i am wondering how to do it.

hekmat replied on : 12 of 137

As per your suggestions(replied on March 28th, 2007 at 2:34 pm)
, I tried to work on this, it is giving following errors
what does it mean?
??? Index into matrix is negative or zero. See release notes on changes to logical indices.

Error in ==> C:imtry2.m
On line 57 ==> out_red(bw)=rgb(:,:,1);

Steve replied on : 15 of 137

Pradeepan and Naven – The Image Acquisition Toolbox offers a way to get camera data via framegrabber, USB, or IEEE 1394 (FireWire) ports. MATLAB has a serial port interface that you might be able to use.

thomas replied on : 16 of 137

do you know if it is possible to use the slice function with rgb data and transparency ?

Steve replied on : 17 of 137

Thomas – From the doc, it looks to me like slice is intended for volumetric data, not RGB image data.

Palash replied on : 18 of 137

Hi steve
I have an avi video measureing tumor kinetics.I want to find the average intensity of each pixel in the tumor over time specifying the ROI.

sudhanshu replied on : 20 of 137

i want to copy pixel coordinates and pixel intensity from pixelregion to text file . although i can copy and paste it but it is taking too much time because of large number of pixels. Is there any way to copy and paste it in text file by any single command and easy way method?

Steve replied on : 21 of 137

Sudhanshu – You can use the MATLAB clipboard function to copy values from a matrix to the clipboard. Here’s an example:

>> I = imread('cameraman.tif');
>> clipboard('copy', I(1:3,1:3))

copies this string to the clipboard:

[156 159 158;160 154 157;156 159 158]
sudhanshu replied on : 22 of 137

I am processing sea floor in a software “poseidon” where i have got intensity values of backscatter data, i mean, acoustic waves. i have got the pixel intensity of all the pixels in a textfile and its an array of very high order(>1000). now i want to do some statistics in matlab like getting proability distribution function. so i will have to read that textfile first and then what i should do next.please tell me in detail

Steve replied on : 23 of 137

Sudhanshu – I can’t tell you in detail what to do, but I think I can give you a pointer to get started — take a look at the textscan function.

sudhanshu replied on : 24 of 137

that part of reading textfile i have al;ready did bt i want to know about this proability distribution of the array..its urgent, please tell em soon if u can.

Steve replied on : 25 of 137

Sudhanshu — You can get a rough idea of the distribution of values in the file by using the hist function. See the Statistics Toolbox for tools to estimate probability density functions.

sudhanshu replied on : 26 of 137

hi steve,
jst clear my doubt.
i have a matrix and i want to draw a 2-D graph . X-axis will be frequency and y-axis will be elements of the matrix. In short, i want to know the frequency of an element of the matrix..and the matrix is of high order.please tell me

sudhanshu replied on : 27 of 137

sorry, i want x-axis be the elements of the matrix and y-axis frequency means how many times that element of the matrix is there in the matrix

Sid replied on : 29 of 137

Hii Steve, I am working on image RGB vaues…I want to find out the RGB values of each and every pixel of the image an I want to write those values to a file. For example if an image of 1024 resolution…. I want to find out the RGB values of every pixel…Hw can I do that…I wrote a small code for that but it is not working….Plzzz help????

Steve replied on : 30 of 137

Sid—RGB images are simply M-by-N-by-3 arrays in MATLAB. You obtain pixel values by indexing into the array. Here’s how you would obtain the red, green, and blue components for the pixel on row 34, column 25:

red = rgb_image(34,25,1);
green = rgb_image(34,25,2);
blue = rgb_image(34,25,3);
smita replied on : 31 of 137

hi steve i m studing in final year of BE in electronics and communication and i m working on noise reduction in images though this topic is very simple n easy for you but for me it is too hard.but when i visted to your site i get lot of knowledge about matlab.thanks

mat replied on : 32 of 137

Hi Steve,
hope you can help me with this:
I have an 2 images
(1) the data image I with several regions of interest (ROIs) (2) a mask with the locations of the ROIs.

I label the ROIs (using “[mask,numObjects] = bwlabel(x,4);”).
Now i would like to know for each ROI the pixel with the largest intensity and the x/y
coordinates of this pixel in the data image. I think i know how to get the maxium intensity pixel (find(max(…)). But how to find it’s coordinates in the original data image?

Steve replied on : 33 of 137

Mat—That’s a great question. You can do something like:

L = bwlabel(x, 4);
s = regionprops(L, 'PixelIdxList');
% Find location of maximum-intensity pixel in 1st region:
[maxval, maxidx] = max(I(s(1).PixelIdxList));

% Find linear index of maximum-intensity pixel:
idx_list = s(1).PixelIdxList;
i = idx_list(maxidx);

% Convert linear index to row-column coordinates:
[r,c] = ind2sub(size(I), i);
stev replied on : 36 of 137

Hi Steve,
I have a matrix J (250×200) which contains several ROIs (equidistinaly spaced and about 6×5 in size) with non-ROI pixel values set at 0. 1) I would like to find the minimum pixel intensity value within each ROI in J and 2)place that minimal value in the same x,y coordinate in a new 250×200 matrix (all other pixel values in the new matrix should be 0). Hope you can help.

Garry replied on : 39 of 137

Hi Steve,
I have an image of 4 objects and a set of x-y axis. I have filtered the image so that all the objects and the axis are white and the background is black. I would like to be able to locate the white objects and attached a set of coordinate axis to each object with respect to the axis in the image.
any ideas?

Steve replied on : 40 of 137

Garry—You can use bwlabel to identify the objects, and then use regionprops to compute the centroid of each one. I don’t have any suggestions about the “attaching a set of coordinate axis to each object” part of your question. You can probably draw the necessary lines using MATLAB graphics functions.

Abrar replied on : 41 of 137

Hello Steve,
I have a fingerprint image and I want to mark or color
a specific pixels,each pixel has a different meaning,
that’s why I want to show it,
but I don’t know how can I make this marks.
I hope you can help me.

Steve replied on : 42 of 137

Abrar—You change the color of an image pixel simply by assigning a new value to it.

% I is uint8 image matrix
I(50,100) = 255;  % Make pixel (50,100) be white.

You might also want to look at my post about the imoverlay function on the MATLAB Central File Exchange.

Abrar replied on : 43 of 137

Hi Steve,

Thanks for your reply,

but this way will not be efficient, because its a binary image and makeing this pixel white will not be clear in the pictuer,
so I need to mark my minutiae by useing star (*) or somthing else,

could you help me in this.

Damodar Pokhrel replied on : 44 of 137

Hi Steve;

i have a gray valuse image(768×1024), it contains black seeds at the center on the gray background, and rest of the image outside gray value is black (three different regions). i am trying to extract black seeds only in the uniform white
background. could you please help me out?


Steve replied on : 46 of 137

Damodar—You might find the Image Processing Toolbox functions graythresh and imclearborder to be helpful.

erhan replied on : 48 of 137

I have 2 grayscale image, and I want to display both of them on the same figure. one of them will be a colormap(like imagesc do) while the other one as grayscale. It is similar to the Hekmat’s problem, but I dont have rgb images for the mask regions and I want to use what imagesc do for those masked regions. Is there any way to implement a imagesc operation on a ROI of grayscale image? Thx.

Steve replied on : 49 of 137

erhan—Figures can have only one colormap at a time. You will probably need to convert both images to truecolor format and do the pixel scaling arithmetic yourself.

erhan replied on : 51 of 137

Hi Steve, thx for your previous suggestion, it works fine. But I have another problem( with the colorbar function). Although all my pixels are in the range of [0-1], when I use colorbar option of Matlab, my colorbar seems to be between [0-64] for 3-channel image. Do you have any idea about this? Thx

Steve replied on : 52 of 137

Erhan—MATLAB displays a three-channel (truecolor) image independently of the figure colormap, so using colorbar doesn’t really work. Besides, if you have three channels, then you have three numbers per pixel, so what scale should be plotted on a colorbar anyway?

preethi replied on : 53 of 137

hi steve .. we are beginners in image processing .. i have an image 200*200. i need to divide the image into 5 * 5 size to get 40 blocks . and need to find the average intensity of each of the block.. can u help me out in this..

kellie replied on : 55 of 137

Hi Steve,
I know this must have a simple answer but I can’t find it. I have an RGB image. For each pixel in the image, I want to assign a height value. So it is actually an m x n x 4 matrix (because of R, G, B, height). I am wondering how to plot this so that the 3D image will elevate each pixel to the assigned height in the Z axis but retain the R,G,B truecolor value. Thank you so much for your help.

Tom replied on : 57 of 137

Hi Steve,
how can i cut out a ROI that is specified by a binary mask?
My raw data consists of image stacks (50 images, 1392×1040). After inspection i select a ROI somwhere in the first image (e.g. a 300×300 pixel square somwhere in the image). Now i’d like to cut this ROI from every image in the stack and use these cut-outs to form a new, reduced stack of 50 images, 300×300.

Steve replied on : 58 of 137

Tom—Extracting a rectangular subimage in MATLAB is just a matter of indexing.

cut_out = A(r1:r2, c1:c2);

If your image stack is represented as a 3-D array, you could do the indexing for all the images in one step:

cut_out = A(r1:r2, c1:c2, :);
Muchacha replied on : 59 of 137

steve, i’ve try to divide the 100*100*3 (RGB image) into 3×3 size block..i know to process block image is by using blkproc, but how about color images? can we use blkproc function too?

Muchacha replied on : 61 of 137

Oh too bad..
I’ve no idea how to do it.. :(
is there any suggestion on how to do it?
i mean, what is the first step to do..
where i can find the reference?

Steve replied on : 62 of 137

Muchacha—You really just need for loops and ordinary matrix indexing to extract the submatrices (blocks). If you’re not that familiar with MATLAB programming, you might want to go through the Getting Started Guide in the MATLAB documentation.

Ed replied on : 64 of 137

Dear Steve,
Here is my problem (I am new at Matlab):
I have a binary tif image showing an irregular line.
Since this is a binary image, the pixels of this line have Gray levels of = 1 and the surrounding background pixels have gray level values of = 0.
This line is thick which means that within each column of the image array, there are 2 or more y coordiantes that have pixels with gray levels of 1 (so the line is more than 2 pixels thick).
What I need is first to transform this thick line into a thin line (one pixel thick), then, I need to get the x,y values of this thin line in order to be able to plot it as a graph.
I think that the best way to make this line thinner
(1 pixel thick), I would have to do the following: for each column (x value) to average the y coordinates that contain gray levels of 1 (the line). This way, I will be able to get a new image array with the same x values but with new y coordinate values, one y point for each pixel that contains GL=1.
Once doing this transformation, I will need to figure out how to get the new x,y coordinates (of this new thin line), in order to plot them in excel, as a graph.
Please help !
Thank you,

Arvind replied on : 66 of 137

I need help in converting rgb to gray of a image taken in black and whit mode of a IR camera whose specifications are 640 480 3. Please help as soon as possible


Ed replied on : 68 of 137

Dear Steve,
Thanks for the “thin” option tip. I still have problems with this issue. The skeletonization option created multiple vertical lines in addition to the horizontal line.
I am therefore trying to solve this issue in a different way, as follows: my image contains a thick dark line (GL=1), surrounded by a white (GL=255) background.
By using:[x,y]=find(a<255) I was able to obtain the XY coordinates of my line. Let’s say I got the following array (truncated, for illustration purposes) :
b =
2 2
2 1
2 2
3 1
3 2
3 3
Let’s call the left column “X” and the right column “Y”.
I need to calculate the mean of all these “Y” values that have a similar “X” value. So, for X=2, I need to average
the following y values: 2,1,2. For X=3, I need to average y the following y values: 1,2,3. If I am able to do this
automatically, for a longer array, then I will reduce the thickness of my line, to one value (the average), per X coordinate.
Many thanks in advance !

Ed replied on : 69 of 137

Hi Steve,
I have solved my line problem (above) first by using the “find” function to get the xy coordinates, then I created a “for” loop with an “if” condition.
Thanks anyway,

Lion replied on : 71 of 137

Hi Steve,
let Xbin be a binary image. When i write using .jpg format it automatically gets converted into grayscale because by default bitdepth of jpg is gray-scale my question to you:
Q) How imwrite(Xbin,’file.jpg’) is converting binary image into grayscale when and with what algorithm

Steve replied on : 72 of 137

Lion—You can see for yourself exactly what imwrite is doing by typing:

>> edit private/writejpg
sam replied on : 73 of 137

hi steve,

i have a binary image.How can i display the image in matrix form and how can i calculate the total numbers of ‘0’ and ‘1’ in matrix?


Steve replied on : 74 of 137

Sam—To display the image in matrix form, simply type its variable name at the command prompt and leave off the semicolon as you might do with any other matrix in MATLAB. Use the sum function and the ~ operator for calculating the number of 0s and 1s. You might want to look at the Getting Started section of the MATLAB documentation.

Alex replied on : 75 of 137

Dear Steve,
I just found this website like 2 days ago. It seems great! I have a pretty simple question, probably. So if I want to take an RGB image and find the average pixel intensity, then is converting it to grayscale the way to go? Does grayscale accurately capture the intensity of a truecolor image?

Steve replied on : 76 of 137

Alex—It’s not really a simple question. Many RGB color spaces do not uniquely define a color, because they are device-dependent spaces. That is, they depend on the physical characteristics and settings of the device, which might be a monitor, a camera, a scanner, etc. Also, when some people talk about “intensity” they expect the term to refer to a physical measurement of light intensity. Others use the term more loosely. So when you ask “Does grayscale accurately capture the intensity of a truecolor image?”, I have to respond “it depends.” It depends on what you mean by “grayscale,” what you mean by “accurately,” what you mean by “intensity,” and what you mean by “truecolor.” I’m sorry to give a less-than-completely-helpful answer, but these issues are why I am often heard to mutter that “color science makes my brain hurt.”

Tina replied on : 77 of 137

Hi Steve,
I have a problem of converting a ring shape extracted from an image into a rectangular strip. I want the values of the ring to be mapped to the values of the rectangular strip.

Steve replied on : 78 of 137

Tina—See the “Exploring a Conformal Mapping” demo on the Image Processing Toolbox examples page.

Alex replied on : 79 of 137

Thanks a lot for your reply, Steve. Both here and to my other question in the “truecolor and indexed images” section.

Michael Krause replied on : 80 of 137

Hi Steve,

I am trying to calculate the threshold value to convert a grayscale image (~5000 pixels x 3600 pixels) to a binary image. I know the target value 21% of the area, so I am iterating to find the corresponding luminescence. I am finding however that the range is too small to give me the right answer. Is there a way to check the grayscale pixel values instead, I know the cutoff is the value 187 in grayscale (from ImageJ), but I can’t get matlab to find this using the Luminescence threshold of 0 to 1, it will find 20.6% and then jump to 21.4% no matter what the luminescence increment is.



Steve replied on : 81 of 137

Michael—There’s a great deal about your question that I don’t understand … Why do you need to calculate a threshold value that you already know? What does “21% of the area” mean? What is your definition of luminescence and why are you using it? What is the “it” that’s jumping from 20.6% to 21.4%?

So let’s start here: What’s wrong with:

bw = my_image > 187;
Gene replied on : 82 of 137

Hi, Steve

I have a binary image with only white and black.
I would like to calculate the white area vs the total.

These white area are irregular shaped.

How can I do it?

prasad replied on : 84 of 137

how to convert an black and white image into pixel values in matrix form and again how to reconvert matrix form to black and white image.

Samarth replied on : 86 of 137

hi steve,
i want to count the no. of white blocks(50*50 pixels) in my black and white photo.
can u please help me out?

Chaitanya A. replied on : 88 of 137

Hi Steve,
I saw your reply to Hekmat and seems like I had the same problem. But I was wondering more why did MATLAB do away with


that was around in the old versions. Is there a replacement for it? I am now using MATLAB 2008b with the ImageProcessing Toolbox and the absence of imoverlay seems unfortunate. To summarize my problem I have 2 grey scale images, one of which I want to allocate the color red and save the two together as RGB.


le replied on : 90 of 137

i have an rgb image and i would like to binarize it so that yellow colours are white and non-yellow are black. how do i do this using your binary method?

mansi replied on : 94 of 137

i mean the intensity value of a pixel in an image..
i hav the cordinates of the pixel….but cannot calculate the intensity of that pixel.

thank you.

Steve replied on : 95 of 137

Mansi—An image is represented in MATLAB as a matrix. Just use ordinary matrix indexing to extract the value of a pixel: A(r,c).

Madiha replied on : 96 of 137

i am working on number plate extraction in MATLAB. i hac converted an image from rgb to HSI….now i need to convert the intensity map to a binary do i do tht?

Santosh Bansode replied on : 98 of 137

Hi Steve,
Let me know how to calculate average pixel values in a grey scale image in a certain pixel region specified using the imtool. I want to calculate the average pixel values in each rectangle marked area as I have an image with different levels of greyness in it starting from black to white. Cheers and thanks for the help. Santosh.

Steve replied on : 99 of 137

Santosh—To compute the mean of a rectangular area of a grayscale image I:

result = mean2(I(r1:r2, c1:c2));
asanka replied on : 100 of 137

I read a color image and convert it to grayscale. When I dispaly the grayscale image it is in red and blue, not in the shades of gray. Why is that? How can I display the grayscale image in the shades of gray?

Steve replied on : 101 of 137

Asanka—I assume from your description that you are displaying the image using the MATLAB image function. This function does not supply a colormap automatically, so typically the default figure colormap gets used. Use the colormap function to supply a gray-scale colormap, or use the Image Processing Toolbox display functions imshow and imtool.

shankara nand mishra replied on : 102 of 137

i want to find the position and number of pixels corresponding to any intensity value in a gray scale image and plot them…

Steve replied on : 103 of 137

Shankara—Find position and number:

[y, x] = find(im == val);
num = numel(y);

As for plotting, your options are many. See that MATLAB graphics doc.

Stella Karra replied on : 105 of 137

Hey Steve,

I have a tif image and would like to see the pixel values from an area.I have used the imtool and see them but I don’t know how to get them and save them.Can you help please?

AGD replied on : 106 of 137


I am trying to apply the thinning algorithm in order to get an image which is 1-pixel wide.
My image is in the form of 0’s ans 1’s. How do I convert the entire matrix to 0’s and 255’s respectively?


Steve replied on : 107 of 137

AGD—I don’t know why you would have to convert 1s to 255s in order to apply a thinning algorithm. But you could use:

B = im2uint8(A);


B = A;
B(A ~= 0) = 255;
Asmat Ullah replied on : 108 of 137

Dear Steve:
I am currently a Professor in Image Processing and a big fan of your blog.

My question is how to use adapthisteq function iwth bimodal distribution. As I have fingerprint images, I would like to enhance it using adapthisteq.

M Asmat Ullah Khan

Steve replied on : 109 of 137

Stella—Images are just matrices in MATLAB. You extract pixel values using matrix indexing:

p = A(15,20); % pixel value on 15th row, 20th col
sub = A(15:20, 20:25)  % 5-by-5 subimage
Amir replied on : 113 of 137

I’m encountering a problem reading .tif 10 bit images, matlab reads the bottom half of the image wrong ” it reads only three raws and keep repeating those 3 rows for the entire second half of the image.
any idea why?

Billy replied on : 115 of 137

I am a college freshmen and brand new to MATLAB. As an extra-credit problem for my class we have an image which we must find the hidden riddle and answer it. I believe the image has some pixel layers removed, using the command , is there any way to put layers back on top? Thank you,


Amir replied on : 116 of 137

Steve, I took the images with High speed camera we use in our lab, and I open these files with other softwares.

Steve replied on : 117 of 137

Amir—In that case, the best to proceed is to contact MathWorks technical support and provide them with a sample file. It would also be helpful to provide a screen shot, if possible, of what the image is supposed to look like.

Steve replied on : 119 of 137

Billy—I don’t understand what you mean by “pixel layers removed.” You might want to take a look at this post for a simple way to hide and reveal information in an image.

Tasha replied on : 120 of 137

Steve,I send you a comment here but still didn’t get any reply yet.I did not see my comment posted here to.

Steve replied on : 121 of 137

Tasha—I only accept comments that are relevant to the particular blog post or are questions or comments about the blog itself. Also, for comments that I do accept it often takes me several days (or even a week or more) to respond. That’s because in addition to running this blog I have a real job. ;-)

Ika Candra replied on : 122 of 137

steve, i want to convert image after segmentation using thresholding to RGB image

each pixel if the value of bw image>1 (white) will be set in pixel value @ RGB Image(i,j)

if BW(i,j) T
BW(i,j) = rgb_image(i,j)

the code:

[M,N] = size (BW) ;
[M,N] = size (rgbImg);

    for i=1:i < M
        for j=1:i:i  T
                d(i,j)= rgbImg;
               else d(i,j)=0;

but it doesn’t work??
how can i process each pixel value into rgb image??

Ika Candra replied on : 123 of 137

sorry steve this is the code

[M,N] = size (BW) ;
[M,N] = size (rgbImg);

    for i=1:i < M: i++
        for j=1:j  T
                d(i,j)= rgbImg(i,j);
               else d(i,j)=0;

    figure, imshow(d(i,j));
Steve replied on : 124 of 137

Ika—Well, the code as you’ve posted it doesn’t work because it’s not valid MATLAB syntax. The for-loop expressions look more like C, and you’re missing an “if” to go along with that “else.” I can’t quite tell what your real intent is with this code, so I can’t give you better advice.

kavitha replied on : 125 of 137

I have a problem in counting number of black pixels and white pixels in binary image. pls send me the code or function in matlab

sundar replied on : 127 of 137

Hi Steve,
I have a problem with segmentation. Since I was not able to post my comment in your cell segmentation blog, I am posting it here. I hope it is fine.
I am trying to segment protein accumulations within the embryo. Please have a look at image 1 in the link.
I am using the following code to segment and extract protein accumulations but I am unsuccessful so far and I have problems in the final watershed transformation.

% read in image (see image 1 in link)
I = imread(‘Proj of unc60_rnai_nmy2gfp_3s_14hr0000.tif’);
se = strel(‘disk’,6);
se2 = strel(‘disk’,2);
It = graythresh(I);
I1 = im2bw(I,It);
I2 = imdilate(I1,se);
I3 = I2-I1; % apply external gradient to identify embryo
I4 = imerode(I3,se2);
I5 = imfill(I4,’holes’);
In = normalize(I);
Ifin = I5.*In;

% the above steps assigns every pixel outside the embryo to zero (see image 2 in link)

mask = imextendedmax(Ifin,0.5);

% It already looks like it has segmented well but I would like to further use watershed to remove protein accumulations that are connected by a small number of pixels (see image 3) I am fine until this step. I have problems after this.
I complement the original image and use imimposemin and watershed on it. But this is what I get (see image 4)

Icomp = imcomplement(Ifin);
Imod = imimposemin(Icomp,mask);
l = watershed(imod);

Please help me.

Here is the link where you can find all the images

sundar replied on : 128 of 137

Sorry I used the wrong word in the following line in my previous post
% It already looks like it has segmented well but I would like to further use watershed to “segment (and not remove)” protein accumulations

Steve replied on : 129 of 137

Sundar—I don’t recall seeing this comment submitted before, but it is certainly off-topic for this post. In any case, I don’t have time right now to provide individual consulting on image segmentation problems. You might consider posting your query to the MATLAB newsgroup; there are some knowledgeable people there who often chime in on image processing questions. You might consider providing your sample images in a more common format that’s easily displayable in a browser, such as JPEG or PNG. Many people will not be able to diosplay your .eps files.

Ali replied on : 131 of 137

Dear Steve,

I have got a gray scale image (MRI image) that has values between 0 and 1, I am interested in displaying the pixels in different colors such as

pixels with values 0-0.3 in yellow
pixels with values 3.1-5 in blue
…. etc
Many thanks

Kaly replied on : 133 of 137

Hi Steve,

i have a binary image. how to display it in the 10 by 10 matrix form?
i tried by giving simply a variable name but there are so many no. of rows & columns displayed.
please help me.

Steve replied on : 134 of 137

Kaly—Pick which pixels you want to display. For example, display the upper-left 10-by-10 block:

  bw(1:10, 1:10)
shahnawaz replied on : 136 of 137

Hi steve I posted a question about finding distances between binary objects. I thought it would be relevent as aspects of binarisation are being discussed here using Matlab ??

Steve replied on : 137 of 137

Shahnawaz—I did not think your question was relevant to the topic of this post. This blog is not a general help forum; comments should be relevant to the posts.