获取灰度向量
Get a vector of gray levels
我写了一个接受图像和 returns historgam 向量的 matlab 函数。我还想 return 一个灰度级向量,范围从 0 到 255。
这是我的代码:
function h = myimhist(im)
histogram=zeros(1,256);
h = imread(im);
[row,col]=size(h);
for x=1:1:row
for y=1:1:col
if h(x,y) < 1
continue;
else
t=h(x,y);
end
histogram(t)=histogram(t)+1;
end
end
subplot(1,2,1);
imshow(uint8(h));
title('Original Image');
subplot(1,2,2);
bar(histogram);
title('Histogarm of image');
end
如何获得灰度向量?
我认为 header 应该是
function [h,c] = myimhist(im)
如果您只想return 一个从 0 到 255 的 256 元素向量,请执行以下操作:
c = 0 : 255;
但是,如果您希望 c
仅包含图像中存在的那些强度,请尝试执行以下操作:
c = 0 : 255;
c(histogram == 0) = [];
这会删除 c
中没有直方图计数的强度。
如果我可以挑剔你的代码,计算直方图的更有效方法是遍历每个可能的强度,使用逻辑索引并计算 sum
。另外,我认为你的意思是 return 直方图,而不是图像。您的图像存储在 h
中,而您的直方图存储在 histogram
中。因此,您的函数声明应该 return histogram
而不是 h
.
因此,尝试这样的事情:
function [histogram,c] = myimhist(im)
h = imread(im);
%// Change
histogram = zeros(1, 256);
for idx = 0 : 255
histogram(idx) = sum(sum(h == idx));
end
c = 0 : 255;
% c(histogram == 0) = []; %// Depending on what you want
subplot(1,2,1);
imshow(h);
title('Original Image');
subplot(1,2,2);
bar(histogram);
title('Histogram of image');
end
当您显示图像时,您不必要地转换为 uint8
。您的图像已经是 uint8
类型了。这当然是假设您的直方图计算仅限于 [0-255]
,这应该是您的代码假设的情况。
以上代码将遍历所有可能的强度,找到图像中与该强度相等的位置,并对所有出现的位置求和。我们需要使用两个 sum
调用,因为在矩阵上调用 sum
将找到每一列的总和。要找到矩阵的总和,我们将对此结果调用 sum
。
如果你想避免同时使用循环,请考虑使用 bsxfun
和两个 sum
调用:
function [histogram,c] = myimhist(im)
h = imread(im);
pix_counts = bsxfun(@eq, h, permute(c, [3 1 2]));
histogram = squeeze(sum(sum(pix_counts, 1), 2));
c = 0 : 255;
% c(histogram == 0) = []; %// Depending on what you want
subplot(1,2,1);
imshow(h);
title('Original Image');
subplot(1,2,2);
bar(histogram);
title('Histogram of image');
end
bsxfun
with the eq
function is used such that we create a 3D matrix of counts where each slice tells you the locations of those pixels in your image that match a particular intensity. Therefore, the first slice tells you those locations that match intensity 0, the second slice tells you those locations that match intensity 1 and so on. When we're done, all we need to do is add up the elements in each slice individually. This can be done by summing over the rows of each slice first, followed by summing over the columns last. It doesn't matter what operation you do first. You could even sum over the columns first, followed by the rows last. What will result is a single 3D vector, and we want this to be a 1D vector, which is why squeeze
已使用
我写了一个接受图像和 returns historgam 向量的 matlab 函数。我还想 return 一个灰度级向量,范围从 0 到 255。
这是我的代码:
function h = myimhist(im)
histogram=zeros(1,256);
h = imread(im);
[row,col]=size(h);
for x=1:1:row
for y=1:1:col
if h(x,y) < 1
continue;
else
t=h(x,y);
end
histogram(t)=histogram(t)+1;
end
end
subplot(1,2,1);
imshow(uint8(h));
title('Original Image');
subplot(1,2,2);
bar(histogram);
title('Histogarm of image');
end
如何获得灰度向量? 我认为 header 应该是
function [h,c] = myimhist(im)
如果您只想return 一个从 0 到 255 的 256 元素向量,请执行以下操作:
c = 0 : 255;
但是,如果您希望 c
仅包含图像中存在的那些强度,请尝试执行以下操作:
c = 0 : 255;
c(histogram == 0) = [];
这会删除 c
中没有直方图计数的强度。
如果我可以挑剔你的代码,计算直方图的更有效方法是遍历每个可能的强度,使用逻辑索引并计算 sum
。另外,我认为你的意思是 return 直方图,而不是图像。您的图像存储在 h
中,而您的直方图存储在 histogram
中。因此,您的函数声明应该 return histogram
而不是 h
.
因此,尝试这样的事情:
function [histogram,c] = myimhist(im)
h = imread(im);
%// Change
histogram = zeros(1, 256);
for idx = 0 : 255
histogram(idx) = sum(sum(h == idx));
end
c = 0 : 255;
% c(histogram == 0) = []; %// Depending on what you want
subplot(1,2,1);
imshow(h);
title('Original Image');
subplot(1,2,2);
bar(histogram);
title('Histogram of image');
end
当您显示图像时,您不必要地转换为 uint8
。您的图像已经是 uint8
类型了。这当然是假设您的直方图计算仅限于 [0-255]
,这应该是您的代码假设的情况。
以上代码将遍历所有可能的强度,找到图像中与该强度相等的位置,并对所有出现的位置求和。我们需要使用两个 sum
调用,因为在矩阵上调用 sum
将找到每一列的总和。要找到矩阵的总和,我们将对此结果调用 sum
。
如果你想避免同时使用循环,请考虑使用 bsxfun
和两个 sum
调用:
function [histogram,c] = myimhist(im)
h = imread(im);
pix_counts = bsxfun(@eq, h, permute(c, [3 1 2]));
histogram = squeeze(sum(sum(pix_counts, 1), 2));
c = 0 : 255;
% c(histogram == 0) = []; %// Depending on what you want
subplot(1,2,1);
imshow(h);
title('Original Image');
subplot(1,2,2);
bar(histogram);
title('Histogram of image');
end
bsxfun
with the eq
function is used such that we create a 3D matrix of counts where each slice tells you the locations of those pixels in your image that match a particular intensity. Therefore, the first slice tells you those locations that match intensity 0, the second slice tells you those locations that match intensity 1 and so on. When we're done, all we need to do is add up the elements in each slice individually. This can be done by summing over the rows of each slice first, followed by summing over the columns last. It doesn't matter what operation you do first. You could even sum over the columns first, followed by the rows last. What will result is a single 3D vector, and we want this to be a 1D vector, which is why squeeze
已使用