Filling a group of pixels with a new color?

12 views (last 30 days)
I've taken a picture, say 10x10 pixels, and converted it to doubles like this:
a = imread('photo');
b = im2double(a);
This gives me a 10x10 matrix of numbers that represent each pixel. I want to take a group of these pixels and change them all to the same color. I've tried changing the pixel values to the same value but this seems to just change the intensity of the image in that pixel group from dim to bright red.
Example:
b(1:5,1:5) = 100;
% I'm taking the pixels from 1 to 5 on x and y and making their value in
the matrix the same thing. Again, this just changes the intensity of
that group of pixels.
How can I fill those pixels with the same color? I've tried using the fill function as well, nothing is working.
The image is an unedited screenshot that I took on my computer.

Accepted Answer

Jeff E
Jeff E on 30 May 2013
From the sound of things you actually have a 10x10x3 matrix. The "3" represents the color information stored in the red, green and blue channels. You can confirm this with:
size(a);
where "a" is your image.
Your attempted modification:
b(1:5,1:5) = 100;
is only adjusting the red channel. If you want to change all the color layers, you need to use:
b(1:5,1:5, :) = 100;
The imoverlay function in the File Exchange is a good example of how to change image color to a specific value: http://www.mathworks.com/matlabcentral/fileexchange/10502-image-overlay
  1 Comment
Image Analyst
Image Analyst on 31 May 2013
Edited: Image Analyst on 31 May 2013
If you have a 2D matrix, like one you could do b(1:5,1:5) with, then it's either a monochrome (grayscale) image, OR it's an indexed image with associated colormap, OR it's one of the R, G, or B color channels that have been extracted from a color image. But you references b(1:5,1:5) and said something about color. So now we don't if it's an indexed image, or if you were wanting to just refer to the upper left quadrant but left off the third dimension, or if you meant something else. So, if it's an indexed image, you need to change the image to a gray level where the color map is red for that gray level, or if it's a color image you have to do
% Make upper left 5x5 square red by setting red channel to 255
% and blue and green channels to 0.
b(1:5, 1:5, 1) = 255;
b(1:5, 1:5, 2) = 0;
b(1:5, 1:5, 3) = 0;
imshow(b); % Need to refresh display to see it.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!