资源简介
基于灰度梯度共生矩阵,计算混合熵对图像实现分类
代码片段和文件信息
%图像块计算灰度共生矩阵
clc;
close all;
clear all;
gray=256;
I = imread(‘lena.bmp‘);
[mn]=size(I);
Energy = zeros(mn);
Entropy = zeros(mn);
entropy = [];
R = 8;
C = 8;
%划分成8*8图像块
for u = 1:8:m-7
for v = 1:8:n-7
IN = I(u:u+7v:v+7);
%采用平方求和计算梯度矩阵
GM=zeros(RC);
for i=1:R-1
for j=1:C-1
% n_GM=(IN(ij+1)-IN(ij))^2+(IN(i+1j)-IN(ij))^2+(IN(ij)-IN(i+1j+1))^2;
n_GM=(IN(ij+1)-IN(ij))^2+(IN(i+1j)-IN(ij))^2;
GM(ij)=sqrt(double(n_GM));
end
end
GM(1:end-1C) = GM(1:end-1C-1);
GM(R1:end-1) = GM(R-11:end-1);
GM(endend) = GM(end-1end-1);
%找出最大值最小值
n_min=min(GM(:));
n_max=max(GM(:));
%把梯度图象灰度级离散化
%设置新的灰度级为new_gray
new_gray=128;
%新的梯度矩阵为new_GM
new_GM=zeros(RC);
new_GM=uint8((GM-n_min)/(n_max-n_min)*(new_gray-1));
% 计算灰度梯度共生矩阵
H=zeros(graynew_gray);
for i = 1:R
for j =1:C
x = IN(ij);
y = new_GM(ij);
H(x+1y+1) = H(x+1y+1)+1;
end
end
%归一化灰度梯度矩阵 H_basic
H_basic=H/(R*C);
%计算能量 T5
T5=0;
for i=1:gray
for j=1:new_gray
T5=T5+H_basic(ij)^2;
end
end
%计算混合熵 T13
T13=0;
for i=1:gray
for j=1:new_gray
if(H_basic(ij)~=0)
T13=T13-H_basic(ij)*log2(H_basic(ij));
end
end
end
Energy(u:u+7v:v+7) = T5;
Entropy(u:u+7v:v+7) = T13;
entropy = [entropy T13];
end
end
figure;imshow(I);
%figure;imagesc(Energy);colorbar;
figure;imagesc(Entropy);colorbar;
%分为c类,每类数量平均
en = sort(entropy);
classpic = ones(mn);
%分为2类
% c = 2;
% fenwei_loc = round(length(en)*0.5);
% thre = en(fenwei_loc);
% %小于门限值的图像块标记为1,大于门限值的图像块标记为2
% loc = find(Entropy>thre);
% classpic(loc) = 2;
%分为c类
c = 4;
%分位点
fp = (1:(c-1))*(1/c);
floc = length(en)*fp;
thre = [min(en) en(floc) max(en)];
for i = 1:length(thre)-1
mint = thre(i);
maxt = thre(i+1);
loc = find(Entropy>=mint&Entropy<=maxt);
classpic(loc) = i;
end
figure;imagesc(classpic);
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2560 2015-12-16 19:51 test_gray_gradient.m
- 上一篇:自控第五版习题答案胡松涛
- 下一篇:ComToCom电脑串口互传文件软件
评论
共有 条评论