我有一张黑/白图像I0
(512 x 512),我必须删除第一个k
像素并计算生成图像的直方图。
让我解释一下:我必须在不考虑第一个I0
像素的情况下构建图像k
的直方图。
这是我的代码:
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);
其中:
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
要显示两个直方图:
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]);
正如您所看到的,直方图非常不同,但由于差异仅为8像素,因此差别很小。
我哪里错了?
同样在warkspace中,新创建的变量具有以下维度:
vecComp -> 262144 x 1 uint8
histComp -> 1 x 256 double
vecWithoutKPixel -> 65528 x 1 uint8
histWithoutKPixel -> 1 x 256 double
这很奇怪。我应该:
vecComp -> 262144 x 1
vecWithoutKPixel -> 262136 x 1
有人可以帮助我吗? 谢谢
我正在使用DICOM图像和命令info = dicominfo(filename)
获取
size = info.FileSize; % 262582;
colorType = info.ColorType; % grayscale
所以我不认为问题出在图像上。
如果我在行vecWithoutKPixel(end+1) = imageVec(l);
上放置一个制动点,我就知道imageVec
是262144 x 1 uint8,并且:
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
如果我使用vecWithoutKPixel = imageVec((k+1) : l-1);
vecWithoutKPixel(end+1) = imageVec(l);
更改命令vecWithoutKPixel = imageVec((k+1) : l);
,我会得到vecWithoutKPixel = []
。
答案 0 :(得分:1)
使用I0=randi(255,100,100)
创建“随机图像”,然后运行代码将生成如下图:
我猜这个情节看起来应该是应该的,变量的尺寸也是正确的:
vecComp 10000x1 double
vecWithoutKPixel 9992x1 double
因此,我们的问题实际上不是您的代码,而是您的形象。你的图像并不是真正的灰度值,以前曾被其他人提到过。但是不要像@Richard建议的那样将一些变量转换为uint8
,而是应该将图像转换为double。如果您的图片是rgb值,请使用I0=double(rgb2gray(I0));
,或者由于某些其他原因仅使用uint8
,请使用I0=double(I0)
然后将其传递给该函数。希望这会有所帮助。
答案 1 :(得分:0)
我认为问题在于图片数据最初是uint8
格式,并且在某些时候会转换为double
。因此,行
grayI = (vecWithoutKPixel == i);
histKtoEnd
中的可能仅适用于uint8
数据(因为您将数据与整数进行比较)。
尝试添加行
vecWithoutKPixel = uint8(vecWithoutKPixel);
到histKtoEnd
函数:
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)
vecWithoutKPixel = uint8(vecWithoutKPixel); % Cast to integer
grayI = (vecWithoutKPixel == i);
hist(1, i+1) = sum(grayI(:));
end
end