Change image background color - Place pixels of image into array in order to find white pixels. Then change white to grey.

5 views (last 30 days)
Hi all,
I am trying to find a way to change the background color from white to grey in a very large amount of images. I reckon the best way is to create a matrix containing the values of each pixel, and then search for all values corresponding to white, replacing them with values corresponding to grey. I am just really not sure how to get there as I am new to Matlab.
Steps I think I need:
  • 1. Load image
  • 2. Create matrix of pixels
  • 3. Find white pixels in image
  • 4. Replace white pixels with grey pixels
  • 5. Save new image*
This method is obviously not ideal as it will also change pixels that are not part of the background. But this is the best I could come up with. Any other suggestions are welcome!!
function changecolorImages
% Change background color of images
% location images
pathToImages = 'C:\...expImages';
d = dir(fullfile(pathToImages, imageNames));
nImages = length(d);
for iImage = 1:nImages
[im, map] = imread(fullfile(pathToImages, d(iImage).name), 'bmp');
%if image black/white(2dims),change to 3 dims
if ndims(im) < 3
%Convert to color
im = ind2rgb(im, map);
end
*here I would like to find a way to go through every pixel in the image, and have all the pixels that are white changed to grey
*Maybe something along the lines of
ImageMatrix = MakeMatrixofImagePixels for i:LastElementInMatrix if white change to grey...
or maybe with the find function find...white pixels...*
% Save new file
imwrite(im, fullfile(pathToImages, filename));
end
end
% end
Thank you for your time, it would be a great help!

Answers (1)

Image Analyst
Image Analyst on 11 Apr 2014
First you need to decide if the image is gray scale, RGB, or indexed. You can look at the colormap and the number of color channels. If it's color or grayscale, simple change all pixels at 255 to the new gray level. If it's indexed, you need to find out which index in the colormap is white, then change that index to the the new gray level, and apply that new color map. Here's some untested code:
[rows, columns, numberOfColorChannels] = size(im);
if numberOfColorChannels == 3 || isempty(map)
% RGB or standard gray scale.
whitePixels = im == 255;
im(whitePixels) = newGrayLevel;
% Write out
imwrite(im, filename);
else
% indexed
newColorMap = map; % Initialize
% Find white and change it to gray
for gl = 1 : size(map, 1)
if map(gl, 1) == 255 && map(gl, 2) == 255 && map(gl, 3) == 255
% Change from white to our gray level
newColorMap (gl, 1) = newGrayLevel;
newColorMap (gl, 2) = newGrayLevel;
newColorMap (gl, 3) = newGrayLevel;
end
end
% Apply new colormap
imwrite(im, newColorMap, filename);
end
I didn't test it - that's just off the top of my head - so let me know if it works.
  2 Comments
Rajan Paudyal
Rajan Paudyal on 31 May 2014
Hello, I was also trying to do something similar as Aaki asked. I used your following code to change whitepixels to some fixed gray value (eg. 110) in my gray image.
whitePixels = im == 255;
im(whitePixels) = newGrayLevel
It worked and I want some more help. The white pixels in my gray image is as a rectangle, (for example). I now want it to be filled by random gray values of range 100-150. My point is that I don't want that rectangle to be homogeneous. Please help me by providing a simple code that I can use.
Image Analyst
Image Analyst on 31 May 2014
newColorMap (gl, 1) = uint8(100 + 50*rand(1));
newColorMap (gl, 2) = uint8(100 + 50*rand(1));
newColorMap (gl, 3) = uint8(100 + 50*rand(1));
There are better ways, but that's the way using a for loop as originally asked for.

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!