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 CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by