How to define a blank frame and paste an image of smaller size into it?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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.
Accepted Answer
Image Analyst
on 9 Feb 2013
Edited: Image Analyst
on 9 Feb 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 Comments
Cesar
on 9 Feb 2013
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.
Cesar
on 9 Feb 2013
Send me an email address where I can send you a pic of the design I want done.
Image Analyst
on 9 Feb 2013
This is how we share images: http://www.mathworks.com/matlabcentral/answers/7924-where-can-i-upload-images-and-files-for-use-on-matlab-answers
Cesar
on 9 Feb 2013
Edited: Image Analyst
on 10 Feb 2013
here is the link to the design needed:
Cesar
on 9 Feb 2013
any update after seeing the design?
Image Analyst
on 10 Feb 2013
Just got back from skiing - I'll take a look tomorrow.
Cesar
on 10 Feb 2013
Thanks.
Image Analyst
on 10 Feb 2013
There is nothing in the document you uploaded. try it yourself.
Cesar
on 10 Feb 2013
You should see a blank frame that included copies of 2 images of smaller size. But only the diagram instead of a real picture
Image Analyst
on 10 Feb 2013
Should, but don't. I invite you to try it. The two small images just say image1 and image2 - there is no image whatsoever.
Cesar
on 10 Feb 2013
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.
Image Analyst
on 10 Feb 2013
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?
Cesar
on 10 Feb 2013
Please repost the code and I will run.
Cesar
on 10 Feb 2013
The previous code does not at all for this question. Completely different scenario. Not even close.
Image Analyst
on 10 Feb 2013
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);
Cesar
on 10 Feb 2013
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.
Cesar
on 10 Feb 2013
By the way, no cropping. The 2 images must be defined as smaller size and pasted to the blank frame.
Image Analyst
on 10 Feb 2013
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.
Cesar
on 12 Feb 2013
Thanks. There is a simpler, faster, shorter, easier way to solve this.
Image Analyst
on 13 Feb 2013
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.
More Answers (0)
Categories
Find more on Convert Image Type in Help Center and File Exchange
Products
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)