I have a black/white image I0
(512 x 512) to which I have to remove the first k
pixel and calculate the histogram of the resulting image.
Let me explain: I have to build the histogram of the image I0
without considering the first k
pixels.
This is my code:
k = 8;
% compute the histogram of the entire image I0
[vecComp, histComp] = histKtoEnd(I0, 0, k);
% compute the histogram of the image I0 without the first k pixel
[vecWithoutKPixel, histWithoutKPixel] = histKtoEnd(I0, k, k);
where:
function [vecWithoutKPixel, hist] = histKtoEnd(image, k, colorDepth)
% image to row vector
imageVec = reshape(image.', [], 1);
l = length(imageVec);
% I "delete" the first k pixel
vecWithoutKPixel = imageVec((k+1) : l-1);
vecWithoutKPixel(end+1) = imageVec(l);
% inizialization
hist = zeros(1, 2^colorDepth);
% create the vector of occurrences
for i = 0 : (2^colorDepth - 1)
grayI = (vecWithoutKPixel == i);
hist(1, i+1) = sum(grayI(:));
end
end
To display the two histograms do:
subplot(1, 2, 1);
bar(0:2^k-1, histComp, 'r');
title('Histogram of the entire image');
axis([minColor, maxColor, 0, numberOfPixels]);
subplot(1, 2, 2);
bar(0:2^k-1, histWithoutKPixel, 'r');
title('Histogram of the image without the first k pixels');
axis([minColor, maxColor, 0, numberOfPixels]);
As you can see, the histograms are very different, yet should differ very little since the difference is only 8 pixels.
Where am I wrong?
Also in warkspace the newly created variables have these dimensions:
vecComp -> 262144 x 1 uint8
histComp -> 1 x 256 double
vecWithoutKPixel -> 65528 x 1 uint8
histWithoutKPixel -> 1 x 256 double
This is very strange. I should have:
vecComp -> 262144 x 1
vecWithoutKPixel -> 262136 x 1
Could someone help me? Thank you
I'm working with DICOM images and the command info = dicominfo(filename)
get
size = info.FileSize; % 262582;
colorType = info.ColorType; % grayscale
So I don't think that the problem is the image.
If I put a brakpoint on line vecWithoutKPixel(end+1) = imageVec(l);
I get that imageVec
is 262144 x 1 uint8, and:
function [vecWithoutKPixel, hist] = histKtoEnd(image, k, colorDepth)
% image to row vector
imageVec = reshape(image.', [], 1);
l = length(imageVec);
size(imageVec) % 262144 x 1
% I "delete" the first k pixel
vecWithoutKPixel = imageVec((k+1) : l-1);
vecWithoutKPixel(end+1) = imageVec(l);
% inizialization
hist = zeros(1, 2^colorDepth);
% create the vector of occurrences
for i = 0 : (2^colorDepth - 1)
grayI = (vecWithoutKPixel == i);
hist(1, i+1) = sum(grayI(:));
end
end
If I change the commands vecWithoutKPixel = imageVec((k+1) : l-1);
vecWithoutKPixel(end+1) = imageVec(l);
with vecWithoutKPixel = imageVec((k+1) : l);
I get that vecWithoutKPixel = []
.