image_encode.m

来自「用Cross validation的方法建立人工神经网络的模型!」· M 代码 · 共 47 行

M
47
字号
%
% function  [reconstruct_image,frequency] = image_encode(image_data,cdbk): 
% Encode each image using given codebook
% 
% Input:    image_data -- N*n_features, a matrix to represents an image,
%                         each row represents a block
%                 cdbk -- nwds*n_features matrix, codebook for image encoding
% Output: 
%     compressed_image -- nwds*1, represents a compressed 2D image, each
%                         item is an index of the cdbk
%    reconstruct_image -- N*n_features matrix, the reconstructed image
%                         using keyblocks
%            frequency -- 1*nwds the number of each cdword used in
%                         reconstruction
%

function [compressed_image,reconstruct_image,frequency] = image_encode(image_data,cdbk)

n_point = size(image_data,1);
nwds = size(cdbk, 1);
frequency = zeros(1,nwds);

for i_point = 1: n_point
    % get the vector of one point
    p = image_data(i_point);
    
    % partition this point into one region based on nearest
    % neighbor condition
    % initialize the parameter to be impractical    
    mindis = Inf;
    index_nwds = 0;
    
    for ic = 1: nwds
        cw = cdbk(ic,:);
        dis = my_distance(p,cw);
        if dis < mindis
            mindis = dis;
            index_nwds = ic;
        end
    end
    % encode current feature using nearest codeword
    compressed_image(i_point) = index_nwds;
    reconstruct_image(i_point,:) = cdbk(index_nwds,:);
    frequency(index_nwds) = frequency(index_nwds)+1;
end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?