# Circular region in an image

2 次查看（过去 30 天）
Sordin 2018-1-15

I am trying to replace a circular region in one image with the corresponding region in another image. Here is my code so far:
% Sample images:
[imageSizeY, imageSizeX,z1] = size(I);
centerX = ceil(imageSizeX/2);
centerY = ceil(imageSizeY/2);
for i=1:max(imageSizeY,imageSizeX)
if (((centerY+i) - centerY).^2 ...
+ ((centerX+i) - centerX).^2 <= radius.^2)
I(centerY:centerY+i, centerX:centerX+i, :) ...
= I2(centerY:centerY+i, centerX:centerX+i, :);
end
end
imshow(I)
Unfortunately, my code does not work correctly, and instead of a circle the result is a square (corresponding to only one quadrant of the desired circle): What are the mistakes here? Any explanation is greatly appreciated.

### 采纳的回答

Matt J 2018-1-15

I don't really follow the logic of your code, but in any case there are much simpler and more efficient approaches, e.g.,
[yy,xx,~]=ndgrid( (1:imageSizeY)-centerY, (1:imageSizeX)-centerX ,1:z1);
I(map)=I2(map);
##### 3 个评论显示 2更早的评论隐藏 2更早的评论
Sordin 2018-1-16
Hi Image Analyst, Yes this worked for the color images as well.

### 更多回答（2 个）

Image Analyst 2018-1-15
See my attached copy and paste demos.
##### 0 个评论显示 -1更早的评论隐藏 -1更早的评论

Prince Masedi 2018-7-18
II. Determine the following statistical parameter using Matlab program (Note you must copy and paste your code on the answer booklet. (1. Error variance, 2. Assume X=Rainfall Y=Runoff fill in the table with respective determined square of (X-Xm) where Xm is the mean of X and determine estimated Y termed as Yest.
##### 1 个评论显示 无隐藏 无
Image Analyst 2018-7-18
How does this answer the original question about pasting circular regions into an image?

### 类别

Help CenterFile Exchange 中查找有关 Image Arithmetic 的更多信息

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!