Otsu Thresholding using graythresh
조회 수: 11 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
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
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 Center 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!