How do I remove the background (specifically the vignette) from this image without removing the particles of sand?

4 views (last 30 days)
I am doing research where my goal is to remove out of focus particles of sand, and count in focus particles of sand that I capture in lab. I am currently trying to isolate the objects using imgradient but the vignette hinders the ability to distinguish a gradient. I would like to remove the backround vignette as it disrupts the ability to count the particles. I would appreciate any help!
Here's my code (readPGRaw is code we wrote in order to read in our raw images):
filename= '/Users/sammybuckets/Documents/SandTests/S5_2023-07-18-193451-0000.raw';
ubit= 8;
size= [2448 2048 1];
I = readPGRaw(filename,ubit,size);
figure
G= imgradient(I);
J= G>110;
imagesc(J)
axis image
colormap gray
[JV, JU]= find(J);
hold on
plot(JU,JV,'.')
se = strel('disk',15);
closeJ = imclose(J,se);
[JVC, JUC]= find(closeJ);
plot(JUC,JVC,'.')
plot(JU,JV,'.')
BW2 = imfill(closeJ,'holes');
figure
imshow(BW2)
title('Filled Image')

Accepted Answer

Image Analyst
Image Analyst on 25 Jul 2023
Your best bet is to take a separate, blank shot with no particles in the field of view and then divide your particle images by that blank image. Then you can just use a simple fixed global threshold. Attached is a demo.

More Answers (1)

Angelo Yeo
Angelo Yeo on 25 Jul 2023
The function imflatfield can be of help.
I = imread('raw.png');
%% Removing Vignette and binarizing image
sigma = 20;
Iflatfield = imflatfield(I, sigma);
Iflatfield = rgb2gray(Iflatfield);
BW = imbinarize(Iflatfield);
IBW = uint8(zeros(size(BW)));
IBW(~BW) = 255;
%% applying morphologies
J = medfilt2(medfilt2(IBW));
SE = strel('ball',5, 5);
J = imerode(J, SE);
SE = strel('ball', 4, 4);
J = imdilate(J, SE);
%% check results
C = imfuse(I, J,"falsecolor");
imshow(C)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!