如何使用matlab将for循环中的多个直方图连接成1个直方图

时间:2017-08-13 01:08:20

标签: matlab image-processing histogram block overlapping

我是MATLAB的新手。我使用imhist函数计算了25个直方图,这是通过使用for循环将图像细分为25个块(1block = 1个组合图)而创建的。如何在同一图表上连接这些直方图以制作一个直方图,例如,第一个块的直方图在x轴上从0到255延伸,第二个块的直方图从256延伸到511,依此类推,直到第25个街区。我怎么能这样做?

我找到了这样的解决方案How to concatenate 3 histograms on the same graph in Matlab,但我的问题是我使用for循环计算了所有25个直方图,我不知道应该传递什么变量,这样我就可以连接1长的所有直方图直方图,因为我想要它。我也找到了类似的例子,但它是在python Concatenate multiple histograms in matplotlib

这就是我所做的:

%declare variable
B=12; %B represent the block size
overlapp=3; %overlapping block is slid by 3 pixels along the image 
nob=0; %no of blocks

%get input from grayscale image
gray_path = 'D:\gray_image_folder\*.png';
total_gray_images=dir(gray_path);

for noOfGRAYimage = 1:length(total_gray_images)
% Specify images names with full path and extension    
    grayfilename=strcat('D:\gray_image_folder\', total_gray_images(noOfGRAYimage).name);
    grayImage = imread(grayfilename);% Read gray images 
    [r, c, p]=size(grayImage);
      for i=1:overlapp:(r-B)+1; 
  %         fprintf('i = %d\n', i);
  %         numberofblock = sprintf('%s_%d','block',nob)
          for j=1:overlapp:(c-B)+1;
  %             fprintf('j = %d\n', j);
              nob=nob+1;
              fy_mad(nob).block=grayImage(i:i+B-1,j:j+B-1); %partition (12x12) of blocks
              fy_mad(nob).position=[i j]; 
              fy_mad(nob).index=nob; 
              [rb, cb]=size(fy_mad(nob).block); %[12,12]
              localBinaryPatternImage = fy_mad(nob).block;
              %call function LBP
              LBP_centre = LBP (localBinaryPatternImage);
              figure;imhist(uint8(LBP_centre)) %histogram for each block

             % How to concatenate all histograms?
             %////

          end
      end
  end

我根据@crazyGamer

尝试了这个
%get input from grayscale image
gray_path = 'D:\gray_image_folder\*.png';
total_gray_images=dir(gray_path);

allCounts = []
allBinLocs = []

for noOfGRAYimage = 1:length(total_gray_images)
% Specify images names with full path and extension    
    grayfilename=strcat('D:\gray_image_folder\', total_gray_images(noOfGRAYimage).name, total_gray_images(noOfGRAYimage).name);
    grayImage = imread(grayfilename);% Read gray images 
    [r, c, p]=size(grayImage);

    for i=1:overlapp:(r-B)+1; 
        for j=1:overlapp:(c-B)+1;
            nob=nob+1;
            fy_mad(nob).block=grayImage(i:i+B-1,j:j+B-1); 
            fy_mad(nob).position=[i j]; 
            fy_mad(nob).index=nob; %number of blocks 

            [rb, cb]=size(fy_mad(nob).block); 
            localBinaryPatternImage = fy_mad(nob).block;

            %call function LBP
            LBP_centre = LBP (localBinaryPatternImage);

           % Let's compute and display the concatenation of all histogram.
            [counts, binLocs] = imhist(uint8(LBP_centre), 256);
            allCounts = [allCounts, counts];
            allBinLocs = [allBinLocs, binLocs + length(allBinLocs)]
            % "+ length(allBinLocs)" is to shift range from 256-511, and so on for each block.

        end
    end
end

figure;
stem(allBinLocs, allCounts);

结果如下:Concatenate histogram

1 个答案:

答案 0 :(得分:0)

使用return values of imhist存储bin位置和值,而不是直接在循环中绘制它们。

然后,您可以将它们连接在一个大数组中并显示直方图。

allCounts = []
allBinLocs = []

for noOfGRAYimage = 1:length(total_gray_images)
    % ...
    for i=1:overlapp:(r-B)+1; 
        % ...          
        for j=1:overlapp:(c-B)+1;
            % ...

            [counts, binLocs] = imhist(uint8(LBP_centre), 256);
            allCounts = [allCounts; counts(:)];
            allBinLocs = [allBinLocs; binLocs(:) + length(allBinLocs)];
            % "+ length(allBinLocs)" is to shift range from 256-511, and so on for each block.

        end
    end
end

figure;
stem(allBinLocs, allCounts);