How to interpret the wrong image background of data augmentation?
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Shivansh
2024-9-8
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.
回答(1 个)
Image Analyst
2024-9-8
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!