⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 magic_camera.m

📁 彩色图像纹理提取的一种算法
💻 M
字号:
% function: magic_camera
% description: read an image containing a collection of objects on a flat
% solid color background (i.e. a table) and produce (return & display) a
% new image containing all the original objects, organized so that similar
% objects are located close to one another, and all objects "point" the
% same direction (when applicable)
% author: Adam Meadows
% last updated: 5/9/2006
%
% - add an input parameter 'fname' (name of image file)
%
% - add an input parameter 'lightback'
%                           lightback = 1, background is light, bw must be
%                           swapped
%                           otherwise, no swap is needed
% 
% - add an input parameter 'cluster_by'
%                           cluster_by = 0, use shapes
%                           cluster_by = 1, use colors
%                           cluster_by = 2, use texture
%
% - add an input parameter 'order' (order in which to place objects on img)
%                           order = 1, use left-right order
%                           order = 2, use right-left order
%                           order = 3, use top-bottom order
%                           order = 4, use bottom-up order
%                           order = 0, randomly choose order
%
% - add an input parameter 'do_rot' 
%                           do_rot = 1, try to rotate objects to align
%                           do_rot = 0, don't try to rotate
%
% - add an input parameter 'thresh'
%                           threshold for determining objects (0-1)
%                           thresh = 0, use value calcualted by graythresh
%
% 
% - add an imput parameter 'mirror'
%                           mirror = 1, also check mirror image of objects
%                           mirror = 0, don't
%
% - add an input parameter 'dist_meas'
%                           dist_meas = 0, Euclidean Distance
%                           dist_meas = 1, Dynamic Time Warping
%                         
% - add an input parameter 'do_norm' (only valid with cluster by shape)
%                           do_norm = 1, normalize and interpolate to the
%                           same length
%                           otherwise, don't do it
%   
%
function nice_img = magic_camera(fname, lightback, cluster_by, order, do_rot, thresh, mirror, dist_meas, do_norm)

%if < 9 parameters, fill in depending on cluster_by

if(nargin <9)
    
    %flags that are the same regardless of cluster_by
    if nargin < 6
        thresh = 0;
    end
    if nargin < 5
        do_rot = 1;
    end
    if nargin < 4
        order = 0;
    end
    
    if cluster_by == 0
        %defaults for shape (mirror, euclidean, normalize)
        do_norm = 1;
        if nargin < 8
            dist_meas = 0;
        end
        if nargin < 7
           mirror = 1; 
        end
   
    elseif cluster_by == 1
        
        %defaults for color (no mirror, euclidean)
        do_norm = 0;
        if nargin < 8
            dist_meas = 0;
        end
        if nargin < 7
            mirror = 0;
        end
        
  
    elseif cluster_by == 2
        
        %defaults for texture (no mirror, euclidean)
        do_norm = 0;
        if nargin < 8
            dist_meas = 0;
        end
        if nargin < 7
            mirror = 0;
        end
        
    end
    
        
end

%TBD, add error checking for variable number of input arguments


%first read in the image
img = imread(fname);

%extract objects
objs = imsplit(img, lightback, do_rot, thresh);

%extract background
background = imbackground(img, lightback);

%shape clustering
if(cluster_by == 0)
    
    %now convert the objects into timeseries by shape
    freq = images2ts(objs, 0, do_norm, 0);

    
    %now create the distance matrix (and dist_vector for dendogram)
    dist_matrix = create_dist_matrix(freq, mirror, dist_meas);
    nice_img = create_new_image(dist_matrix, objs, background, order);
    
elseif(cluster_by == 1)
   
    %now convert the objects into timeseries by color
    freq = [];
    for i = 1:length(objs)
        c = objcolor(objs{i}, lightback, thresh);
        freq = [freq; c];
    end %end of for all objects
    
    dist_matrix = squareform(pdist(freq));
    nice_img = create_new_image(dist_matrix, objs, background, order);
    

elseif(cluster_by == 2)
    
    %now convert the objects to timeseries by texture
    freq = [];
    for i = 1:length(objs)
        t = imtext(objs{i}, lightback, thresh);
        freq = [freq; t];
    end %end of for all objects
    
    dist_matrix = squareform(pdist(freq));
    nice_img = create_new_image(dist_matrix, objs, background, order);
    
else
    disp('Invalid value of parameter "cluster_by", only (0,1) accepted');
    return;
end %end of if/elseif/else
  

%display the new image
figure
imshow(nice_img);

%create new filename
outfile = [fname '-after.jpg'];
imwrite(nice_img, outfile, 'jpg');

end %end of magic_camera function

⌨️ 快捷键说明

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