MATLAB将一行中的多个图像粘贴到另一个图像上?

时间:2013-12-02 00:53:41

标签: image matlab pasting

我正在尝试将多个较小的图像(9个不同大小的矩形)粘贴到黑色背景上。这些图像中的每一个的中心水平线应该对齐,并且所有边缘都应该接触。只要它们如所描述的那样对齐就粘贴在黑色的哪个位置并不重要。我会附上一些我的意思。

较小图像的示例 http://i.imgur.com/Dlu6es4.png

期望的结果 http://i.imgur.com/ujuhCWs.png

ImageAnalyst提供了这个用于粘贴图像的优秀代码,但我认为我需要的是更复杂一点。如果有人可以帮助或指出我的方向,我将非常感激。非常感谢!

if true
% % Lets user drag out a box on an image, then define where they want to paste it.
% Then it pastes the drawn region onto the original image.
% Figure out how to select two points for pasting
clc;    % Clear the command window.
close all;  % Close all figures (except those of imtool.)
imtool close all;  % Close all imtool figures.
clear;  % Erase all existing variables.
workspace;  % Make sure the workspace panel is showing.
fontSize = 20;
format compact;
grayImage = imread('Blackout.png');
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Display the cropped image.
rotatedImage=imread('rotatedImage.png');
subplot(2, 2, 3);
imshow(rotatedImage);
axis on;
title('Region that you defined', 'FontSize', fontSize);
% Paste it onto the original image
[rows2 columns2] = size(rotatedImage)
promptMessage = sprintf('Click on the upper left point where you want to paste it,\nor                 Cancel to abort processing?'); 
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
return;
end
[x, y] = ginput(1)%pick 1 two-dimensional points from the figure and returns x y     coordinates.
% Determine the pasting boundaries.
r1 = int32(y);
c1 = int32(x);
r2 = r1 + rows2 - 1;
r2 = min([r2 rows]);
c2 = c1 + columns2 - 1;
c2 = min([c2, columns]);
plot([c1 c2 c2 c1 c1], [r1 r1 r2 r2 r1], 'r-');
% Paste as much of croppedImage as will fit into the original image.
grayImage(r1:r2, c1:c2) = rotatedImage(1:(r2-r1+1), 1:(c2-c1+1));
subplot(2, 2, 4);
imshow(grayImage);
axis on;
title('Region that you defined pasted onto original', 'FontSize', fontSize);
%save rotated image
imwrite(grayImage, 'alignedImage.png');

1 个答案:

答案 0 :(得分:1)

实际上,我想我已经弄明白了!只需使用每个图像的行大小来确定下一个图像的位置。这有点乱,但我认为这是一个好的开始:)

% Lets user drag out a box on an image, then define where they want to paste it.
% Then it pastes the drawn region onto the original image.
% Figure out how to select two points for pasting
clc;    % Clear the command window.
close all;  % Close all figures (except those of imtool.)
imtool close all;  % Close all imtool figures.
clear;  % Erase all existing variables.
workspace;  % Make sure the workspace panel is showing.
fontSize = 20;
format compact;
%%
grayImage = imread('Blackout.png');
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[I1R I1C numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(1, 3, 1);
imshow(grayImage);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
%%
% Display the cropped image.
rotatedImage=imread('rotCoxa.png');
subplot(1, 3, 2);
imshow(rotatedImage);
axis on;
title('Region that you defined', 'FontSize', fontSize);
% Paste it onto the original image
[I2R I2C] = size(rotatedImage);
%%
x = 1;
y = I1R/2;
% Determine the pasting boundaries.
r1 = int32(y);
c1 = int32(x);
r2 = r1 + I2R - 1;
r2 = min([r2 I1R]);
c2 = c1 + I2C - 1;
c2 = min([c2, I1R]);
plot([c1 c2 c2 c1 c1], [r1 r1 r2 r2 r1], 'r-');
% Paste as much of croppedImage as will fit into the original image.
grayImage(r1:r2, c1:c2) = rotatedImage(1:(r2-r1+1), 1:(c2-c1+1));
subplot(1, 3, 3);
imshow(grayImage);
axis on;
title('Region that you defined pasted onto original', 'FontSize', fontSize);
%%
%save rotated image
imwrite(grayImage, 'alignedImage.png');
%%
clc;    % Clear the command window.
close all;  % Close all figures (except those of imtool.)
imtool close all;  % Close all imtool figures.
clear;  % Erase all existing variables.
workspace;  % Make sure the workspace panel is showing.
fontSize = 20;
format compact;
%%
%delete('Blackout.png');
grayImage = imread('alignedImage.png');
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[I1R I1C numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(1, 3, 1);
imshow(grayImage);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
%%
% Display the cropped image.
rotatedImage=imread('rotTrochanter.png');
subplot(1, 3, 2);
imshow(rotatedImage);
axis on;
title('Region that you defined', 'FontSize', fontSize);
% Paste it onto the original image
[I3R I3C] = size(rotatedImage);
%%
previouslyrotatedImage=imread('rotCoxa.png');
[I2R I2C] = size(previouslyrotatedImage);
x = I2C;
y = I1R/2 + I2R/2 - I3R; %check math. I3R/2 or I3R?
% Determine the pasting boundaries.
r1 = int32(y);
c1 = int32(x);
r2 = r1 + I3R - 1;
r2 = min([r2 I1R]);
c2 = c1 + I3C - 1;
c2 = min([c2, I1C]);
plot([c1 c2 c2 c1 c1], [r1 r1 r2 r2 r1], 'r-');
% Paste as much of croppedImage as will fit into the original image.
grayImage(r1:r2, c1:c2) = rotatedImage(1:(r2-r1+1), 1:(c2-c1+1));
subplot(1, 3, 3);
imshow(grayImage);
axis on;
title('Region that you defined pasted onto original', 'FontSize', fontSize);
%%
%save rotated image
imwrite(grayImage, 'alignedImage.png');
相关问题