gpt4 book ai didi

matlab - 创建圆形 ROI 并考虑部分像素值

转载 作者:行者123 更新时间:2023-12-02 03:17:52 25 4
gpt4 key购买 nike

我有一张低分辨率的图片,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

最佳答案

如果您真的想精确地执行此操作,则需要进行一些微积分(每个像素在正方形域上的圆的积分)。但是,这对您的应用程序来说可能有点矫枉过正。我的建议是在精细网格上计算你的圆,然后调整它的大小以匹配图像:

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(:))

关于matlab - 创建圆形 ROI 并考虑部分像素值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35510355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com