MATLAB: Isolating specific dots in an image.

image analysisimage processingimage segmentationMATLAB

Hello, I'm trying to get my image to the point where only the dots inside the black squares remain.
So far, all I've done with this code is use inmextendedmin on the original grayscale image, and then masked it ontop of the grayscale image to produce this result.
figure(1);
imshow(grayImage);
mask = imextendedmin(grayImage,2);
figure(2);
imshow(mask);
figure(3);
imshowpair(grayImage,mask,'blend');
Before, I had been trying to use watershedding, dividing the background, and a lot of other neat tricks to try and single out the black dots, but it seems that this simple bit of code alone gets me half way to what I want (As each black square has a dot already in it, all i want are those dots). Is there a way to get rid of everything else in this image besides the gray dots inside the black dots?
The problem I often had with watershedding and whatnot is I needed a variable threshold, as well as watershedding was rather time and processes intensive, so I'm trying to find more efficient ways of doing it.

Best Answer

im=imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/122320/2steps%20from%20perfection.PNG');
im=max(im,[],3);
bw_bkg=imfill(im<=50,'holes');
bw=im>50 & bw_bkg;
figure, imshow(bw)