如何使用matlab将for循环中的多个直方图连接成1个直方图
How can I concatenate multiple histograms in a for loop into 1 histogram using matlab
我是 MATLAB 新手。我使用 imhist 函数计算了 25 个直方图,这些直方图是使用 for 循环将图像细分为 25 个块 (1block=1histogram) 创建的。我如何将这些直方图连接到同一个图上以制作一个直方图,例如,第一个块的直方图在 x 轴上从 0 延伸到 255,第二个块的直方图从 256 延伸到 511,依此类推直到第25块。我该怎么做?
我找到了这样的解决方案 but my problem is I calculated all 25 histograms using for loop and I don't know what variable should I pass so that I can concatenate all histograms in 1 long histogram as I want it to be. I also found the similar example but it is in python
这是我所做的:
%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
使用 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);
我是 MATLAB 新手。我使用 imhist 函数计算了 25 个直方图,这些直方图是使用 for 循环将图像细分为 25 个块 (1block=1histogram) 创建的。我如何将这些直方图连接到同一个图上以制作一个直方图,例如,第一个块的直方图在 x 轴上从 0 延伸到 255,第二个块的直方图从 256 延伸到 511,依此类推直到第25块。我该怎么做?
我找到了这样的解决方案
这是我所做的:
%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
使用 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);