图像的颜色特征,分块

*******************************************************************************************************************************************************

颜色特征提取(一)------颜色直方图

http://blog.csdn.net/langb2014/article/details/45557935

*******************************************************************************************************************************************************


*******************************************************************************************************************************************************

颜色特征提取(二)------颜色矩

http://blog.csdn.net/langb2014/article/details/45564679

*******************************************************************************************************************************************************


*******************************************************************************************************************************************************

图像分块并保存matlab实现

http://blog.csdn.net/langb2014/article/details/45581007

*******************************************************************************************************************************************************


*******************************************************************************************************************************************************

颜色特征提取(四)------颜色相关图

http://blog.csdn.net/langb2014/article/details/45618167

*******************************************************************************************************************************************************



具体内容如下:

=====================================================================================================

颜色特征提取(一)------颜色直方图

=====================================================================================================

颜色特征是一种全局特征,描述了图像或图像区域所对应的景物的表面性质.一般颜色特征是基于像素点的特征,此时所有属于图像或图像区域的像素都有各自的贡献.由于颜色对图像或图像区域的方向、大小等变化不敏感,所以颜色特征不能很好地捕捉图像中对象的局部特征.另外,仅使用颜色特征查询时,如果数据库很大,常会将许多不需要的图像也检索出来.颜色直方图是最常用的表达颜色特征的方法,其优点是不受图像旋转和平移变化的影响,进一步借助归一化还可不受图像尺度变化的影响,基缺点是没有表达出颜色空间分布的信息.

(颜色直方图):

       A color histogram of an image represents the distribution of the composition of colors in the image. It shows different types of colors appeared and the number of pixels in each type of the colors appeared. The relation between a color histogram and a luminance histogram is that a color histogram can be also expressed as “Three Color Histograms”, each of which shows the brightness distribution of each individual Red/Green/Blue color channel.

      使用最多的可能就是灰度直方图,而它丢失了很多颜色信息,所以努力实现颜色直方图,它能够直接代表实际图中的颜色的数量。

MATLAB实现:

function createColorHistograms(im_str)

if ~isstr(im_str)
    if ndims(im_str)==3
        try
            col_array_vals=double(im_str);
        catch
            disp('Input is not a valid three-dimensional array');
            return;
        end
    end
else
    try
        col_array_vals=double(imread(im_str));
        if ndims(col_array_vals)~=3
            disp('Input is not a valid three-dimensional array');
            return;
        end

    catch
        disp('Input string does not point to a valid image file');
        return;
    end
end

res_val=90;

t_count=res_val*floor(col_array_vals(:,:,1)/res_val)+256*(res_val*floor(col_array_vals(:,:,2)/res_val))+256*256*(res_val*floor(col_array_vals(:,:,3)/res_val));
t_count=sort(t_count(:));

[col_val,ind_first]=unique(t_count,'first');
[col_val,ind_last]=unique(t_count,'last');
disp('Drawing color bars')

disp('Drawing image')
subplot(121);
set(gcf,'position',[5   61   274   236]);
imshow(col_array_vals/255)
colorbars(col_val,ind_last-ind_first,1/3,1/4)


function colorbars(triplet_color,triplet_freq,varargin)

if nargin==2
    color_pow=1/3;
    freq_pow=1/4;
else
    color_pow=varargin{1};
    freq_pow=varargin{2};
end

N_rand=randperm(length(triplet_freq));
triplet_freq=sqrt(triplet_freq(N_rand));
triplet_color=triplet_color(N_rand);

triplet_color=([rem(triplet_color,256) floor(rem(triplet_color,256*256)/255) floor(triplet_color/(256*256))]/255);
triplet_color_norm=triplet_color./repmat(((sum(triplet_color.^(1),2))+.005),1,3);
max(triplet_color_norm)
triplet_diff=sum(abs(triplet_color_norm-repmat(triplet_color_norm(end,:),size(triplet_color_norm,1),1)),2);

triplet_diff=sum(abs(triplet_color_norm-repmat([.9 0 0],size(triplet_color_norm,1),1)),2);

max(triplet_diff)

triplet_diff=(triplet_diff/max(triplet_diff).^(color_pow))+(triplet_freq*0).^(freq_pow);



[d,inds_sort]=sort(triplet_diff);
triplet_freq=(triplet_freq(inds_sort));
triplet_color=(triplet_color(inds_sort,:));

