How can to convert Color Image to grey scale image but not changing color of blue and orange loops in bowl?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Zain ul abideen ahsan
am 2 Dez. 2020
Bearbeitet: Image Analyst
am 2 Dez. 2020
Hi There, I need to convert all the image to grey scale but also need the red and orange loops unchanged. I am trying an appraoch filtering hue but the output result is not that satisfying. looking for @Image Analyst guidence becuase he is champ and I am his fan
This course was forcefully assigned to us as corona lockdown happened without prior knowledge now I am stuck in these assignemnt
Thanks for help in advance
Here is my code,
I = imread('Cereal_noise.png');
I = im2double(I);
I_m = colorfilter(I,[150 20]);
imshow(I_m,[]);
function I = colorfilter(image, range)
% RGB to HSV conversion
I = rgb2hsv(image);
% Normalization range between 0 and 1
range = range./360;
% Mask creation
if(size(range,1) > 1), error('Error. Range matriz has too many rows.'); end
if(size(range,2) > 2), error('Error. Range matriz has too many columns.'); end
if(range(1) > range(2))
% Red hue case
mask = (I(:,:,1)>range(1) & (I(:,:,1)<=1)) + (I(:,:,1)<range(2) & (I(:,:,1)>=0));
else
% Regular case
mask = (I(:,:,1)>range(1)) & (I(:,:,1)<range(2));
end
% Saturation is modified according to the mask
I(:,:,2) = mask .* I(:,:,2);
% HSV to RGB conversion
I = hsv2rgb(I);
end
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 2 Dez. 2020
Bearbeitet: Image Analyst
am 2 Dez. 2020
Here is how I'd do it:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = [];
baseFileName = 'cereal1.png';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
% It's not an RGB image! It's an indexed image, so read in the indexed image...
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Create a gray scale image
grayImage = rgb2gray(rgbImage);
% Display the test image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
hFig1.Name = 'Demo by Image Analyst';
% Find a mask of just the white pixels.
fprintf('Masking image...\n');
[mask, maskedRGBImage] = createMask(rgbImage);
% Display the mask image.
subplot(2, 2, 2);
imshow(mask, []);
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
axis('on', 'image');
title('Initial Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Clean up the mask a bit by doing a morphological closing.
fprintf('Doing a morphological closing on the mask image...\n');
se = strel('disk', 2, 0);
mask = imclose(mask, se);
% Display the final mask image.
subplot(2, 2, 3);
imshow(mask, []);
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
axis('on', 'image');
title('Final Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
%-----------------------------------------------------------------------------------
% Make the mask p[ixels gray.
fprintf('Making non red/orange grayscale in the RGB image...\n');
% Get the individual RGB channels
[redChannel, greenChannel, blueChannel] = imsplit(rgbImage);
% Make the image grayscale in the mask regions
% Invert the mask to get non red/orange regions.
mask = ~mask;
redChannel(mask) = grayImage(mask);
greenChannel(mask) = grayImage(mask);
blueChannel(mask) = grayImage(mask);
% Recombine separate color channels into a single, true color RGB image.
filteredRGBImage = cat(3, redChannel, greenChannel, blueChannel);
% Display the initial mask image.
subplot(2, 2, 4);
imshow(filteredRGBImage, []);
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
axis('on', 'image');
title('Non-red/orange set to gray scale.', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
fprintf('Done running %s.m ...\n', mfilename);
msgbox('Done!');
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 02-Dec-2020
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.908;
channel1Max = 0.113;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.143;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.425;
channel3Max = 1.000;
% Create mask based on chosen histogram thresholds
sliderBW = ( (I(:,:,1) >= channel1Min) | (I(:,:,1) <= channel1Max) ) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
Here is the Color Thresholder, which is what I used to set the thresholds:
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Image Segmentation and Analysis finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!