MATLAB Answers

Crop Code Loop End with a key Press

4 views (last 30 days)
Murat Kocaman
Murat Kocaman on 30 Nov 2018
Answered: Geoff Hayes on 30 Nov 2018
close all;
imtool close all; % Close all imtool figures.
format longg;
format compact;
fontSize = 20;
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = 'C:\';
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.jpg');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
fullFileName = fullfile(folder, baseFileName)
original = imread(fullFileName);
grayImage = original;
% Get the dimensions of the image. %LOOP STARTS%
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
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','Create an Object','numbertitle','off')
% Ask user to draw a box.
promptMessage = sprintf('Drag out a box on the region that you want to copy,\nor Cancel to abort processing?');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
k = waitforbuttonpress;
point1 = get(gca,'CurrentPoint');
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);
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')
[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));
axis on;
title('Cropped and duplicated object', 'FontSize', fontSize); %LOOP ENDS%
I would like to impliment above code on my study but I need to end while loop in control. This means I need to start and stop it with a keyboard press or similar approach.
Any comment on this issue would be appreciated.

Answers (1)

Geoff Hayes
Geoff Hayes on 30 Nov 2018
Murat - you could use the WindowKeyPressFcn callback for your figure to exit the loop if a certain key is pressed. For example, in the below code we continue to update the plot until the 'q' character is pressed
function KeyPressTest
hFig = figure;
hPlot = plot(NaN,NaN);
k = 1;
xData = [];
yData = [];
set(hFig, 'WindowKeyPressFcn', @OnKeyPressEvent);
keepGoing = true;
while keepGoing
xData = [xData ; k];
yData = [yData ; k];
set(hPlot, 'XData', xData, 'YData', yData);
k = k + 1;
function OnKeyPressEvent(source, data)
if strcmp(data.Key, 'q')
keepGoing = false;


Community Treasure Hunt

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

Start Hunting!