num_bars=length(triplet_color);
max_val=max(triplet_freq);
% close all;
subplot(122);
axis([0 num_bars 0 1]);
%% 
    [~,ind] = max(triplet_freq);
    triplet_color(ind,:)=[];
    triplet_freq(ind,:)=[];
    num_bars = num_bars-1;
%%   
for i=1:num_bars
    tempColor=min(triplet_color(i,:),.9);
    %===
    % Use patch to draw individual bars
    %===
    patch([i-1 i-1 i i],...
        [0 triplet_freq(i)/max_val triplet_freq(i)/max_val 0],...
        tempColor,...
        'edgecolor',...
        tempColor);
    
end
% colorbar('LineWidth',1);

set(gca,'xtick',[0:10:255])
set(gca,'ytick',[0:0.05:1])
set(gcf,'position',[5 378 560 420]);
set(gca,'visible','on')

function y_val=sigmoidVal(x_val,varargin)

if nargin==1
    multip_val=15;
else
    multip_val=varargin{1};
end

y_val=1./(1+exp(-(x_val-.5)*multip_val));



=====================================================================================================

颜色特征提取(二)------颜色矩

=====================================================================================================

一种非常简单而有效的颜色特征使由StrickerOrengo所提出的颜色矩(color moments) [7]。这种方法的数学基础在于图像中任何的颜色分布均可以用它的矩来表示。此外,由于颜色分布信息主要集中在低阶矩中,因此仅采用颜色的一阶矩(mean)、二阶矩(variance)和三阶矩(skewness)就足以表达图像的颜色分布。与颜色直方图相比,该方法的另一个好处在于无需对特征进行向量化。因此,图像的颜色矩一共只需要9个分量(3个颜色分量,每个分量上3个低阶矩),与其他的颜色特征相比是非常简洁的。在实际应用中为避免低次矩较弱的分辨能力,颜色矩常和其它特征结合使用,而且一般在使用其它特征前起到过滤缩小范围(narrow down)的作用。 

三个颜色矩的数学定义:




[1]stricker M ,Orengo M.similarity of col0rimages[J].Proc.SPIE St0rage and Retrieval f0r Image and Video Dtabases,1995,242O:381—392

下面是颜色矩的matlab实现:

function cmVec = colorMom(direc)
% function cmVec = colorMom(direc)
% Input: directory of a JPG image
% Output: a color moment feature vector of the input image. The feature
% vector is extracted from a 5*5 grid and represented by the first 3
% moments for each grid region in Lab color space as a normalized
% 225-dimensional vector.
% Function RGB2Lab (By Mr. Ruzon in Stanford U.) is used in this function.

jpgfile = imread(direc);
% jpgfile = imread('shot141_18_NRKF_1.jpg');
if length(size(jpgfile))==2
    jpgfile1=zeros(size(jpgfile,1),size(jpgfile,2),3);
    jpgfile1(:,:,1)=jpgfile;
    jpgfile1(:,:,2)=jpgfile;
    jpgfile1(:,:,3)=jpgfile;    
    jpgfile=jpgfile1;
    direc
end

% labfile = RGB2Lab(jpgfile);
[a b c] = size(jpgfile);
% m, n represent sizes of the grid
m = floor(a/5);
n = floor(b/5);
cmVec = zeros(1,225);% 5x5 x9,9个特征
for i=1:5
    for j = 1:5
%        subimage = labfile((i-1)*m+1:i*m,(j-1)*n+1:j*n,:);
         subimage = jpgfile((i-1)*m+1:i*m,(j-1)*n+1:j*n,:);
% cal. Mean, ...
        tmp = (i-1)*5+j-1;
