global OptionStruct;
global DataStruct;
OptionStruct.isMapSelectable = 1;
mySelectionResetFcn(OptionStruct, handles); %调用子程序脚本
OptionStruct.isAreaMarked = 0;
impixelinfo; %显示图片像素信息
button = questdlg('现在选取两个坐标表点吗?','问题提示') %创建一个指定提问内容和窗口标题的提问
questAnswer = ('Yes');
if button == questAnswer
[x1,y1] = ginput(1) %获取图像像素坐标(左上),来自鼠标或光标的图形输入
[x2,y2] = ginput(1) %获取图像像素坐标(右下),来自鼠标或光标的图形输入
DataStruct.startPt = [x1,y1];
DataStruct.endPt = [x2,y2];
end
global OptionStruct;
global DataStruct;
OptionStruct.isMapSelectable = 0;
startPoint = DataStruct.startPt;
endPoint = DataStruct.endPt;
tempPoint = [startPoint,endPoint];
x1 = tempPoint(1); y1 = tempPoint(2);
x2 = tempPoint(3); y2 = tempPoint(4);
startPoint(1,1)= x1;endPoint(1,1)=x2;
startPoint(1,2)= y1;endPoint(1,2)=y2;
myRectangleFcn(startPoint, endPoint); %调用绘制红矩形的子程序
function mySelectionResetFcn(OptionStruct, handles)
OptionStruct.im_WM = imread('USA.PNG'); %保存结构体
axes(handles.axesWorldMap); %设定显示的坐标区
imshow(OptionStruct.im_WM); %显示图片
end
function myRectangleFcn(startPoint, endPoint)
lineWidth = 2;
inner = lineWidth - 1;
x0 = startPoint(1, 1);
x1 = endPoint(1, 1);
y0 = startPoint(1, 2);
y1 = endPoint(1, 2);
if x0 < x1
x_min = x0;
x_max = x1;
else
x_min = x1;
x_max = x0;
end
if y0 < y1
y_min = y0;
y_max = y1;
else
y_min = y1;
y_max = y0;
end
tl2bl_x = [x_min + inner, x_min + inner];
tl2bl_y = [y_min, y_max];
tl2tr_x = [x_min, x_max];
tl2tr_y = [y_min + inner, y_min + inner];
tr2br_x = [x_max - inner, x_max - inner];
tr2br_y = [y_min, y_max];
bl2br_x = [x_min, x_max];
bl2br_y = [y_max- inner, y_max - inner];
hold on;
h_tl2bl = plot(tl2bl_x, tl2bl_y, '-r', 'LineWidth', lineWidth);
h_tl2tr = plot(tl2tr_x, tl2tr_y, '-r', 'LineWidth', lineWidth);
h_tr2br = plot(tr2br_x, tr2br_y, '-r', 'LineWidth', lineWidth);
h_bl2br = plot(bl2br_x, bl2br_y, '-r', 'LineWidth', lineWidth);
hold off;
end