# How to have different threshold for different images?

29 views (last 30 days)
Amlan basu on 7 Dec 2022
Commented: Amlan basu on 7 Dec 2022
I am doing edge detection of microcell images directly taken form microscope. One image has 25 microwells containing the cells. I am cropping the 25 automatically and trying to do edge detection. The code is working. However, because of the threshold the edge detection (outilne formed on the cell spheroid) is not perfect, missing the actual outer edge of the spehroid.
Radius of diamond/ length of line does make a difference but Threshold is the main factor.
It will be very good if anyone can guide me on how to have variable threshold that suits different images for proper edge detection.
Thanks

Chetan Bhavsar on 7 Dec 2022
In general, the threshold used in edge detection algorithms plays a critical role in determining the quality of the detected edges. A high threshold will result in only the strongest edges being detected, while a low threshold will detect more edges but may also include more noise and false edges.
In your specific case, it sounds like the threshold you are using is not providing good results. One way to address this issue would be to try using a different thresholding method, such as Otsu's method, which can automatically determine an appropriate threshold based on the image intensity histogram.
In MATLAB, you can use the graythresh function with the 'Otsu' method to apply Otsu's thresholding to an image. Here is an example of how you can use this function to determine an appropriate threshold for edge detection:
% Use Otsu's method to determine the threshold
threshold = graythresh(I, 'Otsu');
% Use the threshold to detect edges in the image
edges = edge(I, 'Canny', threshold);
Alternatively, you can try using the adaptivethreshold function to apply adaptive thresholding to the image, which can be more effective at detecting edges in images with varying contrast. Here is an example of how you can use this function to perform adaptive thresholding on an image:
% Use adaptive thresholding to detect edges in the image
Using either of these approaches should provide better results than the thresholding method you are currently using, and may help to improve the quality of the edge detection in your microcell images.
Amlan basu on 7 Dec 2022
Here is the code that I am using to detect or make an enclosure around the spheroids. Please if you could let me know how to insert the commands mentioned by you in this code.
image = microwells{b};
[~,threshold] = edge(image,'Sobel');
fudge = 15;
BW = imcomplement(BWO);
% seD = strel('diamond',11);
r = 9;
se90 = strel('line',r,90);
se0 = strel('line',r,0);
BW3 = imdilate(BW,[se90 se0]);
%BW3 = imdilate(BW,seD);
BW4 = imfill(BW3,'holes');
BW5 = imerode(BW4,ones(17));
BW6 = imdilate(BW5,ones(17));
% BWimage{b} = BW6; % create outline image
BWoutline = bwperim(BW6);
array{b} = imoverlay(image,BWoutline,'red');
bound = []; boundary = []; intensity = [];
[bound,I,~,~] = bwboundaries(BW6,'noholes');
figure,imshow(image)
bound_S = rem_edge(I);
hold on
if ~isempty(bound_S)
for k=1:length(bound_S)
boundary = bound{bound_S(k)};
plot(boundary(:,2), boundary(:,1),'r-', 'LineWidth', 1) % plot boundaries
param(b,1:5) = blob_parameters(boundary(:,2), boundary(:,1));
labeledImage = logical(BW6);
intensity = regionprops(labeledImage, image, 'MeanIntensity');
param(b,6) = intensity.MeanIntensity;
end
else
param(b,:) = [NaN NaN NaN NaN NaN NaN];
end
hold off

Image Analyst on 7 Dec 2022
You can use the interactive thresholding utility in my File Exchange:
It may be possible to have the threshold automatically determined. I don't know because you forgot to attach any images.
One major obstacle to using a global threshold is that the images have not been background corrected. All lenses have shading so your image will be darker at the corners and edges than at the middle. Unless you correct for that, you won't have a good binary image.
And you may or may not need edge detection. Beginners often do it unnecessarily. Just because you can see edges in the image does not mean edge detection is a necessary step in the segmentation algorithm. Again, I won't know until I see your pictures.
I'm attaching a few demos that may interest you.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
Amlan basu on 7 Dec 2022
In figure 1 and 3 you will see the outlines are forming perfectly. It is just the line length that needs reduction.
However, in figure 2 you will be able to see the problem which I have been able to find out that it is mainly because of the threshold. This is happening with many images. This is just an example.

### Categories

Find more on Images in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!