Why this regionprops returns 0x1 struct?

I am trying to convert the code of MIT's course Biological Instrumentation and Measurement in the wiki page here from Matlab 7.3 to Matlab R2016a. My input data's features are in the scale of square L2 norm. Gaussian kernel should be using the square L2 norm, but I see significant differences between Gaussian functions in Matlab and Octave, see the answer here. The code is about Estimating resolution from a PSF slide image and about the function EstimateResolutionFromPsfImage with default values of tolarance [0.7 1.3] in Matlab 2016a

resolutionEstimate = EstimateResolutionFromPsfImage(im2bw(imgRGB), [0.7 1.3]); 

where imgRGB is

enter image description here

Output results in complications coming from the fact that the command returns 0x1 struct in objectProperties from the line 29 of EstimateResolutionFromPsfImage

objectProperties = regionprops( dilatedImage, ImageData, ...
    'Area', 'Centroid', 'PixelList', 'PixelValues', 'MaxIntensity' );

where I do not understand how it can be zero in the case. My workspace is ok at start but after passing the objecProperties statement, there is a 0x1 struct which should not be there as almost empty

enter image description here

and eventually leads to errors

Index exceeds matrix dimensions.

Error in EstimateResolutionFromPsfImage (line 108)
    Resolution = mean( allPeakData(:,4) ) ./ 0.336;

Error in masi (line 272)
resolutionEstimate = EstimateResolutionFromPsfImage(im2bw(imgRGB), [0.7 1.3]);

Possible reasons

Psf image generation based on dhanushka's hint

MIT's code SimulatePsfSlide seems to work somehow in generating psfImg. I am reviewing the effect of different parameters on the result and confirming the result but have some difficulties with overexposed images and and eventually having warnings like

Warning: Rank deficient, rank = 4, tol =  1.979466e-12. 
> In nlinfit>LMfit (line 579)
  In nlinfit (line 276)
...
Warning: Rank deficient, rank = 1, tol =  1.979466e-12. 
Warning: Some columns of the Jacobian are effectively zero at the solution,
indicating that the model is insensitive to some of its parameters.  That may be
because those parameters are not present in the model, or otherwise do not
affect the predicted values.  It may also be due to numerical underflow in the
model function, which can sometimes be avoided by choosing better initial
parameter values, or by rescaling or recentering.  Parameter estimates may be
unreliable. 

Here some line which causes complications where I do not understand why 16 and why conversion with im2double

simulatedPsfImage = im2double( simulatedPsfImage * 16 );

dhanushka's code in Matlab 2016a

Matlab does not work with inputs of homogenous changes when the image is not PSF, like the first image.

Dhanushka's filter is not recommended here because of many reasons. I get a much better model with significantly greater tolerance (even 1.00) with the code. Replace dhanushka's not recommended filter by a better one and use this

im = im2double( imgGray ); % zeros( ImageSize ) );
sigma = 5;
simulatedPsfImage = imgaussfilt(im, sigma); 
simulatedPsfImage = im2double( simulatedPsfImage );
[ measuredResolution, standardError, bestFitData ] = ...
    EstimateResolutionFromPsfImage( simulatedPsfImage, [1.00 1.00] ); 

Output is much better

enter image description here

where BestFitData = 249.999999999989 249.999999999989 0.00713504020178639 5.31607106546232 -0.000392450463696062; so estimatedSigma = 5.316, which is worser than dhanushka's second example. You will have significant problems with tolerance levels with the fspecial gaussian in Matlab, but not in Octave like dhanushka's second example shows.

Extension to Mars

Mars case with dhanushka's code when input

enter image description here

Output with imgaussfilt

enter image description here

Final conclusion

L2 norm is not sufficient here.


Why is the output of regionprops here 0x1 struct?

Upvotes: 1

Views: 1072

Answers (1)

dhanushka
dhanushka

Reputation: 10682

To my understanding your input image isn't a PSF image. Quoting from the link you provided, a PSF image is an image of approximate point sources on a dark background, such as a star field or sub resolution fluorescent microspheres. You can generate such image for testing using the SimulatePsfSlide function in the given code.

EDIT

I don't have Matlab. I ran the code in Octave with a simple PSF image having a single point source in the middle of the image generated from the code below. You can first try with a simple known image and check the result.

In the code below, you can vary the Gaussian PSF size and sigma and see how nlinfit estimates the sigma.

Over-exposure shouldn't be a problem, those values are clipped according to the test code in the link.

 clear all
 close all

 psfSize = 9;
 psfSigma = 5;

 % single point source in the middle: this is the object
 ImageSize = [500 500];
 im = im2double( zeros( ImageSize ) );
 im( int32(ImageSize(1)/2), int32(ImageSize(2)/2) ) = 1;
 % gaussian psf: this is the psf of our imaging system
 h = fspecial('gaussian', [psfSize psfSize], psfSigma);
 % convolve the object with psf: the image, this is what we see
 simulatedPsfImage = imfilter(im, h, 'same');
 simulatedPsfImage = im2double( simulatedPsfImage );
 % estimating resolution
 [ measuredResolution, standardError, bestFitData ] = ...
        EstimateResolutionFromPsfImage( simulatedPsfImage );

Input data and nlinfit output (beta and MSE only): Note that in the second case, the MSE is smaller, indicating that the input data closely matches the model. Also we get the correct sigma.

psfSize = 9, sigma = 5, estimated sigma = 3.0730

beta = 
2.5000e+002
2.5000e+002
2.0275e-002
3.0730e+000
-4.4688e-004

mse =   1.6114e-006

size9

psfSize = 25, sigma = 5, estimated sigma = 5.0000

beta = 
2.5000e+002
2.5000e+002
6.5254e-003
5.0000e+000
7.3796e-010

mse =   2.2996e-020

size25

Output in Matlab 2016a with psfSize=9 and psfSigma=5, which shows a significant difference between fspecial in Matlab and Octave

enter image description here

where bestFitData = 250.000000000593 250.000000000593 0.0202577533025840 3.07726724108174 -0.000451857701021258; here estimatedSigma = 3.077.

Upvotes: 3

Related Questions