Otsu Thresholding using graythresh

11 次查看(过去 30 天)
Gucci
Gucci 2022-2-24
Hello, I have a mat file of roughly 1000 images, uint16 format, which I convert to double format to then threshold each image and subsequently find the center of mass of each image.
Currently, I have to manually look at some images and determine a suitable Threshold value myself to then input into the loop and determine the center of mass, however I need to find a way to perform an automated way of obtaining this 'Th' value, for example using the function 'graythresh' as seen commented below following the file load statement.
When I use this, however it always returns a Th value of 0 with no errors present. Please help in how to properly implement 'graythresh'
clc;
clear;
close all;
load('C:\Users\Mitre\Desktop\trappedbeaddata\new_data\2022_02_17\30pc_laser.mat');
%% Global image threshold using Otsu's method (graythresh)
Th = graythresh(ims);
ImsThresholded = double(ims).*(ims>(Th*max(ims,[],'all')));
%% Simple threshold input
% Th=input('Threshold limit: ');
%% Center Container
center=zeros(2,size(ims,3)); % zeros array
%% Image loop
for x=1:1000
A=ims(:,:,x); % suppress output with semicolon
AA = double(A); % Convert to double
%% Size of Matrix A
[r,c]=size(AA);
%% Compare each element of matrix with threshold value
for i=1:r
for j=1:c
if AA(i,j)<Th
AA(i,j)=0; % suppress output with semicolon
else
% AA(i,j)>Th; % Remove for textured center
% AA(i,j)=1; % Remove for textured center % suppress output with semicolon
AA(i,j)=AA(i,j)-Th; % Value - Threshold value = More complex threshold
end
end
end
%% Center of mass method 1: using mean
% https://uk.mathworks.com/matlabcentral/answers/363181-center-of-mass-and-total-mass-of-a-matrix
tot_mass = sum(AA(:));
[ii,jj] = ndgrid(1:size(AA,1),1:size(AA,2));
R = sum(ii(:).*AA(:))/tot_mass;
C = sum(jj(:).*AA(:))/tot_mass;
%% display / store values of x, (C,R) and FrameTime
% display 'x'
%disp("x=" + num2str(x));
% Columns = x-axis // Rows = y-axis
center(:,x) = [C;R];
end

回答(1 个)

Image Analyst
Image Analyst 2022-2-24
How many blobs do you expect in the image?
You don't need to convert to double before thresholding.
To compute the center of mass for each blob you can do
props = regionprops(ImsThresholded, ims, 'WeightedCentroid');
  12 个评论
Gucci
Gucci 2022-3-19
hi @Image Analyst, Im still a bit confused, been trying this graythresh method, but getting no progress
Image Analyst
Image Analyst 2022-3-20
You can mask your image to erase/blacken pixels below the threshold then if you don't want to use regionprops() you can do it manually like this:
grayImage = imread('moon.tif');
[rows, columns, numberOfColorChannels] = size(grayImage);
imshow(grayImage);
axis('on', 'image');
sumgx = 0;
sumgy = 0;
sumg = 0;
sumg = sum(grayImage(:))
for col = 1 : columns
for row = 1 : rows
gl = double(grayImage(row, col));
sumgx = sumgx + col * gl;
sumgy = sumgy + row * gl;
end
end
xCOG = sumgx / sumg
yCOG = sumgy / sumg
hold on
xline(xCOG, 'LineWidth', 2, 'Color','r');
yline(yCOG, 'LineWidth', 2, 'Color','r');
caption = sprintf('Center of Gravity: (%.2f, %.2f)', xCOG, yCOG);
title(caption, 'FontSize', 18)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by