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!