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

3 ビュー (過去 30 日間)
Abhishek Shah Alias Sangani
Abhishek Shah Alias Sangani 2021 年 8 月 25 日
コメント済み: Jenifer NG 2022 年 5 月 18 日
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 Center および File ExchangeImages についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by