Matlab 我需要跟踪基于颜色直方图箱的视频图像。比如这个例子https://en.wikipedia.org/wiki/Color_histogram#Example_1
嘿,我在试着做基于箱子的颜色直方图 像这个例子: 现在,我自己只做了3个垃圾箱,但我想做3个以上Matlab 我需要跟踪基于颜色直方图箱的视频图像。比如这个例子https://en.wikipedia.org/wiki/Color_histogram#Example_1,matlab,Matlab,嘿,我在试着做基于箱子的颜色直方图 像这个例子: 现在,我自己只做了3个垃圾箱,但我想做3个以上 function H=my_hist(img) SIZE=size(img); H=[0 0 0;0 0 0;0 0 0]; for i=1:3 H(:,:,i)=[0 0 0;0 0 0;0 0 0];%%the template end for row=1:SIZE(1)%%%%%the main function for calculating bins for col
function H=my_hist(img)
SIZE=size(img);
H=[0 0 0;0 0 0;0 0 0];
for i=1:3
H(:,:,i)=[0 0 0;0 0 0;0 0 0];%%the template
end
for row=1:SIZE(1)%%%%%the main function for calculating bins
for col=1:SIZE(2)
%%check the bin of color
if(img(row,col,1)<(1/3))
red=1;
elseif(img(row,col,1)<(2/3))
red=2;
elseif(img(row,col,1)<=1)
red=3;
end
if(img(row,col,2)<(1/3))
green=1;
elseif(img(row,col,2)<(2/3))
green=2;
elseif(img(row,col,2)<=1)
green=3;
end
if(img(row,col,3)<(1/3))
blue=1;
elseif(img(row,col,3)<(2/3))
blue=2;
elseif(img(row,col,3)<=1)
blue=3;
end
H(red,green,blue)=H(red,green,blue)+1;%%fill histogram of bins
end
end
end
函数H=my_hist(img)
尺寸=尺寸(img);
H=[0 0;0 0;0 0 0];
对于i=1:3
H(:,:,i)=[0;0;0;0];%模板
结束
对于行=1:SIZE(1)%%计算存储箱的主要功能
对于col=1:大小(2)
%%检查颜色的箱子
如果(img(行,列,1)用于收集带有n
xn
xn
bin的三维直方图,则可以使用以下阶段:
- 将RGB值离散为每个
n
级别。
示例:对于n
=4,所有值都将是0、1、2或3
- 将离散化RGB转换为2D图像,每个元素相当于“基数n”中的数字。
想想看,是一个以十进制为基数的三位数,如:
从数字3、7、5中获取375,其中3为MSD(最高有效位),5为LSD(最低有效位)
- 使用MATLAB函数采集二维图像的直方图。
用n^3个箱子收集直方图
- 将收集的直方图重塑为
n
xn
xn
3D矩阵
下面是一个示例代码:
RGB = imread('peppers.png');
RGB = double(RGB)/256; %Convert image from uint8 to double (range [0, 0.9961]).
n = 3; %Number of histogram bins.
%Discretize: 1/n lowr elements goes to zero, next 1/n elements goes 1 next 1/n elements goes 2...
%The following formula applies uniform quantization, when range RGB is [0, 0.9999].
dRGB = floor(RGB * n); %In case of n = 3, discretizedRGB elements are going to be 0, 1, 2 (and 3)
%Convert dRGB to 2D image, each element is equivalent to digit in "base n".
%Think about is a three digits number in decimal base like:
%Getting 375 from the digits 3, 7, 5, when 3 is the MSD (most significant digit) and 5 is the LSD (least significant digit).
%I = dRGB(:,:,1) * n^2 + dRGB(:,:,2) * n + dRGB(:,:,3); %Take red as MSD
I = dRGB(:,:,1) + dRGB(:,:,2) * n + dRGB(:,:,3) * n^2; %Take blue as MSD
%Collect histogram of 2D image, with n^3 bins
H = histcounts(I(:), n^3);
%Reshape histogram to 3D matrix.
H3 = reshape(H, [n, n, n]);
%Check if result is equal to result of my_hist
if n == 3
refH3 = my_hist(RGB);
all(H3(:) == refH3(:))
end
如您所见,当n=3时,结果等于您的实现结果
备注:收集3D直方图不是很有用。
在大多数情况下,您需要为每个颜色通道收集单独的直方图:
R = RGB(:, :, 1);
G = RGB(:, :, 2);
B = RGB(:, :, 3);
rH = histcounts(R(:), n); %Red channel histogram
gH = histcounts(G(:), n); %Green channel histogram
bH = histcounts(B(:), n); %Blue channel histogram
在某些情况下,亮度直方图:
Gray = rgb2gray(RGB);
grayH = histcounts(Gray(:), n); %Histogram of Grayscale equivalent image.