Azzera filtri
Azzera filtri

How to perform OTSU thresholding while retaining the pixel information near the image

3 visualizzazioni (ultimi 30 giorni)
The mistake with my code is when I calculate the centroid -
The matrix I are centroiding: bin_im = im2bw(B, temp) returns a binary image. This is an image where values are either 0 or 1, which is discarding all the information about which pixels are brighter than others near the peak....
How can I still use the graythresh method for OTSU thresholding, and still retain the information about the pixels that are brighter than others near the peak of the bead?? I.E. not use a binary image for the centroid calculation
a sample dataset is included with 3 images, and my code is below:
clc;
clear;
close all;
load('my_mat_file.mat');
%% Center Container
center=zeros(2,size(ims,3));
%% Image loop
for x=1:3
A=ims(:,:,x);
%% Size of Matrix A
[r,c]=size(A);
%% Otsu thresholding
B=im2gray(A);
temp=graythresh(B);
bin_im=im2bw(B,temp);
%% centroid calculation
tot_mass = sum(bin_im(:));
[ii,jj] = ndgrid(1:size(bin_im,1),1:size(bin_im,2));
R = sum(ii(:).*bin_im(:))/tot_mass;
C = sum(jj(:).*bin_im(:))/tot_mass;
center(:,x) = [C;R];
end

Risposta accettata

Image Analyst
Image Analyst il 22 Apr 2022
You can use the gray scale image to determine the weighted centroid -- the centroid of the binary blobs but weighted by the brightness of the pixels in the gray scale image.
props = regionprops(bin_im, B, 'WeightedCentroid', 'Centroid');
% Extract from structure into matrices
xy = vertcat(props.Centroid); % Centroid of binary blobs.
xyWeighted = vertcat(props.WeightedCentroid); % Centroid of binary blobs but weeighted by gray level.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by