Thresholding based on smaller domains

Hello all
I have an image (intl16) that I'm trying to detect defcts on them. First, what I tried to do was to get the mean/median value of the whole image and define some if condition to set any i,j to1 if they are above or lower the threshold (upper and lower). However, I have some images whic have regins with either higher or lower grayscle value. I wanted to try to have some small doimns (like 10by10 for the image 5120by5120) and get the mean value for the domins and theresholding for each domin separately. Is there anyone who has some experince to let me know how I can approach this problem?
any help will be really apprecited. I cannot share the imges, sorry.

Answers (3)

You can try with any size, lets suppose you have image named as 'grayImage' (256x256)
grayImage=randi([0,255],[256,256]); %Random Image Data
% Find Mean
meanImage=mean2(grayImage)
% or Find Median
medImage=median(grayImage(:))
Define threshold, say "th"
th=150;
% Next suppose assign all those pixels greater than mean value of the image equal to zero (Black)
grayImage(grayImage>meanImage)=0;
Deals with threshold
grayImage(grayImage>th)=0;
More ways.......
Or Any logical condition as you wish to apply on image based on mean or median of the image.
Good Luck!
:)
Kalyan

2 Comments

Thanks Kalyan for your answer. What you explained nicely is what I had done. My problem is some images which have some "darker". That makes the mean/median values is not correct and the threshold wrong, which detect some "fake defect" pixels.
What I am trying to do instead is to have smaller domains, for example my image is 5120 by 5120 and I want to have 10 by 10 domains and get the mean values for them individually, then try to get defect detections for the local pixels based on the local mean value.
I hope I could explain it well enough.
I have shared the steps described in the description of the question. It would be easy to answer by looking at the images.

Sign in to comment.

Two solution:
X = randi([0,10], 12, 12); % arbitrary test data
n = 10; % Neighborhood
Y = conv2(x, ones(n, n) / (n * n), 'same');
mask = (X - Y) > Thresh;
X(mask) = Y(mask);
Or calculate the moving mean by:
Y1 = movmean(X, 10, 1);
Y = movmean(Y1, 10, 2);
% Replacing see above
This differs for the elements on the borders.
See also: medfilt2

6 Comments

Thanks a lot, Jan.
Could you please explain a little more about your first solution? or is there any examples that I could follow?
Start at:
help conv2
doc conv2
The convolution multiplies a window taken from the larger input with the matrix and calculates the sum over the elements. For ones(n,n)/(n*n) this is the average over [n x n] submatrices.
The code blurs your image. If the gray levels vary as you move around the image, a global threshold will not work. You need to either flatten your image or get a custom threshold for each pixel in the image.
Jan,
Thanks a lot. Let me word it in this way:
So we our image, defined as X. Then we do convolution as Y. Afterwards, we subtract X with Y which I see it as a normalization or flatting the images that could be better for global thresholding. I have the plot for X (image) and plot y.
Am I correct?
Yes, we can now work on the flatted image but I was looking for an adjustment for thresholding.
plot (X)
plot (Y)
Did you even try the imbinarize() with the adaptive option like I suggested below?
Thanks a lot for reminding me.
Yes I have tried this one at my first try. What I did was:
S = adaptthresh(I);
BW = imbinarize(I,T);
And then I tried this:
BW = imbinarize(I,'adaptive','ForegroundPolarity','bright','Sensitivity',0.1);
with different sensivity from 0.1 to 0.5.
for both ways, I found by far less defect I had expected. Even though, "adaptthresh" looked better detections.

Sign in to comment.

imbinarize() has an 'adaptive' option try that. Otherwise try to use adapthisteq() to flatten the image so that you can use a global threshold. Attach a similar image (non-secret, non-proprietary) if you need more help.

1 Comment

Hello
I have a question regarding useing imbinarize "(Image, 'adaptive') ".
For defects which are bright I used 'ForegroundPolarity','bright' (or nothing as the defult is the same) along with sensivity 0.1. I collected some data. However, when I used the same argument with 'dark' I had to use 0.5 sensivity. Why is that so?
The code I am using is:
BW = imbinarize(image,'adaptive','ForegroundPolarity','dark','Sensitivity',0.5);
CC = bwconncomp(BW, 8);
numPixels = cellfun(@numel,CC.PixelIdxList);
[biggest,idx] = max(numPixels)
CC.NumObjects;
Stats_1 = regionprops(BW,'Centroid'); % this should give me the number of defects
% then I wanted to map the defcts on the image and I try this:
centroids = cat(1,Stats_1.Centroid);
imshow(image);
hold on;
plot(centroids(:,1),centroids(:,2),'bo');
hold off
When I imshowpair my image with BW, I can see the dark defects but I do not know why the "regionprops with 'Centroid'" cannot detect them and map them afterwards properly.
Sorry if it is too long and thanks in advance for your help.

Sign in to comment.

Categories

Asked:

on 13 Feb 2021

Commented:

on 17 Feb 2021

Community Treasure Hunt

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

Start Hunting!