创建循环ROI并考虑部分像素值

时间:2016-02-19 16:42:19

标签: matlab image-processing matlab-figure

我有一张低分辨率的图像,128x128像素。我需要获得圆形ROI的平均值,为了做到这一点,我使用简单的方法:

%% Draw circle ROI 
t = 0:pi/500:2*pi;
xi = ((R0/pixelSize)*cos(t)+63.5+x0+((Rsphere)/pixelSize)*cos(theta))*4;
yi = ((R0/pixelSize)*sin(t)+63.5+y0+((Rsphere)/pixelSize)*sin(theta))*4;

%% Calculate roi statistics
line(xi,yi,'LineWidth',1,'Color',color);
ROImask = poly2mask(xi,yi, size(im,1),size(im,2));
ptROI = find(ROImask);
ROImean = mean(im(ptROI));

这里的问题是使用这种方法我不会考虑ROI中像素的部分值,如图中所示。

有没有直接的方法来获得加权像素值的ROI的平均值?

由于

Artististic impression of the problem

1 个答案:

答案 0 :(得分:1)

如果你真的想要这样做,你需要做一些微积分(每个像素的方形域上的圆的积分)。但是,这可能对你的应用程序来说太过分了。我的建议是在精细网格上计算你的圆圈,然后调整它以匹配图像:

upFactor = 3;

% load built-in example image
x = imread('rice.png');

% convert to double
x = im2double(x);

% define the ROI
center = [68.5, 180]; % [row, column]
radius = 1; % pixels

% do the distance calculation
% (getting the coordinate systems to match is the hardest part, try making
% small examples to see how it works)
iVector = (0:size(x,1)*upFactor-1)/upFactor + .5 + 1/upFactor/2;
jVector = (0:size(x,2)*upFactor-1)/upFactor + .5 + 1/upFactor/2;
[I, J] = ndgrid( iVector - center(1), jVector - center(2));
sqDist = I.^2 + J.^2;
insideBig = double(sqDist <= radius^2); % need this to not be logical type

% this resizes back to the original image size my taking the mean of each
% upFactor by upFactor block
inside = reshape( mean( im2col( insideBig, [upFactor upFactor], 'distinct')), size(x));

% check that we have the values we expect
uniqueVals = unique(inside(:))

% show examples
figure
imagesc(x)

figure
imagesc(inside)

result =  sum(sum( x .* inside )) / sum(inside(:))