How can I predefine my variable better?

조회 수: 1 (최근 30일)
learningmatlab
learningmatlab 2020년 5월 23일
답변: Cris LaPierre 2020년 12월 20일
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.

답변 (1개)

Cris LaPierre
Cris LaPierre 2020년 12월 20일
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)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by