Effacer les filtres
Effacer les filtres

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

3 vues (au cours des 30 derniers jours)
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
  1 commentaire
Gucci
Gucci le 22 Avr 2022
@Image Analyst is there a way to do this, so that the pixel value is retained on the object of interest?

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 22 Avr 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.
  1 commentaire
Gucci
Gucci le 28 Avr 2022
Modifié(e) : Gucci le 28 Avr 2022
Thank you this works
Also how can I display the gray level image in a figure?

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by