How to define a blank frame and paste an image of smaller size into it?

How to define a blank frame and paste an image of smaller size into it? By example if the blank frame is 200 X 200, an image of reduced size 75 X 75 can be pasted within that blank frame.

 Respuesta aceptada

Image Analyst
Image Analyst el 9 de Feb. de 2013
Editada: Image Analyst el 9 de Feb. de 2013
See my demo below. I made it up just a few days ago for someone who asked the same thing. Oh wait - that was you! http://www.mathworks.com/matlabcentral/answers/62507-how-to-add-an-additional-object-to-an-image Is there any reason why you're not responding to people in the discussions you start? And even deleting your comments that I responded to? Or why you're not marking any answers as Accepted, and apparently just ignoring them? Please try to work on being a better Answers citizen.
% Lets user drag out a box on an image, then define where they want to paste it.
% Then is pastes the drawn region onto the original image.
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;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'eight.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% 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]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Let's compute and display the histogram, just for fun.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Ask user to draw a box.
subplot(2, 2, 1);
promptMessage = sprintf('Drag out a box that you want to copy,\nor Cancel to abort processing?');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
return;
end
k = waitforbuttonpress;
point1 = get(gca,'CurrentPoint'); % button down detected
finalRect = rbbox; % return figure units
point2 = get(gca,'CurrentPoint'); % button up detected
point1 = point1(1,1:2); % extract x and y
point2 = point2(1,1:2);
p1 = min(point1,point2); % calculate locations
offset = abs(point1-point2); % and dimensions
% Find the coordinates of the box.
xCoords = [p1(1) p1(1)+offset(1) p1(1)+offset(1) p1(1) p1(1)];
yCoords = [p1(2) p1(2) p1(2)+offset(2) p1(2)+offset(2) p1(2)];
x1 = round(xCoords(1));
x2 = round(xCoords(2));
y1 = round(yCoords(5));
y2 = round(yCoords(3));
hold on
axis manual
plot(xCoords, yCoords, 'b-'); % redraw in dataspace units
% Display the cropped image.
croppedImage = grayImage(y1:y2,x1:x2);
subplot(2, 2, 3);
imshow(croppedImage);
axis on;
title('Region that you defined', 'FontSize', fontSize);
% Paste it onto the original image
[rows2 columns2] = size(croppedImage)
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)
% 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) = croppedImage(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);

20 comentarios