%         I=imshow(subimage,[]);
        cmVec(tmp*9+1) = mean(mean(subimage(:,:,1)));
        cmVec(tmp*9+2) = mean(mean(subimage(:,:,2)));
        cmVec(tmp*9+3) = mean(mean(subimage(:,:,3)));
        % cal Moment 2 and 3
        for p = 1:m
            for q = 1:n
                % === Moment2
                cmVec(tmp*9+4) = cmVec(tmp*9+4) + (subimage(p,q,1)-cmVec(tmp*9+1))^2;
                cmVec(tmp*9+5) = cmVec(tmp*9+5) + (subimage(p,q,2)-cmVec(tmp*9+2))^2;
                cmVec(tmp*9+6) = cmVec(tmp*9+6) + (subimage(p,q,3)-cmVec(tmp*9+3))^2;
                % === Moment3
                cmVec(tmp*9+7) = cmVec(tmp*9+7) + (subimage(p,q,1)-cmVec(tmp*9+1))^3;
                cmVec(tmp*9+8) = cmVec(tmp*9+8) + (subimage(p,q,2)-cmVec(tmp*9+2))^3;
                cmVec(tmp*9+9) = cmVec(tmp*9+9) + (subimage(p,q,3)-cmVec(tmp*9+3))^3;                
            end
        end
        cmVec((tmp*9+4):(tmp*9+9)) = cmVec((tmp*9+4):(tmp*9+9))/(m*n);
        cmVec(tmp*9+4) = cmVec(tmp*9+4)^(1/2);
        cmVec(tmp*9+5) = cmVec(tmp*9+5)^(1/2);
        cmVec(tmp*9+6) = cmVec(tmp*9+6)^(1/2);

        if cmVec(tmp*9+7) >0
            cmVec(tmp*9+7) = cmVec(tmp*9+7)^(1/3);
        else
            cmVec(tmp*9+7) = -((-cmVec(tmp*9+7))^(1/3));
        end
        if cmVec(tmp*9+8) >0
            cmVec(tmp*9+8) = cmVec(tmp*9+8)^(1/3);
        else
            cmVec(tmp*9+8) = -((-cmVec(tmp*9+8))^(1/3));
        end
        if cmVec(tmp*9+9) >0
            cmVec(tmp*9+9) = cmVec(tmp*9+9)^(1/3);
        else
            cmVec(tmp*9+9) = -((-cmVec(tmp*9+9))^(1/3));
        end 
        
    end
end
% Normalize...
if sqrt(sum(cmVec.^2))~=0
    cmVec = cmVec / sqrt(sum(cmVec.^2));
end

=====================================================================================================

图像分块并保存matlab实现

=====================================================================================================

我对一张图进行的分块并且保存分块的简单实现:

[FileName,PathName] = uigetfile('*.*','Select the image'); 
Im=imread([PathName FileName]);
imshow(Im)
hold on
L = size(Im);
height=64;
width=64;
max_row = floor(L(1)/height);
max_col = floor(L(2)/width);
seg = cell(max_row,max_col);
%分块
for row = 1:max_row      
    for col = 1:max_col        
    seg(row,col)= {Im((row-1)*height+1:row*height,(col-1)*width+1:col*width,:)};  
    end
end 
for i=1:max_row*max_col
imwrite(seg{i},strcat('m',int2str(i),'.bmp'));  
end
%画出分块的边界
for row = 1:max_row      
    for col = 1:max_col  
 rectangle('Position',[160*(col-1),160*(row-1),160,160],...
         'LineWidth',2,'LineStyle','-','EdgeColor','r');
        end
end 
hold off

后面贴一个小方法:

A=rand(256,64);
%将A分块
B=mat2cell(A,ones(256/16,1)*16,ones(64/16,1)*16);
%B{i,j}就是所要的分块矩阵
%将分块矩阵合并
C=cell2mat(B)
%C就是合并好的矩阵,即C=A




=====================================================================================================

颜色特征提取(四)------颜色相关图

=====================================================================================================

颜色相关图(color correlogram)是图像颜色分布的另一种表达方式[16]。这种特征不但刻画了某一种颜色的像素数量占整个图像的比例,还反映了不同颜色对之间的空间相关性。实验表明,颜色相关图比颜色直方图和颜色聚合向量具有更高的检索效率,特别是查询空间关系一致的图像。
       一种简化的变种是颜色自动相关图(color auto-correlogram),它仅仅考察具有相同颜色的像素间的空间关系,因此空间复杂度降到O(Nd)。假设图像的记号为I(x,y),x、y为空间坐标;包含的颜色有C1,C2,C3...Cn.设置两种颜色之间的距离为d.那我们将生成这样的一个直方图:它的bin的个数为n的平方(颜色的组合数目),对于其中的每Bin,Bin的大小为Bin(Ci,Cj) = Σx,y{||I(x,y,Ci)-I(x,y,Cj)|| =d}。其中,||*||表示像素值为Ci,Cj的两个像素的空间距离,然后统计这样的像素个数。所以,要是设置不同的距d1,d2,d3...dm(共D个).那个Bin的维数为(n*n*D)。进一步,我们只考虑相同颜色之间空间关系,就称为颜色自相关图(colorauto-correlogram)那个Bin的维数为(n*D)。颜色相关图可以看作是一张用颜色对<x, y>索引的表,其中<x, y>的第k个分量表示颜色为c(x)的像素和颜色为c(y)的像素之间的距离小于k的概率。如果考虑到任何颜色之间的相关性,颜色相关图会变得非常复杂和庞大 (空间复杂度为O(N2d))。

       颜色相关图概念来自http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=609412,

                                       http://www.cs.cornell.edu/rdz/Papers/ecdl2/spatial.htm#Huang97

       原文解释:A color correlogram (henceforth correlogram) expresses how thespatial correlation of pairs of colors changes with distance. Informally, a correlogramfor an image is a table indexed by color pairs, where the d-th entry for row (i,j)specifies the probability of finding a pixel of color at a distancedfrom a pixel of color i in this image. Here d is chosen from a set ofdistance values (see [Huang] for the formal definition).An autocorrelogram captures spatial correlation between identical colors only.This information is a subset of the correlogram and consists of rows of the form (i,j)only.

