用matlab计算图像的hu矩具体代码
时间: 2023-12-02 10:02:09 浏览: 110
下面是一个计算图像Hu矩的MATLAB示例代码:
```matlab
% 读取图像
I = imread('image.jpg');
% 转换为灰度图像
Igray = rgb2gray(I);
% 二值化
Ibw = imbinarize(Igray);
% 计算Hu矩
hu = hu_moments(Ibw);
% 输出Hu矩
fprintf('Hu Moments:\n');
fprintf(' %f\n', hu);
```
需要注意的是,上述代码中需要使用`hu_moments`函数,该函数是MATLAB自带的函数,无需另外编写。如果需要自己编写计算Hu矩的函数,可以参考上面的方法,按照Hu矩的定义计算每个Hu矩的值。
相关问题
图像块的hu矩matlab代码
以下是计算图像块的Hu矩的 Matlab 代码:
```matlab
% 读取图像
img = imread('test.jpg');
% 转换为灰度图像
gray_img = rgb2gray(img);
% 提取图像块
block = gray_img(1:100, 1:100);
% 计算图像块的Hu矩
hu_moments = hu_moments(block);
disp(hu_moments);
% 计算Hu矩的函数
function hu = hu_moments(block)
% 计算归一化的中心矩
M00 = moment(block, 0, 0);
M10 = moment(block, 1, 0);
M01 = moment(block, 0, 1);
x_bar = M10 / M00;
y_bar = M01 / M00;
u20 = moment(block, 2, 0) / M00 - x_bar^2;
u02 = moment(block, 0, 2) / M00 - y_bar^2;
u11 = moment(block, 1, 1) / M00 - x_bar * y_bar;
u30 = moment(block, 3, 0) / M00 - 3 * x_bar * u20 + 2 * x_bar^3;
u03 = moment(block, 0, 3) / M00 - 3 * y_bar * u02 + 2 * y_bar^3;
u21 = moment(block, 2, 1) / M00 - 2 * x_bar * u11 - y_bar * u20 + 2 * x_bar^2 * y_bar;
u12 = moment(block, 1, 2) / M00 - 2 * y_bar * u11 - x_bar * u02 + 2 * y_bar^2 * x_bar;
% 计算Hu矩
hu = zeros(1, 7);
hu(1) = u20 + u02;
hu(2) = (u20 - u02)^2 + 4 * u11^2;
hu(3) = (u30 - 3 * x_bar * u20 + 2 * x_bar^3)^2 + (3 * y_bar * u02 - u03 + 2 * y_bar^3)^2;
hu(4) = (u30 + u12)^2 + (u21 + u03)^2;
hu(5) = (u30 - 3 * x_bar * u12 + 2 * x_bar^3) * (u30 + u12)^2 - (u03 - 3 * y_bar * u21 + 2 * y_bar^3) * (u21 + u03)^2;
hu(6) = (u20 - u02) * (u30 + u12)^2 + (4 * u11 * (u30 + u12) * (u21 + u03)) + (u21 - u03) * (u21 + u03)^2;
hu(7) = (3 * u21 - u03) * (u30 + u12) * ((u30 + u12)^2 - 3 * (u21 + u03)^2) - (u30 - 3 * u12) * (u21 + u03) * (3 * (u30 + u12)^2 - (u21 + u03)^2);
end
% 计算矩的函数
function m = moment(block, p, q)
[m, n] = size(block);
m = sum(sum(double(block) .* (repmat((0:n-1)', 1, m) .^ p) .* (repmat(0:m-1, n, 1) .^ q)));
end
```
该代码使用 `rgb2gray` 函数将彩色图像转换为灰度图像,然后提取图像块,最后使用 `hu_moments` 函数计算图像块的Hu矩。Hu矩是一个7维的向量,可以用来描述图像块的形状和纹理特征。
Hu矩提取matlab代码
Hu矩提取是一种基于轮廓分析的图像特征提取算法,其实现代码在Matlab中如下:
% 读取图像
I = imread('image.png');
% 将图像转化为二值图像
I_binary = imbinarize(I);
% 根据二值图像获取其轮廓
[B, L] = bwboundaries(I_binary, 'noholes');
% 计算轮廓的Hu矩
hu_moments = hu_moments(B{1});
% 输出Hu矩值
disp(hu_moments);
其中hu_moments是自定义的一个函数,实现如下:
function hu_moments = hu_moments(boundary)
% 计算轮廓的Hu矩
hu_moments = zeros(1, 7);
hu_moments(1) = inv_moment(boundary, 2, 0) + inv_moment(boundary, 0, 2);
hu_moments(2) = (inv_moment(boundary, 2, 0) - inv_moment(boundary, 0, 2))^2 + 4*inv_moment(boundary, 1, 1)^2;
hu_moments(3) = (inv_moment(boundary, 3, 0) - 3*inv_moment(boundary, 1, 2))^2 + (3*inv_moment(boundary, 2, 1) - inv_moment(boundary, 0, 3))^2;
hu_moments(4) = (inv_moment(boundary, 3, 0) + inv_moment(boundary, 1, 2))^2 + (inv_moment(boundary, 2, 1) + inv_moment(boundary, 0, 3))^2;
hu_moments(5) = (inv_moment(boundary, 3, 0) - 3*inv_moment(boundary, 1, 2))*(inv_moment(boundary, 3, 0) + inv_moment(boundary, 1, 2))*((inv_moment(boundary, 3, 0) + inv_moment(boundary, 1, 2))^2 - 3*((inv_moment(boundary, 2, 1) + inv_moment(boundary, 0, 3))^2)) + (3*inv_moment(boundary, 2, 1) - inv_moment(boundary, 0, 3))*(inv_moment(boundary, 2, 1) + inv_moment(boundary, 0, 3))*(3*(inv_moment(boundary, 3, 0) + inv_moment(boundary, 1, 2))^2 - ((inv_moment(boundary, 2, 1) + inv_moment(boundary, 0, 3))^2));
hu_moments(6) = (inv_moment(boundary, 2, 0) - inv_moment(boundary, 0, 2))*((inv_moment(boundary, 3, 0) + inv_moment(boundary, 1, 2))^2 - (inv_moment(boundary, 2, 1) + inv_moment(boundary, 0, 3))^2) + 4*inv_moment(boundary, 1, 1)*(inv_moment(boundary, 3, 0) + inv_moment(boundary, 1, 2))*(inv_moment(boundary, 2, 1) + inv_moment(boundary, 0, 3));
hu_moments(7) = (3*inv_moment(boundary, 2, 1) - inv_moment(boundary, 0, 3))*(inv_moment(boundary, 3, 0) + inv_moment(boundary, 1, 2))*((inv_moment(boundary, 3, 0) + inv_moment(boundary, 1, 2))^2 - 3*((inv_moment(boundary, 2, 1) + inv_moment(boundary, 0, 3))^2)) - (inv_moment(boundary, 3, 0) - 3*inv_moment(boundary, 1, 2))*(inv_moment(boundary, 2, 1) + inv_moment(boundary, 0, 3))*(3*(inv_moment(boundary, 3, 0) + inv_moment(boundary, 1, 2))^2 - ((inv_moment(boundary, 2, 1) + inv_moment(boundary, 0, 3))^2));
% 将Hu矩标准化
hu_moments = -sign(hu_moments).*log10(abs(hu_moments));
end
其中inv_moment也是自定义的一个函数,用来计算轮廓的非中心矩,实现如下:
function inv_m = inv_moment(boundary, p, q)
% 计算轮廓的非中心矩
inv_m = sum((boundary(:, 2).^p).*(boundary(:, 1).^q));
end
以上就是Hu矩提取的Matlab代码实现。
阅读全文