How can i convert black backgorund to white
8 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Image Analyst
2013-12-20
编辑:Image Analyst
2013-12-20
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 个评论
Image Analyst
2013-12-21
kalaivaani - what's going on? Did you ever check back to see if anyone took the effort to answer your question and help you?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Segmentation and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!