I have a image in my gui with added gridlines. I need be able to to create and save a text file with the N by M dimensions of the graph containing the contents of each box in the grid.
我在我的gui中添加了一个带有网格线的图像。我需要能够使用包含网格中每个框内容的图的N×M维创建和保存文本文件。
Is there a way to do this?
有没有办法做到这一点?
1 个解决方案
#1
0
Here is an example.
这是一个例子。
Main file
This should be the main script.
这应该是主要的脚本。
Grid_Size = [5 5];
Grid_Type = 1; % The group of the new clicked points.
% Initialize
Grid_Matrix = zeros(Grid_Size);
grid_h = []; % Grid image handle
fig_h = figure('Units', 'Normalized');
% Show your image
rgb = imread('peppers.png');
im_h = imshow(rgb);
% Shut off the 'HitTest' to keep the functionality of figure ButtonDownFcn
set(im_h, 'HitTest', 'Off');
hold on % Hold on for the overlaying grid image.
% Image normalized position
Image_Pos = get(ancestor(im_h, 'axes'), 'Position');
im_size = size(rgb(:,:,1));
% Set the Mouse click callback for the figure (to a script in this case)
set(fig_h, 'ButtonDownFcn', 'Mouse_Click');
Mouse_Click.m
The script callback, I suggest script rather than function to avoid the complexity of scope and etc.
脚本回调,我建议脚本而不是功能,以避免范围的复杂性等。
Clicked = fliplr((get(fig_h, 'CurrentPoint') - Image_Pos(1:2))./Image_Pos(3:4));
if ~sum(Clicked < 0) && ~sum(Clicked > 1) % If inside of the image is clicked
% Find the corresponding matrix index
ind = num2cell(ceil(Clicked.*Grid_Size));
% and toggle that element
if Grid_Matrix(ind{:}) == 0
Grid_Matrix(ind{:}) = Grid_Type;
else
Grid_Matrix(ind{:}) = 0;
end
% Show the result to user
delete(grid_h); % delete the previous grid image
% resize the Grid Matrix to the image size
Grid_Matrix_rs = imresize(flipud(Grid_Matrix), im_size, 'box');
Grid_Matrix_rgb = label2rgb(Grid_Matrix_rs, 'spring', 'b');
grid_h = imshow(Grid_Matrix_rgb);
set(grid_h, 'HitTest', 'Off');
set(grid_h, 'AlphaData', 0.3); % Show the grid image with an opacity
% Write data to file after each update (user click)
dlmwrite('GridBox.dat', flipud(Grid_Matrix));
end
Note that flipud
is used to flip the matrix to match the image indexing.
请注意,flipud用于翻转矩阵以匹配图像索引。
During clicking you can change Grid_Type
(Using a UI or in command window) to another integer so that you get another color. However the previous colors might change, but finally all the same colors have the same index inside the Grid_Matrix
.
在单击期间,您可以将Grid_Type(使用UI或在命令窗口中)更改为另一个整数,以便获得另一种颜色。但是以前的颜色可能会改变,但最终所有相同的颜色在Grid_Matrix中都有相同的索引。
You will get something like:
你会得到类似的东西:
Corresponding to this Grid_Matrix
:
对应这个Grid_Matrix:
4,0,3,0,2
0,1,0,0,0
3,0,1,0,4
2,0,2,1,0
0,0,3,0,0
To Read the result from file
从文件中读取结果
res = dlmread('GridBox.dat')
#1
0
Here is an example.
这是一个例子。
Main file
This should be the main script.
这应该是主要的脚本。
Grid_Size = [5 5];
Grid_Type = 1; % The group of the new clicked points.
% Initialize
Grid_Matrix = zeros(Grid_Size);
grid_h = []; % Grid image handle
fig_h = figure('Units', 'Normalized');
% Show your image
rgb = imread('peppers.png');
im_h = imshow(rgb);
% Shut off the 'HitTest' to keep the functionality of figure ButtonDownFcn
set(im_h, 'HitTest', 'Off');
hold on % Hold on for the overlaying grid image.
% Image normalized position
Image_Pos = get(ancestor(im_h, 'axes'), 'Position');
im_size = size(rgb(:,:,1));
% Set the Mouse click callback for the figure (to a script in this case)
set(fig_h, 'ButtonDownFcn', 'Mouse_Click');
Mouse_Click.m
The script callback, I suggest script rather than function to avoid the complexity of scope and etc.
脚本回调,我建议脚本而不是功能,以避免范围的复杂性等。
Clicked = fliplr((get(fig_h, 'CurrentPoint') - Image_Pos(1:2))./Image_Pos(3:4));
if ~sum(Clicked < 0) && ~sum(Clicked > 1) % If inside of the image is clicked
% Find the corresponding matrix index
ind = num2cell(ceil(Clicked.*Grid_Size));
% and toggle that element
if Grid_Matrix(ind{:}) == 0
Grid_Matrix(ind{:}) = Grid_Type;
else
Grid_Matrix(ind{:}) = 0;
end
% Show the result to user
delete(grid_h); % delete the previous grid image
% resize the Grid Matrix to the image size
Grid_Matrix_rs = imresize(flipud(Grid_Matrix), im_size, 'box');
Grid_Matrix_rgb = label2rgb(Grid_Matrix_rs, 'spring', 'b');
grid_h = imshow(Grid_Matrix_rgb);
set(grid_h, 'HitTest', 'Off');
set(grid_h, 'AlphaData', 0.3); % Show the grid image with an opacity
% Write data to file after each update (user click)
dlmwrite('GridBox.dat', flipud(Grid_Matrix));
end
Note that flipud
is used to flip the matrix to match the image indexing.
请注意,flipud用于翻转矩阵以匹配图像索引。
During clicking you can change Grid_Type
(Using a UI or in command window) to another integer so that you get another color. However the previous colors might change, but finally all the same colors have the same index inside the Grid_Matrix
.
在单击期间,您可以将Grid_Type(使用UI或在命令窗口中)更改为另一个整数,以便获得另一种颜色。但是以前的颜色可能会改变,但最终所有相同的颜色在Grid_Matrix中都有相同的索引。
You will get something like:
你会得到类似的东西:
Corresponding to this Grid_Matrix
:
对应这个Grid_Matrix:
4,0,3,0,2
0,1,0,0,0
3,0,1,0,4
2,0,2,1,0
0,0,3,0,0
To Read the result from file
从文件中读取结果
res = dlmread('GridBox.dat')