Sphere-based color slicing of image

10 次查看(过去 30 天)
Sphere based color slicing

采纳的回答

Image Analyst
Image Analyst 2020-5-26
Sucharita: Here, I've done 95% of it for you. If I do much more, then you'd just be turning in my answer as your own and I don't think your course rules allow that. You just need to replace the line that says "% code" with your own code and then display the output image after the loop is done.
% Demo to find color distances in RGB color space. By Image Analyst, May 26, 2020.
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);
a1 = [134, 51, 143]
a2 = [131, 132, 4]
R0 = 30
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = pwd;
baseFileName = 'image.jpeg';
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
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Display the test image full size.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Reference 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';
[rows, columns, numberOfColorChannels] = size(rgbImage);
outputImage = zeros(rows, columns, numberOfColorChannels, class(rgbImage));
distanceImage = zeros(rows, columns);
for col = 1 : columns
for row = 1 : rows
thisR = double(rgbImage(row, col, 1));
thisG = double(rgbImage(row, col, 2));
thisB = double(rgbImage(row, col, 3));
distanceImage(row, col) = sqrt((thisR - a1(1))^2 + (thisG - a1(2))^2 + (thisB - a1(3))^2); % or a2 for case ii
if distanceImage(row, col) < R0
% code...
end
end
end
maxDistance = max(distanceImage(:))
% Display the distance image.
subplot(2, 2, 2);
imshow(distanceImage, []);
axis('on', 'image');
caption = sprintf('Distance Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
colorbar;
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Display the histogram of the distance image.
subplot(2, 2, 3);
histogram(distanceImage, 256);
grid on;
title('Histogram of Color Distances', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Gray Level', 'FontSize', fontSize, 'Interpreter', 'None');
ylabel('Count', 'FontSize', fontSize, 'Interpreter', 'None');
fprintf('Done running %s.m ...\n', mfilename);

更多回答(1 个)

Image Analyst
Image Analyst 2020-5-25
编辑:Image Analyst 2020-5-26
Just scan the image pixel by pixel getting the RGB color. Then compute the color distance and if it's less than R0, copy that color to an output image. Here's a start:
[rows, columns, numberOfColorChannels] = size(rgbImage);
outputImage = zeros(rows, columns, numberOfColorChannels, class(rgbImage));
for col = 1 : columns
for row = 1 : rows
thisR = double(rgbImage(row, col, 1));
thisG = double(rgbImage(row, col, 2));
thisB = double(rgbImage(row, col, 3));
distance = sqrt((thisR - a1(1))^2 + .........) % or a2 for case ii
if distance < R0
% code...
end
end
end

Community Treasure Hunt

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

Start Hunting!

Translated by