Different histogram output from same image

조회 수: 9 (최근 30일)
Farisa
Farisa 2023년 5월 16일
댓글: Walter Roberson 2023년 5월 16일
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?

답변 (1개)

Cris LaPierre
Cris LaPierre 2023년 5월 16일
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
Walter Roberson
Walter Roberson 2023년 5월 16일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Histograms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by