How to do background subtraction between two images
81 次查看(过去 30 天)
显示 更早的评论
I got many numerical frames. Please help me to revise the code to subtract the background so that I can work on the region which I am Interested (spray evolution). I also attached two pictures for your refenences.
My code:
backgroundImage = imread('animation-1_0000.png');
% Ensure the background image is grayscale
if size(backgroundImage, 3) == 3
backgroundImage = rgb2gray(backgroundImage);
end
% Check if the file exists
if isfile(currentFileName)
% Read the current image
thisImage = imread(currentFileName);
% Convert to grayscale if necessary
if size(thisImage, 3) == 3
thisImage = rgb2gray(thisImage);
end
% Debug: Print sizes before resizing
fprintf('Background image size: [%d, %d]\n', size(backgroundImage));
fprintf('Current image size: [%d, %d]\n', size(thisImage));
% Resize to match the background image size
if ~isequal(size(thisImage), size(backgroundImage))
thisImage = imresize(thisImage, size(backgroundImage));
end
% Perform subtraction
subtractionImage = backgroundImage - thisImage;
% Save the subtraction result
saveFileName = sprintf('%s/Sub/%04d.bmp', savefolder, i);
imwrite(subtractionImage, saveFileName);
% Black and White Image Processing
% Create a binary mask based on threshold
mask1 = abs(Wh) > 10; % Threshold above the noise level (10 gray levels)
% Rotate the mask image
J = imrotate(mask1, 90);
% Save the rotated mask
saveFileName = sprintf('%s/Wh/animation-1_%04d.png', savefolder, i);
imwrite(J, saveFileName);
end
0 个评论
回答(1 个)
Ayush
about 13 hours 前
Hi Khai,
I understand that you need to subtract the background effectively. Few improvements to the current logic can be done and integrated with your specific region of interest, which is the spray evolution.
Here is how you can enhance the process:
1. The current approach directly subtracts the background from each frame, but it may create negative values or lose fine details. To avoid this, we can take the absolute value of the subtraction or apply a threshold to highlight significant differences (such as spray movement).
2. After subtraction, we can focus on the region of interest (e.g., the spray). You might want to either define a fixed region or use image segmentation to extract dynamic areas where the spray evolves.
3. To better isolate the spray, we can apply thresholding to the difference image, followed by morphological operations (like erosion or dilation) to remove noise and fill gaps in the spray's structure.
4. We will add your mask-processing logic for further refinement, ensuring only relevant areas (spray) are focused upon.
Here is pseudo code for your reference:
backgroundImage = imread('animation-1_0000.png');
% Ensure the background image is grayscale
if size(backgroundImage, 3) == 3
backgroundImage = rgb2gray(backgroundImage);
end
if isfile(currentFileName)
% Read the current image
thisImage = imread(currentFileName);
if size(thisImage, 3) == 3
thisImage = rgb2gray(thisImage);
end
if ~isequal(size(thisImage), size(backgroundImage))
thisImage = imresize(thisImage, size(backgroundImage));
end
% Perform background subtraction (absolute difference to avoid negative values)
subtractionImage = abs(double(backgroundImage) - double(thisImage));
% Threshold to remove small differences (noise removal)
threshold = 10;
subtractionImage(subtractionImage < threshold) = 0;
% Apply binary thresholding to focus on significant differences (e.g., spray)
binaryImage = subtractionImage > threshold; % Create a binary mask
% Apply morphological operations to clean up the binary mask (optional)
binaryImage = imclose(binaryImage, strel('disk', 5)); % Fill small gaps
binaryImage = imopen(binaryImage, strel('disk', 3)); % Remove small noise
saveFileName = sprintf('%s/Sub/%04d.bmp', savefolder, i);
imwrite(subtractionImage, saveFileName);
% Further processing on the mask
% Rotate the mask for consistency with your logic
rotatedMask = imrotate(binaryImage, 90);
saveFileName = sprintf('%s/Wh/animation-1_%04d.png', savefolder, i);
imwrite(rotatedMask, saveFileName);
end
For more information on “imread”, you can refer here: https://www.mathworks.com/help/matlab/ref/imread.html
For more information on “imwrite”, you can refer here: https://www.mathworks.com/help/matlab/ref/imwrite.html
For more information on “rgb2gray”, you can refer here: https://www.mathworks.com/help/matlab/ref/rgb2gray.html
Hope it helps!
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!