How to interpret the wrong image background of data augmentation?
35 views (last 30 days)
Show older comments
I'm working in semantic segmentation and faced a problem with the output of data augmentation after running the attached code. This problem is related to the background of some ground truth images; instead of a black background, it displays white, as shown in the attached screenshot.
for j = 1:numImages
% Read the original image and ground truth
img = readimage(imdsClass, j);
gtImg = readimage(gtImdsClass, j);
gtImg=imcomplement(gtImg);
% Generate filenames for the original images
originalImgName = fullfile(classImageFolder, ['original_' num2str(j) '_' classLabel '.jpg']);
originalGtImgName = fullfile(classGTFolder, ['original_' num2str(j) '_' classLabel '.png']);
% Save the original image and ground truth
imwrite(img, originalImgName);
imwrite(gtImg, originalGtImgName);
end
% Perform augmentation for the images that need to be generated
for j = 1:numToGenerate
% Read an image and its corresponding ground truth
img = readimage(imdsClass, mod(j, numImages) + 1);
gtImg = readimage(gtImdsClass, mod(j, numImages) + 1);
gtImg=imcomplement(gtImg);
% Apply augmentation (random rotation and flipping)
x = randi([-30, 30], 1); % Random rotation angle
y = randi([1, 2], 1); % Random flip direction (1: vertical, 2: horizontal)
% Augment the image and ground truth
augmentedImg = imrotate(img, x, 'bilinear', 'crop');
augmentedImg = flip(augmentedImg, y);
augmentedGtImg = imrotate(gtImg, x, 'nearest', 'crop');
augmentedGtImg = flip(augmentedGtImg, y);
% Generate filenames for the augmented images
augmentedImgName = fullfile(classImageFolder, ['augmented_' num2str(j) '_' classLabel '.jpg']);
augmentedGtImgName = fullfile(classGTFolder, ['augmented_' num2str(j) '_' classLabel '.png']);
% Save the augmented image and ground truth
imwrite(augmentedImg, augmentedImgName);
imwrite(augmentedGtImg, augmentedGtImgName);
end
2 Comments
Shivansh
on 8 Sep 2024 at 10:24
Hi Abdulrahman,
I tried to reproduce the issue but the code works fine on my end.
Please share a few images for reproducing the issue.
Answers (1)
Image Analyst
on 8 Sep 2024 at 14:39
Why are you calling imcomplement? Try not doing that.
What is the original background: white or black?
Finally, you could just check if it's white in the upper left pixel, and invert it if it's white.
if augmentedImg(1,1) ~= 0
augmentedImg = ~augmentedImg; % or imcomplement(augmentedImg), whatever works.
end
See Also
Categories
Find more on Image Data Workflows 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!