Crop Code Loop End with a key Press

2 views (last 30 days)
Murat Kocaman
Murat Kocaman on 30 Nov 2018
Answered: Geoff Hayes on 30 Nov 2018
clc;
clearvars;
close all;
imtool close all; % Close all imtool figures.
workspace;
format longg;
format compact;
fontSize = 20;
tic;
% 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;
end
% 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.
return;
end
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.
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','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')
return;
end
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);
imshow(croppedImage);
axis on;
title('Region that you defined', 'FontSize', fontSize);
while(1)
% 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));
imshow(grayImage);
axis on;
title('Cropped and duplicated object', 'FontSize', fontSize); %LOOP ENDS%
end
Hello,
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;
drawnow;
end
function OnKeyPressEvent(source, data)
if strcmp(data.Key, 'q')
keepGoing = false;
end
end
end

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!