颜色自相关图的MATLAB实现:

方法一:

function  output = ColorCorrelogram(rgb,d)  
R = rgb(:,:,1);  
G = rgb(:,:,2);  
B = rgb(:,:,3);  
R_BITS = 2;%bit量化  
G_BITS = 2;  
B_BITS = 2;  
size_color = 2^R_BITS*2^G_BITS*2^B_BITS;%归一化后的颜色种数  
R1=bitshift(R,-(8-R_BITS));  
G1=bitshift(G,-(8-G_BITS));  
B1=bitshift(B,-(8-B_BITS));  
%包含的颜色种数为:4x4x4  
I=R1+G1*2^R_BITS+B1*2^R_BITS*2^B_BITS;%新生图像  
temp = zeros(size_color,1);  
os = offset(d);  
s = size(os);  
for i = 1:s(1)  
    offset = os(i,:);  
    glm = GLCMATRIX(I,offset,size_color);  
    temp = temp+glm;  
end  
hc = zeros(size_color,1);  
for j = 0:size_color-1  
    hc(j+1) = numel(I(I == j));%Index为j的计数  
end  
output = temp./(hc+eps);  
output = output/(8*d);  
end  
function os = offset(d)  
[r,c] = meshgrid(-d:d,-d:d);  
r = r(:);  
c = c(:);  
os = [r c];  
bad = max(abs(r),abs(c)) ~= d;  
os(bad,:) = [];  
end  
function out = GLCMATRIX(si,offset,nl)  
s = size(si);%图像大小  
[r,c] = meshgrid(1:s(1),1:s(2));%网格化,很明显  
r = r(:);%向量化  
c = c(:);%向量化  
r2 = r+offset(1);%加偏置  
c2 = c+offset(2);%加偏置  
bad = c2<1|c2>s(2)|r2<1|r2>s(1);%筛选出index不在图像内的点  
Index = [r c r2 c2];%原始距离与相对距离的矩阵  
Index(bad,:) = [];%剔除坏点  
  
v1 = si(sub2ind(s,Index(:,1),Index(:,2)));%从索引到数据  
v2 = si(sub2ind(s,Index(:,3),Index(:,4)));  
v1 = v1(:);  
v2 = v2(:);  
Ind  = [v1 v2 ];  
bad = v1~=v2;%这里计算的是颜色自相关图  
Ind(bad,:) = [];  
if isempty(Ind)  
    oneGLCM2 = zeros(nl);  
else  
    oneGLCM2 = accumarray(Ind+1,1,[nl,nl]);  
end  
out = [];  
for i = 1:nl  
    out = [out oneGLCM2(i,i)];  
end  
out = out(:);  
end  


function correlogram_vector=color_auto_correlogram(I,distance_vector)  
  
% This function creates the auto-correlogram vector for an input image of  
% any size. The different distances which is assumed apriori can be user-defined in a vector.  
  
% It implements the algorithm as defined in Huang et al. paper 'Image Indexing using color  
% autocorelogram'  
  
% Input:  
% I=The uint8 matrix representing the color image  
% distance_vector= The vector representating the different distances in  
% which the color distribution is calculated.  
  
% Output:  
% correlogram_vector=This is a straight vector representating the  
% probabilities of occurence of 64 quantized colors. Its total dimension is  
% 64n X 1; where n is the number of different inf-norm distances  
  
% Usage: (To create the auto-correlogram vector for user-defined distances)  
% I=imread('peppers.png'); distance_vector=[1 3];  
% correlogram_vector=color_auto_correlogram(I,distance_vector);  
  
