【问题标题】:MATLAB - Create img from linear index array + colorMATLAB - 从线性索引数组 + 颜色创建 img
【发布时间】:2019-02-07 03:04:05
【问题描述】:

我有一个包含大小为 x、y、3 的全零的空白 img。 我有一个线性索引数组,例如

[1 10 99 1562]

我有一个颜色为 1、1、3 双数组,例如

color(:,:,1) = 100
color(:, :, 2) = 200
color(:,:,3) = 100

如何将 img 的所有索引设置为该颜色?

【问题讨论】:

  • 您所说的“线性索引”一词是错误的。但我明白你在问什么
  • 这些类型的索引叫什么?我使用 sub2ind 创建了它们,所以我只是假设我正在创建线性索引?你也可以帮我设置值吗
  • 我能得到更多信息吗
  • 可能“图像的单一颜色通道的线性索引”是更好的术语。

标签: arrays image matlab indexing


【解决方案1】:

有一个循环:

[r, c] = ind2sub(size(A(:,:,1)),ind); %Getting corresponding row and column subscripts
for k = 1:numel(ind)
    A(r(k),c(k),:) = color;           %Changing each of them to desired color
end

或矢量化解决方案:

%Creating a mask
mask = false(size(A(:,:,1)));   mask(ind) = true;
%Transferring the color to the mask
A = mask.*color;    %Impl.exp., use A = bsxfun(@times,mask,color) in <R2016b 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-20
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多