How can i convert black backgorund to white
8 views (last 30 days)
Show older comments
i want to eliminate or convert the black background to white except the centre image that i needed.this
<<
>>
2 images i had attached. the black intensity ranges as 0:45
0 Comments
Accepted Answer
Image Analyst
on 20 Dec 2013
Edited: Image Analyst
on 20 Dec 2013
Try this:
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
close all;
clear all;
format long g;
format compact;
fontSize = 20;
% Read in a color demo image.
folder = 'C:\Users\kalaivaani\Documents\Temporary';
baseFileName = 'bgrnd.JPG';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage);
axis on;
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% If background is less than 45, say it's background.
% Adjust the value as needed to achieve what you want.
thresholdValue = 45;
mask = redChannel <= thresholdValue & greenChannel <= thresholdValue & blueChannel <= thresholdValue;
% Display the mask image.
subplot(2, 2, 2);
imshow(mask);
title('Mask Image', 'FontSize', fontSize);
% Initialize the masked channels.
maskedRed = redChannel;
maskedGreen = greenChannel;
maskedBlue = blueChannel;
% Do the masking - make white where it was black.
maskedRed(mask) = 255;
maskedGreen(mask) = 255;
maskedBlue(mask) = 255;
% Recombine separate masked color channels into a single, true color RGB image.
maskedRgbImage = cat(3, maskedRed, maskedGreen, maskedBlue);
% Display the masked color image.
subplot(2, 2, 3);
imshow(maskedRgbImage);
title('Masked Color Image', 'FontSize', fontSize);
2 Comments
Image Analyst
on 21 Dec 2013
kalaivaani - what's going on? Did you ever check back to see if anyone took the effort to answer your question and help you?
More Answers (0)
See Also
Categories
Find more on Image Segmentation and Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!