Purple colour extraction only
13 ビュー (過去 30 日間)
古いコメントを表示
Ramdhan Zakwan Roslan
2021 年 1 月 21 日
コメント済み: Ramdhan Zakwan Roslan
2021 年 1 月 23 日
Hi, im stuggling as how to extract only the purple colour from this image.
the result that i manage to obtain
as you can see there a thin layer of the red circle and the background turn white.
How to obtain only the purple circle and make the background still white. Thank you.
My code
>> a=imread('circles.jpg');
>> redChannel=a(:,:,1);
>> greenChannel=a(:,:,2);
>> blueChannel=a(:,:,3);
>> black=zeros(size(a,1), size(a,2));
>> mask=redChannel>200; %the mask
>> imshow(mask);
>> purpleOnly=bsxfun(@times, a, cast(~mask, 'like', a)); %extract the purple image only
>> imshow(purpleOnly)
0 件のコメント
採用された回答
Image Analyst
2021 年 1 月 21 日
Try this:
mask = imdilate(redChannel>200, true(5)); %the mask
6 件のコメント
Image Analyst
2021 年 1 月 23 日
Count what? Number of purple spots, the total of all purple pixels? The area of each purple spot?
Try this. I compute all three:
props = regionprops(mask, 'Area');
individualAreas = [props.Area]
totalArea = sum(individualAreas)
numPurpleSpots = numel(props)
その他の回答 (1 件)
Image Analyst
2021 年 1 月 23 日
Ramdhan:
OK, here is a full demo:
% Demo by Image Analyst.
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. This is a horrible image. NEVER use JPG format for image analysis. Use PNG, TIFF, or BMP instead.
folder = [];
baseFileName = 'purple spots.jpg';
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.
subplot(3, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
hp = impixelinfo(); % Set up status line to see values when you mouse over the 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';
% Create a binary image of the purple spots using code generated from the Color Thresholder app.
[binaryImage, maskedRGBImage] = createMask(rgbImage);
% Display the images.
subplot(3, 2, 3);
imshow(binaryImage, []);
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;
% Show the masked RGB image.
subplot(3, 2, 4);
imshow(maskedRGBImage, []);
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
axis('on', 'image');
title('Initial Masked Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Get the areas of the blobs from the initial mask image.
props = regionprops(binaryImage, 'Area');
allAreas = sort([props.Area]) % There are a bunch of areas less than 30 and 5 areas greater than 4199.
% Filter out small blobs with area less than 500 or so with bwareaopen():
binaryImage = bwareaopen(binaryImage, 500);
% Display final mask.
subplot(3, 2, 5);
imshow(binaryImage, []);
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;
% Show the final masked image.
% Mask the image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
maskedRGBImage = bsxfun(@times, rgbImage, cast(binaryImage, 'like', rgbImage));
subplot(3, 2, 6);
imshow(maskedRGBImage, []);
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
axis('on', 'image');
title('Final Masked Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Get the areas of the blobs from the final mask image.
props = regionprops(binaryImage, 'Area');
% Get a bunch of measurements.
individualAreas = [props.Area]
totalArea = sum(individualAreas)
numPurpleSpots = numel(props)
% Show bar chart of areas.
subplot(3, 2, 2);
bar(individualAreas);
grid on;
title('Circle Areas', 'FontSize', fontSize);
xlabel('Circle Index', 'FontSize', fontSize);
ylabel('Area in Pixels', 'FontSize', fontSize);
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 23-Jan-2021
%------------------------------------------------------
% Convert RGB image to chosen color space
I = RGB;
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.000;
channel1Max = 240.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 82.000;
channel2Max = 228.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 219.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
You will see:
individualAreas =
4203 4199 4199 16806 67294
totalArea =
96701
numPurpleSpots =
5
参考
カテゴリ
Help Center および File Exchange で Image Processing and Computer Vision についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!