% Contact Author:  
% Soumyabrata Dev  
% E-mail: soumyabr001@e.ntu.edu.sg  
% http://www3.ntu.edu.sg/home2012/soumyabr001/  
  
correlogram_vector=[];  
[Y,X]=size(rgb2gray(I));  
  
% quantize image into 64 colors = 4x4x4, in RGB space  
[img_no_dither, ~] = rgb2ind(I, 64, 'nodither');  
% figure, imshow(img_no_dither, map);  
%rgb = ind2rgb(img_no_dither, map); % rgb = double(rgb)  
  
[~,d]=size(distance_vector);  
count_matrix=zeros(64,d);   total_matrix=zeros(64,d);  
prob_dist=cell(1,d);  
  
for serial_no=1:1:d  
    for x=1:X  
        for y=1:Y  
            color=img_no_dither(y,x);  
         
            % At the given distance   
            [positive_count,total_count]=get_n(distance_vector(serial_no),x,y,color,img_no_dither,X,Y);  
            count_matrix(color+1,serial_no)=count_matrix(color+1,serial_no)+positive_count;  
            total_matrix(color+1,serial_no)=total_matrix(color+1,serial_no)+total_count;         
        end  
    end  
  
    prob_dist{serial_no}=count_matrix(:,serial_no)./(1+total_matrix(:,serial_no));  
  
end  
  
for serial_no=1:d  
    correlogram_vector=cat(1,correlogram_vector,prob_dist{serial_no});  
end  
  
end  

function [positive_count,total_count]=get_n(n,x,y,color,img_no_dither,X,Y)  
% This function is useful to get the validity map of the neighborhood case.  
% It can handle any number of neighborhood distances.  
  
% Input  
% n=The order of the neighborhood  
% x & y= x y co-ordinates of the given pixel  
% color= particular quantized color  
% img_no_dither= The color quantized image matrix  
% X & Y= The original dimensions of the input image  
  
% Output  
% positive_count= The number of occurences which have the same color  
% total_count= The total number of valid cases for this particular instant  
  
  
    valid_vector8n=zeros(1,8*n); % This is because of the propoerty of inf-norm. Each distance has 8 times the order  
    positive_count=0;   total_count=0;  
  
    nbrs_x=zeros(1,8*n);    nbrs_y=zeros(1,8*n);  
  
    % The counting of the pixels is done in the following manner: From the  
    % given pixel, go left-->up-->right-->down-->left-->up  
    % Y co-ordinates of nbrs  
    nbrs_y(1)=y;  
    d=1;  
    for k=2:1+n  
       nbrs_y(k)=y-d;  
       d=d+1;  
    end  
      
    nbrs_y(1+n:1:3*n+1)=y-n;  
      
    d=0;  
    for k=3*n+1:5*n+1  
       nbrs_y(k)=y-n+d;  
       d=d+1;  
    end  
      
    nbrs_y(5*n+1:1:7*n+1)=y+n;  
      
    d=0;  
    for k=7*n+1:1:7*n+1+(n-1)  
       nbrs_y(k)=y+n-d;  
       d=d+1;  
    end  
      
    % X co-ordinates of nbrs  
    nbrs_x(1)=x-n;  
      
    nbrs_x(2:1:1+n)=x-n;  
      
    d=0;  
    for k=1+n:1:3*n+1  
        nbrs_x(k)=x-n+d;  
        d=d+1;  
    end  
      
    nbrs_x(3*n+1:5*n+1)=x+n;  
      
    d=0;  
    for k=5*n+1:7*n+1  
        nbrs_x(k)=x+n-d;  
        d=d+1;  
    end  
          
    nbrs_x(7*n+1:7*n+1+(n-1))=x-n;  
      
    % Assigning the validity of the neighborhood  
    for i=1:8*n  
          
        if nbrs_x(i)>0 && nbrs_x(i)<=X && nbrs_y(i)>0 && nbrs_y(i)<=Y  
            valid_vector8n(i)=1;  
          
        else  
            valid_vector8n(i)=0;  
      
        end  
      
    end  
      
      
    % Couting the number of common colors in the valid areas of the  
    % neighborhood.  
    for j=1:8*n  
       if valid_vector8n(j)==1  
          data= img_no_dither(nbrs_y(j),nbrs_x(j));  
          if (data==color)  
              positive_count=positive_count+1;  
          end  
          total_count=total_count+1;  
       end  
    end  
  
end  




  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值