How to get a simillar pair of images for image 256x256x3 in getSiameseBatch operation.

2 次查看(过去 30 天)
I am having two images I1=256x256x3 (Face1) and I2=256x256x3 (Face2). I am trying to make similar pair based on following code:
batchSize = 10;
[pairImage1,pairImage2,pairLabel] = getSiameseBatch(imdsTrain,batchSize);
Display the generated pairs of images.
for i = 1:batchSize
if pairLabel(i) == 1
s = "similar";
else
s = "dissimilar";
end
subplot(2,5,i)
imshow([pairImage1(:,:,:,i) pairImage2(:,:,:,i)]);
title(s)
end
but it is displaying only black and white pixels.

回答(1 个)

Sai Pavan
Sai Pavan 2024-4-12
Hello Sakshi,
I understand that you are trying to resolve the issue where the displayed images appear as only black and white pixels after "getSiameseBatch" operation.
The issue typically arises from a mismatch in the expected data range between the image data and what "imshow" expects. The "imshow" function assumes the input image data is in one of the following formats:
  • For grayscale images, values are expected to be within the range [0, 1] for floating-point types or in the range [0, 255] for integer types.
  • For RGB images, the same rules apply, with each channel of the RGB image needing to be within the appropriate range.
The problem might be that the images are of floating-point type but have values outside the [0, 1] range, which "imshow" interprets as being either all black (values <= 0) or all white (values >= 1) for most of the pixels.
This can be resolved by ensuring the images are correctly scaled before they are displayed using "imshow". If your images are of type double or single, we can normalize them to the [0, 1] range by dividing by their maximum value if you know the images are not already in this range. If the images are supposed to be in the [0, 255] range, converting them to uint8 might be the simplest solution if they're not already in an integer format.
Please refer to the modified code snippet attached below that illustrates the possible fix:
batchSize = 10;
[pairImage1,pairImage2,pairLabel] = getSiameseBatch(imdsTrain,batchSize);
for i = 1:batchSize
if pairLabel(i) == 1
s = "similar";
else
s = "dissimilar";
end
% Adjust the images to the correct format and range
img1 = pairImage1(:,:,:,i);
img2 = pairImage2(:,:,:,i);
% Normalize if necessary (assuming images are floating-point)
if isfloat(img1)
img1 = img1 - min(img1(:)); % Shift minimum to 0
img1 = img1 / max(img1(:)); % Scale maximum to 1
end
if isfloat(img2)
img2 = img2 - min(img2(:)); % Shift minimum to 0
img2 = img2 / max(img2(:)); % Scale maximum to 1
end
% If the images are supposed to be in the [0, 255] range but are not in uint8 format
if ~isa(img1, 'uint8')
img1 = im2uint8(img1); % Convert to uint8 if not already
end
if ~isa(img2, 'uint8')
img2 = im2uint8(img2); % Convert to uint8 if not already
end
subplot(2,5,i);
imshow([img1 img2]); % Display the image pair side by side
title(s);
end
Please refer to the below documentation to learn more about "im2uint8" function: https://www.mathworks.com/help/images/ref/im2uint8.html
Hope it helps!

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by