1

我已经有了这个:
function [h] = histImage (img)
nPix = numel(img);
h = accumarray( img (:)+1 , ones(nPix,1)/nPix, [256 1] , @sum,0)

this function will return a grayscale histogram for a given img into a 1X256 vector

now i want to build this function: input: img - grayscale image matrix in rage [0..255] windowSize - a 1x2 array [r c] rows and cols.
output: histArray 3d matrix for every i,j the 1D array histArray(i,j,:) is the histogram of img of the size WindowSize whose top left corner is (i,j)

function [histArray] = localHistograms (img,windowSize)
histArray = zeros(windowSize(1),WindowSize(2),256);
for i = 1:windowSize(1)
    for j = 1:windowSize(2)
        histArray(i,j,:) = histImage(img( i:windowSize(1), j:windowSize(2) ))
    end
end
end     

这是我到目前为止所拥有的,你能告诉我我的错误吗?我如何检查我的错误?只需输入一些随机图像?

4

1 回答 1

0

好的,所以我的朋友帮我找出我的错误是工作和测试的代码:

function [ histArray  ] = localHistograms ( img,windowSize )
% Given an image returns a 3D array of histograms – 
% one histogram per window in image.
% Input:
%   img - a grayscale image in the range [0..255]
%   windowSize – a 1x2 array [r c]  defining the num rows and num cols 
%                of image windows.      
% Output:
%   histArray – an NxMx256 array.
%
% For every (i,j) the 1D array histArray(i,j,:) is the histogram
% of an image window of size windowSize whose top left corner 
% pixel is (i,j). Histograms of windows that exceed the boundary 
% of the of img are not included in histArray (thus N = number of 
% rows of img less the number of rows of window +1. 
% Similarly  M=size(img,2)-windowSize(2)+1 ).
%
% Method:   Scans the img pixel by pixel. For each scanned pixel,
% determines the histogram of the image window starting at the 
% pixel and extending  windowSize(1) rows and windowSize(2).

N = size(img,1) - windowSize(1) + 1;
M = size(img,2) - windowSize(2) + 1;
histArray = zeros(N ,M,256);

for i = 1:N
 for j = 1:M
   histArray(i,j,:) = histImage(img(i:i+windowSize(1)-1,j:j+windowSize(2)-1));
 end
end

end
于 2012-11-13T23:00:41.503 回答