首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用交互式网格在matlab上屏蔽图像?

如何用交互式网格在matlab上屏蔽图像?
EN

Stack Overflow用户
提问于 2017-02-02 13:33:02
回答 2查看 306关注 0票数 0

我正试图在matlab上处理一幅图像,我需要用10x10交互网格覆盖图像。交互式网格将修复使用默认颜色单击的框,并存储单击单元格的位置数据。

到目前为止我的代码:

代码语言:javascript
复制
I = imread('LcmsResult_ImageRng_000280.jpg');
imshow(I) 
hold on
M = size(I,1);
N = size(I,2);
a=10; 
b=10;
for k = 1:a:M
    x = [1 N]; 
    y = [k k];
    plot(x,y,'Color','black','LineStyle','-');
    set(findobj('Tag','MyGrid'),'Visible','on')
end
for k = 1:b:N 
    x = [k k]; 
    y = [1 M];
    plot(x,y,'Color','red','LineStyle','-');
    set(findobj('Tag','MyGrid'),'Visible','on')
end
hold off
[x,y] = ginput(2); 
hold on;
fill( [x-10 x x x-10],[y y y+10 y+10],'g' );

这只是我的第一次尝试,我仍然试图确定解决这个问题的最佳方法,因为我对matlab工具的了解是有限的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-02-02 13:51:16

您需要阅读有关以下内容的文档:

按钮下移功能:plots/button-down-callback-function.html

鼠标点击捕捉:plots/capturing-mouse-clicks.html

补丁程序:https://www.mathworks.com/help/matlab/ref/patch.html

RGB颜色规格:https://www.mathworks.com/help/matlab/ref/colorspec.html

绘图图像:https://www.mathworks.com/help/images/ref/imshow.html

一些示例代码可能是:

代码语言:javascript
复制
figure
imshow('imagename.jpg');
p = patch([0, 10], [0, 10], [1 1 1]);
set(p, 'FaceAlpha', 0); % make patch transparent
set(p, 'ButtonDownFcn', @(~,~)button_down_callback(p),'PickableParts','all');

您需要分别定义button_down_callback函数。

代码语言:javascript
复制
function button_down_callback(p)
    display('Clicked');
    set(p, 'Color', [0.5, 0.9, 0.2], 'FaceAlpha', 0.5); % Change color and set transparency to half
end

我将把图像分割成一个很好的10x10网格的问题留给您作为编程的练习。

票数 0
EN

Stack Overflow用户

发布于 2017-02-04 22:40:12

我的工作代码:

代码语言:javascript
复制
function [ ] = defect_marking( )
    % This function divides a figure into grids. The grid cells can be clicked  
    % ,on detecting the click the cell would turn to red. Use keypress to exit 
    % the funtion
        disp('Defect Function');
        pw = waitforbuttonpress;
        while pw ~= 1 
                        cell = 100; % size of single cell
                        col = 11; % maximum number of columns in the grid
                        [c1, c2] = ginput(1);% detect cursor co-ordinates
                        cell_n = (floor(c2/cell)*col)+ (floor(c1/cell)+1);
                        n = cell_n;% index of cell cell number selected
                        n_row = floor(n/col); 
                        n_col = mod(n,col);

    % Calculations for determining co-ordinates for grid cell to be patched.
                        x1=  (n_col * cell)- cell;         
                        y1= (n_row * cell)+ cell;

                        x2= (n_col * cell);
                        y2= (n_row * cell)+ cell;

                        x3= (n_col * cell);               
                        y3 = (n_row * cell);

                        x4= (n_col * cell)- cell;         
                        y4= (n_row * cell);

                        x = [x1 x2 x3 x4];
                        y = [y1 y2 y3 y4];

                        p = patch(x,y,'red');% applying patch on grid
                        pw = waitforbuttonpress;% updating button press to detect keypress to exit
        end 
    end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42003567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档