How to interpret the wrong image background of data augmentation?

1 回表示 (過去 30 日間)
Abdulrahman
Abdulrahman 2024 年 9 月 8 日
編集済み: Image Analyst 2024 年 9 月 9 日
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
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.
Abdulrahman
Abdulrahman 2024 年 9 月 8 日
This atteached file is the data that I used.

サインインしてコメントする。

回答 (1 件)

Image Analyst
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
  2 件のコメント
Abdulrahman
Abdulrahman 2024 年 9 月 8 日
The original background images is black.
My problem is how to unifirm the background of all data as shown in attached pic.
I run the code without gtImg=imcomplement(gtImg);for small data (attached DA) and got correct results using your code (if augmentedImg(1,1) ~= 0 ......) but I got wrong results for whole dataset.
Image Analyst
Image Analyst 2024 年 9 月 9 日
編集済み: Image Analyst 2024 年 9 月 9 日
Why are you manually augmenting your images instead of using augment?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImage Data Workflows についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by