I want to cut/crop an RGB image and paste it on different location in another image

조회 수: 6 (최근 30일)
Hi, I would like to cut an RGB image and then paste it on another RGB image at different location.

답변 (1개)

Wan Ji
Wan Ji 2021년 8월 25일
I will show you how to copy Part of image A to a certain position of image B
clc;clear
A = imread('Lena.jpg');
A = imresize(A,[200,200]);
imshow(A)
B = uint8(ones(600,600,3)*255); % create an empty image to get the copy from A
figure(1); clf; imshow(B)
title('Empty white image: the target image')
% copy Part of image A to image B
copyLen = 160; copyWid = 120;
copyPosAx = 11; copyPosAy = 31; % the pos coordinate
copyPosBx = 21; copyPosBy = 21;
% copy for the first time
B = imcopy(A, B, [copyPosAx, copyPosAy], [copyPosBx, copyPosBy], [copyLen, copyWid]);
figure(2); clf; imshow(B); title('After copy once')
copyPosBx = 201; copyPosBy = 201; % change the copy position of image B
% copy for the second time
B = imcopy(A, B, [copyPosAx, copyPosAy], [copyPosBx, copyPosBy], [copyLen, copyWid]);
figure(3); clf; imshow(B); title('After copy twice')
copyPosBx = 391; copyPosBy = 301; % change the copy position of image B
% copy for the third time
B = imcopy(A, B, [copyPosAx, copyPosAy], [copyPosBx, copyPosBy], [copyLen, copyWid]);
figure(4); clf; imshow(B); title('After copy third time')
The imcopy function is used for three times, after that, you can see how Lena's profile show in a white image with 3 times
Wish you like it.
  댓글 수: 1
Jenifer NG
Jenifer NG 2022년 5월 18일
I have an error with imcopy when my position is not interger number
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in imcopy (line 13)
B(Bx, By, :) = A(Ax, Ay, :); % copy part if A to B

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

카테고리

Help CenterFile Exchange에서 Read, Write, and Modify Image에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by