Different histogram output from same image

5 views (last 30 days)
Farisa
Farisa on 16 May 2023
Commented: Walter Roberson on 16 May 2023
When executing this code:
locationTrain = 'D:\Kuliah\PCB\Tubes\Images\training\*.png';
train = imageDatastore(locationTrain);
locationGTTrain = 'D:\Kuliah\PCB\Tubes\GT\training\*.png';
GTTrain = imageDatastore(locationGTTrain);
trainName = ["017" "032" "036" "037" "040" "042" "049" "057" "060" "063" "064" "066" "068" "069" "080" "081" "084" "088" "094" "098" ];
count = 0;
countmasked = 0;
i = 1;
while hasdata(train)
img = read(train) ; % read image from datastore
red = img(:,:,1); % Red channel
green = img(:,:,2); % Green channel
x = read(GTTrain);
Mask = logical(x); % Crop Image
props = regionprops(Mask, 'BoundingBox');
thisBB = props.BoundingBox;
MaskCrop = imcrop(Mask, thisBB); % Crop GT in binary
TrainCrop = imcrop(green, thisBB); % Crop image in red channel
% Masked & cropped image
% Use GT as mask
Maskedgreen = bsxfun(@times, uint8(TrainCrop), cast(MaskCrop,class(uint8(TrainCrop))));
figure;
imhist(Maskedgreen);
saveas(gcf,['histo green train ' num2str(trainName(i)) '.jpg'])
count = count + imhist(green);
countmasked = countmasked + imhist(Maskedgreen);
i = i + 1;
end
The histogram output is:
However, I am unable to run that code and thus modifying it to:
locationTrain = 'D:\Kuliah\PCB\Tubes\Images\training\*.png';
train = imageDatastore(locationTrain);
locationGTTrain = 'D:\Kuliah\PCB\Tubes\GT\training\*.png';
GTTrain = imageDatastore(locationGTTrain);
trainName = ["017" "032" "036" "037" "040" "042" "049" "057" "060" "063" "064" "066" "068" "069" "080" "081" "084" "088" "094" "098" ];
count = 0;
countmasked = 0;
i = 1;
while hasdata(train)
img = read(train) ; % read image from datastore
red = img(:,:,1); % Red channel
green = img(:,:,2); % Green channel
x = read(GTTrain);
Mask = logical(x); % Crop Image
props = regionprops(Mask, 'BoundingBox');
thisBB = props.BoundingBox;
BB2 = reshape(thisBB, [], 2);
thisBB = reshape(BB2(1:2,:), 1, []);
MaskCrop = imcrop(Mask, thisBB); % Crop GT in binary
TrainCrop = imcrop(green, thisBB); % Crop image in red channel
% Masked & cropped image
% Use GT as mask
Maskedgreen = bsxfun(@times, uint8(TrainCrop), cast(MaskCrop,class(uint8(TrainCrop))));
figure;
imhist(Maskedgreen);
saveas(gcf,['histo green train ' num2str(trainName(i)) '.jpg'])
count = count + imhist(green);
countmasked = countmasked + imhist(Maskedgreen);
i = i + 1;
end
and the resulting histogram is:
Why can it be different? What approach can I use to produce the same histogram as before?

Answers (1)

Cris LaPierre
Cris LaPierre on 16 May 2023
Without your images to test with, it's hard to say for certain. However, the only difference in your code is the following two lines
BB2 = reshape(thisBB, [], 2);
thisBB = reshape(BB2(1:2,:), 1, []);
This could potentially change what coordinates are used for your bounding box, which then affects what data is cropped. It would make sense, then, that if the size of the bounding box changes, the resulting histogram will change as well.
  1 Comment
Walter Roberson
Walter Roberson on 16 May 2023
That section of code is from a previous response I made to the user, and had to do with compensating for the situation in which GTTrain is potentially 3 dimensions such as if it is rgb or a volume.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!