Want to find the mean dispersion of the white particles in the image

2 views (last 30 days)
I have been trying to calculate the mean dispersion of the white particles from an image. The code calculates the mean dispersion but somehow it gives the wrong axis length. I am attaching the code, the results got and the results expected. I have also tried caliberating the image.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'bump.tif';
folder = pwd;
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
%=======================================================================================
% Read in demo image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Crop off screenshot stuff
% rgbImage = rgbImage(132:938, 352:1566, :);
% Display image.
subplot(2, 2, 1);
imshow(rgbImage, []);
impixelinfo;
axis on;
caption = sprintf('Original Color Image\n%s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% 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.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
% Take one of the color channels, whichever one seems to have more contrast..
grayImage = rgb2gray(rgbImage);
% Display the color segmentation mask image.
subplot(2, 2, 2);
imshow(grayImage, []);
title('Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
axis('on', 'image');
drawnow;
% Show the histogram
subplot(2, 2, 3);
imhist(grayImage);
grid on;
caption = sprintf('Histogram of Gray Scale Image')
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Gray Level', 'FontSize', fontSize)
ylabel('Count', 'FontSize', fontSize)
%=======================================================================================
% Threshold the image to get the disk.
lowThreshold = 205;
highThreshold = 255;
% Use interactive File Exchange utility by Image Analyst to to the interactive thresholding.
% https://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image?s_tid=srchtitle
% [lowThreshold, highThreshold] = threshold(114, 255, grayImage);
mask = grayImage >= lowThreshold & grayImage <= highThreshold;
xline(lowThreshold, 'Color', 'r', 'LineWidth',2);
xline(highThreshold, 'Color', 'r', 'LineWidth',2);
% Now do clean up by hole filling, and getting rid of small blobs.
%mask = bwareafilt(mask, 1); % Take largest blob only.
% Fill the mask from the top most pixel downwards in the column.
% for col = 1 : columns
% topRow = find(mask(:, col), 1, 'first');
% if ~isempty(topRow)
% mask(topRow:end, col) = true;
% end
% end
% Display the color segmentation mask image.
subplot(2, 2, 4);
imshow(mask, []);
title('Final Mask', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
axis('on', 'image');
drawnow;
% [y1,x1]=find(mask==1);
% miny1=min(y1);
% maxy1=max(y1);
% scanint=1;
% save_mat=[];
% currentvaly=min(y1)
% for i1=1:(maxy1-miny1+1)
% yloc=find(y1==currentvaly)
% ynow=currentvaly;
% xnow=zeros(length(yloc),1);
% for j1=1:length(yloc)
% xnow(j1)=x1(yloc(j1));
% end
% summ=0;
% xavg=mean(xnow);
% for j1=1:length(yloc)
% summ=(xavg-xnow(j1))^2;
% end
% Dispersion1=sqrt((1/length(yloc))*summ);
% savedata=[ynow,Dispersion1];
% save_mat=[save_mat; savedata];
%
%
%
% currentvaly=currentvaly+1;
% end
[y1,x1]=find(mask==1);
minx1=min(x1);
maxx1=max(x1);
scanint=1;
save_mat=[];
currentvalx=min(x1)
for i1=1:(maxx1-minx1+1)
xloc=find(x1==currentvalx)
xnow=currentvalx;
ynow=zeros(length(xloc),1);
for j1=1:length(xloc)
ynow(j1)=y1(xloc(j1));
end
summ=0;
yavg=mean(ynow);
for j1=1:length(xloc)
summ=(yavg-ynow(j1))^2;
end
Dispersion1=sqrt((1/length(xloc))*summ);
savedata=[xnow,Dispersion1];
save_mat=[save_mat; savedata];
currentvalx=currentvalx+1;
end
figure;
plot(save_mat(:,1),save_mat(:,2),'k.')
uiwait(helpdlg('Done!'));
figure;
plot(y1,x1,'k.')

Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!