I have a few paddy in the image. I need to know the RGB of each paddy but how should i study the paddy one by one?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi, I have an image that containing a few paddy and I will like to know the RGB of each paddy. I have tried using image tool to crop the image but it is not so accurate. I hope I can have some suggestions on how to study each paddy one by one. Below is one example of my image.
0 commentaires
Réponses (1)
Image Analyst
le 23 Mai 2020
See my Image Segmentation Tutorial. It will walk you through the process.
2 commentaires
Image Analyst
le 2 Juin 2020
You simply had to do it for each color channel one at a time. Get them with imsplit(). Here's full demo for you:
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 = 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 RGB image full size.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Original 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';
% Do color segmentation to get the mask.
[BW, maskedRGBImage] = createMask(rgbImage);
% Get rid of blobs less than 100 in size.
BW = bwareaopen(BW, 100);
% Fill blobs in case they have any holes in them.
BW = imfill(BW, 'holes');
% Display the binary image.
subplot(2, 2, 2);
imshow(BW, []);
axis('on', 'image');
caption = sprintf('Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Display the RGB image full size.
subplot(2, 2, 3);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Original 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.
% Extract the individual red, green, and blue color channels using imsplit() (introduced in R2018b).
[redChannel, greenChannel, blueChannel] = imsplit(rgbImage);
propsR = regionprops(BW, redChannel, 'Area', 'MeanIntensity', 'Centroid');
propsG = regionprops(BW, greenChannel, 'Area', 'MeanIntensity', 'Centroid');
propsB = regionprops(BW, blueChannel, 'Area', 'MeanIntensity', 'Centroid');
% Get areas and centroids. These don't depend on color channel.
allAreas = [propsR.Area]
xy = vertcat(propsR.Centroid)
xCenters = xy(:, 1)
yCenters = xy(:, 2)
hold on;
% Plot RGB
for k = 1 : length(propsR)
meanR(k) = propsR(k).MeanIntensity;
meanG(k) = propsG(k).MeanIntensity;
meanB(k) = propsB(k).MeanIntensity;
fprintf('Blob #%d = (%f, %f, %f)\n', k, meanR(k), meanG(k), meanB(k));
caption = sprintf(' Blob #%d', k);
text(xCenters(k), yCenters(k), caption, 'FontSize', 11, 'FontWeight', 'bold', 'Color', 'b');
plot(xCenters(k), yCenters(k), 'r.', 'MarkerSize', 40);
end
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-Jun-2020
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.000;
channel1Max = 1.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.185;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 0.703;
% 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
In the command window you'll see:
Blob #1 = (125.350792, 106.394481, 59.287941)
Blob #2 = (125.411024, 107.974141, 58.699728)
Blob #3 = (117.872516, 98.680761, 54.429175)
Blob #4 = (124.067159, 104.392433, 54.835460)
Blob #5 = (126.159124, 107.330234, 58.970448)
Blob #6 = (121.696403, 102.050360, 56.588969)
Blob #7 = (121.568789, 101.571832, 50.820256)
Blob #8 = (117.276356, 97.147390, 54.666325)
Voir également
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!