Matching values ​​from a matrix in a three-dimensional array

I am trying to match the RGB values ​​in the image.

         % R  G   B
RGBset = [ 3  9  12;
           4  8  13;
          11 13  13;
           8  3   2]

img(:,:,1) = [1   2   3
              6   5   4
              7   9   8
             10  11  12];

img(:,:,2) = [3  4  8;
              6  7  8;
             11 10  9;
             12 13 14];

img(:,:,3)= [3  7  2;
             4  9 10;
             5 11 12;
             6 13 14]

In this image, only one RGB value corresponds to RGBset, which is equal [11,13,13], so the expected result:

[0  0  0;
 0  0  0;
 0  0  0;
 0  1  0]; % reshape(img(4,2,:),1,3) = [11, 13 13] is available in RGBset
           % no other RGB value is present in the image

I made this code, but for large images it is very slow.

matched= zeros(img(:,:,1));
for r=1:size(img(:,:,1),1)
    for c=1:size(img(:,:,2),2)
     matched(r,c)=ismember(reshape(img(r,c,:),1,3),RGBset,'rows');
    end
end

What will be the solution with greater speed?

+4
source share
3 answers

RGB , RGBset, img. 2D 1D . . , , .

, :

% Scaling array for dim reduction
s = [256^2, 256, 1].';

% Reduce dims for RGBset and img
RGBset1D = RGBset*s;
img1D = reshape(img,[],3)*s;

% Finally use find membership and reshape to 2D
out = reshape(ismember(img1D, RGBset1D), size(img,1), []);

-

         % R  G   B
RGBset = [ 3  9  12;
           4  8  13;
          11 13  13;
           8  3   2]

% Setup inputs
img = randi(255, 2000, 2000, 3);
img(3,2,:) = RGBset(4,:);

% Luis soln
disp('--------------------- Reshape + Permute ------------------')
tic
img2 = reshape(permute(img, [3 1 2]), 3, []).';
matched = ismember(img2, RGBset, 'rows');
matched = reshape(matched, size(img,1), []);
toc

% Proposed in this post
disp('--------------------- Dim reduction ------------------')
tic
s = [256^2, 256, 1].';
RGBset1D = RGBset*s;
img1D = reshape(img,[],3)*s;
out = reshape(ismember(img1D, RGBset1D), size(img,1), []);
toc

-

--------------------- Reshape + Permute ------------------
Elapsed time is 3.101870 seconds.
--------------------- Dim reduction ------------------
Elapsed time is 0.031589 seconds.
+5

permute reshape:

img2 = reshape(permute(img, [3 1 2]), 3, []).';
matched = ismember(img2, RGBset, 'rows');
matched = reshape(matched, size(img,1), []);

img2, img. , ismember(..., 'rows') . .

+3

You can simply scroll the color , which is much faster than looping through each pixel.

% Set up your colours into the 3rd dimension, so they match along the same axis
RGB3D = reshape(RGBset,[],1,3);
% Loop over them
for ii = 1:size(RGB3D, 1)
    % See if all 3 members of the colour match any pixel
    matched = all(ismember(img, RGB3D(ii,:,:)),3)
    if any(matched)
        disp(matched) 
        disp(['matched color: ' num2str(ii)]); 
        % do something else with the matched pixels
    end
end
0
source

Source: https://habr.com/ru/post/1684289/


All Articles