Circular region in an image

조회 수: 3 (최근 30일)
Sordin
Sordin 2018년 1월 15일
댓글: Image Analyst 2018년 7월 18일
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:
I=imread('office_1.jpg');
I2=imread('office_5.jpg');
[imageSizeY, imageSizeX,z1] = size(I);
centerX = ceil(imageSizeX/2);
centerY = ceil(imageSizeY/2);
radius = 200;
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
Matt J 2018년 1월 15일
편집: 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);
map=(yy.^2+xx.^2<=radius^2);
I(map)=I2(map);
  댓글 수: 3
Image Analyst
Image Analyst 2018년 1월 16일
편집: Image Analyst 2018년 1월 16일
Does this work for his color image?
Sordin
Sordin 2018년 1월 16일
Hi Image Analyst, Yes this worked for the color images as well.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2018년 1월 15일
See my attached copy and paste demos.

Prince Masedi
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
Image Analyst 2018년 7월 18일
How does this answer the original question about pasting circular regions into an image?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by