Как эффективно нарисовать список пикселей в Matlab изображения? - PullRequest
2 голосов
/ 04 октября 2011

Я уже реализовал функцию, чтобы сделать это, но мне не нравится необходимость в цикле. Однако я не вижу способа избежать петли. Любопытно, есть ли лучший способ сделать это.

function [ colored_img ] = colorImg ( img, ix, c )
% function [ colored_img ] = colorImg ( img, ix, c )
% 
% INPUTS
%   img - a 3D array representing the image to color.  
%   ix  - the indexes of the pixels to set to a new color.
%   c   - a vector representing the color to paint the pixels ix.
%
% OUTPUTS
%   colored_img - the colored image.


colored_img = img;
for jx = 1 : numel(c);
    a = colored_img(:,:,jx);
    a(ix) = c(jx);
    colored_img(:,:,jx) = a;
end




end

1 Ответ

0 голосов
/ 04 октября 2011

Вы можете просто скопировать индексы для цветов.

imageSize = numel(img(:,:,1));
rgbIdx = bsxfun(@plus,ix(:),(0:2)*imageSize);

%# replicate the color so that there are as many 
%# entries as pixels to recolor
%# (skip this if you use a colormap
%#  of length nPixelsToRecolor)
nPixelsToRecolor = length(ix); %# assign this so that comment makes sense
repColor = repmat(c,nPixelsToRecolor,1); %# assuming color is 1-by-3

colored_img = img;
colored_img(rgbIdx(:)) = repColor(:);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...