【问题标题】:MATLAB: Display image inside circleMATLAB:在圆圈内显示图像
【发布时间】:2014-10-27 04:35:17
【问题描述】:

作为圆识别程序的一部分,我有一个背景图像,其中包含已知坐标和半径的几何圆。我希望圆圈的内侧部分被图像填充,而外侧则保持不变。我最好的想法是某种圆形面具,但我不确定这是最好的方法。有什么建议吗?

X = imread('X.jpg'); % Background image jpg
Y = imread('Y.jpg'); % Filling image jpg
cent = [100,100]; % Center of circle
rad = 20; % Circle radius

% Fill circle ?
...

出于保密原因,我没有提供扩展代码。

【问题讨论】:

  • 图片Y的填充尺寸和圆圈一样吗?是否要将整个图像 Y 填充在 X 内的圆圈内?

标签: matlab image-processing shapes image-recognition


【解决方案1】:


我认为困难的部分是由作者完成的:http://matlab.wikia.com/wiki/FAQ#How_do_I_create_a_circle.3F

假设:

  • 我假设您不会指定超出图像范围的点(即我不会在此处添加验证)。
  • 我使用背景图像将圆的“中心”与坐标相关联。
  • 我假设半径以像素为单位。
  • 我没有创建具有已知半径圆的背景图像,因为我认为创建您正在寻找的填充效果没有必要(除非我遗漏了什么)。

代码:

X = imread('rdel_x.png'); % Background image jpg (I used a random image but this can be your blank + geometric circle)
Y = imread('rdel_y.png'); % Filling image jpg
cent = [100,150]; % Center of circle
rad = 70; % Circle radius

% make a mesh grid to provide coords for the circle (mask)
% taken from http://matlab.wikia.com/wiki/FAQ#How_do_I_create_a_circle.3F
[columnsInImage rowsInImage] = meshgrid(1:size(X,2), 1:size(X,1));

% circle points in pixels:
circlePixels = (rowsInImage - cent(1)).^2 ...
    + (columnsInImage - cent(2)).^2 <= rad.^2;
circlePixels3d=repmat(circlePixels,[1 1 3]); % turn into 3 channel (assuming X and Y are RGB)


X((circlePixels3d)) = Y((circlePixels3d)); % assign the filling image pixels to the background image for pixels where it's the desired circle
imagesc(X);
axis image off

结果:从左到右,背景图,填充图,上面代码的结果。

编辑:如果所有内容都封装在您的坐标中,您甚至可能不需要背景图像。例如尝试将其附加到上面的代码中...

Z=zeros(size(X),'uint8'); % same size as your background
Z(circlePixels3d) = Y(circlePixels3d);
figure; % new fig
imagesc(Z);
axis image off

【讨论】:

  • 干得好!我已经挣扎了半个小时没有得到任何 +1
  • 非常好。我会采取同样的方法,但我在工作,无法写出答案:(。+1。
  • 谢谢你,@cthr。这是我完成这项工作所需的输入。非常感谢!
猜你喜欢
  • 2015-09-03
  • 1970-01-01
  • 1970-01-01
  • 2017-11-06
  • 2015-06-09
  • 2016-10-07
  • 2013-12-03
  • 2011-12-06
相关资源
最近更新 更多