简体   繁体   English

无需循环即可获取图像的帧/​​图案MATLAB

[英]Get frame / pattern of an image without loop MATLAB

I would like to extract certain part of an image. 我想提取图像的某些部分。 Let's say, only those parts that are indexed by ones, in some kind of template or frame. 假设只有在某种模板或框架中被索引的那些部分。

GRAYPIC = reshape(randperm(169), 13, 13);
FRAME = ones(13);
FRAME(5:9, 5:9) = 0;
FRAME_OF_GRAYPIC = []; % the new pic that only shows the frame extracted

I can achieve this using a for loop: 我可以使用for循环来实现:

for X = 1:13
for Y = 1:13
    vlaue = FRAME(Y, X);
    switch vlaue

        case 1
            FRAME_OF_GRAYPIC(X,Y) = GRAYPIC(X,Y)
        case 0
            FRAME_OF_GRAYPIC(X,Y) = 0
    end
end
end
imshow(mat2gray(FRAME_OF_GRAYPIC));

However, is it possible to use it with some kind of vector operation, ie: 但是,可以将其与某种矢量操作一起使用,即:

FRAME_OF_GRAYPIC = GRAYPIC(FRAME==1);

Though, this doesn't work unfortunately. 不过,不幸的是,这行不通。

Any suggestions? 有什么建议么?

Thanks a lot for your answers, best, Clemens 非常感谢您的回答,最好,克莱门斯

Too long for a comment... 评论太久了...

GRAYPIC = reshape(randperm(169), 13, 13);
FRAME = zeros(13);        
FRAME(5:9, 5:9) = 0;      
FRAME_OF_GRAYPIC = zeros(size(GRAYPIC); % MUST preallocate new pic the right size
FRAME = logical(FRAME);   % ... FRAME = (FRAME == 1)
FRAME_OF_GRAYPIC(FRAME) = GRAYPIC(FRAME);

Three things to note here: 这里要注意的三件事:

  • FRAME must be a logical array. FRAME必须是逻辑数组。 Create it with true() / false() , or cast it using logical() , or select a value to be true using FRAME = (FRAME == true_value); 使用true() / false()创建它,或者使用logical() FRAME = (FRAME == true_value);它,或者使用FRAME = (FRAME == true_value);选择一个值为true的值FRAME = (FRAME == true_value);
  • You must preallocate your final image to the proper dimensions, otherwise it will turn into a vector. 您必须将最终图像预分配为适当的尺寸,否则它将变成矢量。
  • You need the image indices on both sides of the assignment: 您需要在作业的两侧使用图像索引:
    FRAME_OF_GRAYPIC(FRAME) = GRAYPIC(FRAME);

Output: 输出:

FRAME_OF_GRAYPIC =
    38    64   107    63    27   132   148   160    88    59   102    69    81
    14   108    76    58    49    55    51    19   158    52   100   153    39
    79   139    12   115   147   154    96   112    82    73   159   146    93
   169     2    71    25    33   149   138   150   129   117    65    97    17
    43   111    37   142     0     0     0     0     0   128    84    86    22
     9   137   127    45     0     0     0     0     0    68    28    46   163
    42    11    31    29     0     0     0     0     0   152     3    85    36
    50   110   165    18     0     0     0     0     0   144   143    44   109
   114   133     1   122     0     0     0     0     0    80   167   157   145
    24   116    60   130    53    77   156    35     6    78    90    30   140
    74   120    40    26   106   166   121    34    98    57    56    13    48
     8   155     4    16   124    75   123    23   105    66     7   141    70
    89   113    99   101    54    20    94    72    83   168    61     5    10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM