How can I predefine my variable better?

2 views (last 30 days)
learningmatlab
learningmatlab on 23 May 2020
Answered: Cris LaPierre on 20 Dec 2020
measurement = regionprops(c,img, 'MeanIntensity','Centroid'); %measurements = regionprops(labeledImage, grayscaleImage, 'MeanIntensity');
measurement1=[cat(1,measurement.MeanIntensity) cat(1,measurement.Centroid)];
k=0;
corr_measurement=[0 0 0];
if isempty(measurement1)~=1
for i=1:size(measurement1,1)
k=k+1;
if measurement1(i)>20
corr_measurement(k,1:3)=measurement1(i,:) ;
end
end
end
%if isempty(corr_measurement)~=1
if corr_measurement~=[0 0 0]
stats = regionprops('table',img1,'Centroid','MajorAxisLength','MinorAxisLength','Area') %object detection
centers=corr_measurement(:,2:3);
diameters = mean([stats.MajorAxisLength stats.MinorAxisLength],2);
radii = diameters/2;
hold on
viscircles(centers,radii); %drawing around the object
hold off
num_obj=size(corr_measurement,1)%finding the number of objects
else
num_obj=0
end
I want to define the variable corr_measurement. The reason I want to predefine it is that if there are no objects in my image greater than intensity 20, it gives me an error of "undefined variable". If there are objects in the image less than intensity of 20, it defines them to be [0 0 0] and my code gives the total number of objects to be 0. But if there is a mix i.e. objects having intensity greater than 20 and objects having intensity less than 20, then the objects with intensity less than 20 get corr_measurement value of [0 0 0]; and I get a wrong readout of num_obj=0. How do I get rid of the 0 values in such mix images to get correct object counts? Attached is a screenshot of my values. Please advise.

Answers (1)

Cris LaPierre
Cris LaPierre on 20 Dec 2020
You can define the variable to be empty using empty brackets
corr_measurement = [];
Then you can have your if statement check if it is not empty
if ~isempty(corr_measurement)

Categories

Find more on Images 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!