This question is not about a cropped image at all. It is to define a blank frame and use any image using imread() that will be reduced in sized and pasted to that blank frame. I did accept all your previous answers.
Send me an email address where I can send you a pic of the design I want done.
Cesar
Cesar el 9 de Feb. de 2013
Editada: Image Analyst el 10 de Feb. de 2013
here is the link to the design needed:
any update after seeing the design?
Just got back from skiing - I'll take a look tomorrow.
Thanks.
There is nothing in the document you uploaded. try it yourself.
You should see a blank frame that included copies of 2 images of smaller size. But only the diagram instead of a real picture
Should, but don't. I invite you to try it. The two small images just say image1 and image2 - there is no image whatsoever.
Exactly that the final design needed...you can consider image1 as a bird.jpg and image2 as dog.jpg and those 2 images needed to be reduced in size and pasted to each side of the blank frame.
But I gave you code for that already. Did you run it? Have you tried to adapt it? For example to the 75x75 size you need?
Please repost the code and I will run.
The previous code does not at all for this question. Completely different scenario. Not even close.
Come on - it is close. Very close. It's the same scenario. You just have to change the sizes to 75 and 200 and paste it onto a blank image instead of the original image. Very easy adaptation - I did it for you, as shown below:
% Lets user drag out a box on an image, then define where they want to paste it.
% Then is pastes the drawn region onto the original image.
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;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'eight.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% 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]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Let's compute and display the histogram, just for fun.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Ask user to draw a box.
subplot(2, 2, 1);
promptMessage = sprintf('Drag out a box that you want to copy,\nor Cancel to abort processing?');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
return;
end
k = waitforbuttonpress;
point1 = get(gca,'CurrentPoint'); % button down detected
finalRect = rbbox; % return figure units
point2 = get(gca,'CurrentPoint'); % button up detected
point1 = point1(1,1:2); % extract x and y
point2 = point2(1,1:2);
p1 = min(point1,point2); % calculate locations
offset = abs(point1-point2); % and dimensions
% Find the coordinates of the box.
xCoords = [p1(1) p1(1)+offset(1) p1(1)+offset(1) p1(1) p1(1)];
yCoords = [p1(2) p1(2) p1(2)+offset(2) p1(2)+offset(2) p1(2)];
x1 = round(xCoords(1));
x2 = round(xCoords(2));
y1 = round(yCoords(5));
y2 = round(yCoords(3));
hold on
axis manual
plot(xCoords, yCoords, 'b-'); % redraw in dataspace units
% Display the cropped image.
croppedImage = grayImage(y1:y2,x1:x2);
subplot(2, 2, 3);
imshow(croppedImage);
axis on;
title('Region that you defined', 'FontSize', fontSize);
% Resize the image to 75 by 75.
croppedImage75 = imresize(croppedImage, [75, 75]);
[rowsSource columnsSource] = size(croppedImage75) % Should be 75x75
uiwait(helpdlg('Now we will resize this cropped image to be 75 by 75'));
subplot(2, 2, 3);
imshow(croppedImage75);
axis on;
title('Region that you defined as 75 x 75', 'FontSize', fontSize);
% Paste it onto a 200x200 blank image
destinationImage = zeros(200,200,'uint8');
[rowsDest columnsDest] = size(destinationImage)
subplot(2, 2, 4);
imshow(destinationImage);
axis on;
title('Blank 200x200 image', 'FontSize', fontSize);
promptMessage = sprintf('In the lower left black image,\nclick 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)
% Determine the pasting boundaries.
r1 = int32(y);
c1 = int32(x);
r2 = r1 + rowsSource - 1;
r2 = min([r2 rowsDest]);
c2 = c1 + columnsSource - 1;
c2 = min([c2, columnsDest]);
hold on;
plot([c1 c2 c2 c1 c1], [r1 r1 r2 r2 r1], 'r-');
% Paste as much of croppedImage as will fit into the original image.
destinationImage(r1:r2, c1:c2) = croppedImage75(1:(r2-r1+1), 1:(c2-c1+1));
subplot(2, 2, 4);
imshow(destinationImage);
axis on;
title('Region that you defined pasted onto original', 'FontSize', fontSize);
I did run the new code. Remember two images must be pasted or copied to the blank frame. Your code doesn't any of that.
By the way, no cropping. The 2 images must be defined as smaller size and pasted to the blank frame.
My code pastes one image into a blank frame. The modification to paste two or more 75x75 images onto the same blank frame is straightforward. You don't really need me to show you that trivial step do you?
I don't care where you get your images. My code asks you to indicate a box in a source image and it resizes that block into a 75x75 block. If you want to read it in from an image file then just use imread(). If you obtain it some other way, it doesn't matter. I really don't care where or how you get the image that you resize to 75x75. It doesn't matter - just replace that code with your code that gives you the 75x75 image.
Thanks. There is a simpler, faster, shorter, easier way to solve this.
I seriously doubt that there is a faster, shorter, easier way to paste the image than the single line I suggested:
grayImage(r1:r2, c1:c2) = your75x75Image;
I'd have to see it to believe it. Don't get confused by all the fancy stuff I put into the demo like displaying images, showing axis tick marks, and putting up titles. That's all just stuff to make the demo look nice - it's optional. The main part is just that one line.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Convert Image Type en Centro de ayuda y File Exchange.

Preguntada:

el 9 de Feb. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by