[英]How do I add an image to a frame in MATLAB?
我有以下問題說明:
創建帶有黑色(0)邊框的(400 x 400)位圖白色(255)圖像,以模擬“相框”。 框架的四邊距離應為10個單位。 將此圖像另存為“ ImageFrame.bmp”。 現在,將“ Parrot.png”插入“相框”。 找到鸚鵡圖片的中心和框架的中心,然后放置鸚鵡。
我制作了相框,但是我不知道如何將鸚鵡添加到“相框”中。我們將不勝感激! 鸚鵡圖片是200 X 150。
以下是我嘗試解決此問題的代碼
ImageParrot = imread('Parrot.png','png');
ImageBlank = uint8(zeros(200,150))
ImageBlank = 255 * uint8(ones(400,400))
imshow(ImageBlank)
ImageFrame = ImageBlank
ImageFrame(10:390,10) = 0;
ImageFrame(10,10:390) = 0;
ImageFrame(10:390,390) = 0;
ImageFrame(390,10:390) = 0;
imshow(ImageFrame)
imwrite(ImageFrame,'ImageFrame.bmp')
imshow(ImageParrot)
ImageParrotLarge = imresize(ImageParrot,2)
imshow(ImageParrotLarge)
ImageParrotRotate = imrotate(ImageParrot,90)
ImageParrotRotate = imrotate(ImageParrot,45)
imshow(ImageParrotRotate)
ImageParrotStretched = imagesc(400,400,ImageParrot)
ImageNewParrot = 255 * uint8(ones(400,400));
ImageNewParrot(40:end,40:end) = ImageParrot
imadd(ImageFrame,ImageParrot)
您已經回答了該問題的前兩部分:
假設圖像是灰度的 ,您可以將圖像插入框架的中央,如下所示:
% // Get the centre of the picture frame
[rowsFrame, colsFrame] = size(ImageFrame);
picFrameCenRow = rowsFrame / 2;
picFrameCenCol = colsFrame / 2;
% // Get dimensions of parrot image
[rowsParrot, colsParrot] = size(ImageParrot);
% // Place the parrot in the centre
ImageFrame(picFrameCenRow-(rowsParrot/2) : picFrameCenRow+(rowsParrot/2)-1, ...
picFrameCenCol-(colsParrot/2) : picFrameCenCol+(colsParrot/2)-1) = ImageParrot;
這會將鸚鵡圖像放置在ImageFrame
的中心, ImageFrame
存儲在ImageFrame
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.