How can i convert black backgorund to white

7 Ansichten (letzte 30 Tage)
kalaivaani
kalaivaani am 20 Dez. 2013
Kommentiert: kalaivaani am 22 Dez. 2013
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

Akzeptierte Antwort

Image Analyst
Image Analyst am 20 Dez. 2013
Bearbeitet: Image Analyst am 20 Dez. 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 Kommentare
Image Analyst
Image Analyst am 21 Dez. 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?
kalaivaani
kalaivaani am 22 Dez. 2013
thank you sir it's working i got it

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by