How can I remove all black pixels from an image and use only the unique RGB values ​​found in the image


How can I delete all black pixels from an image and only use the unique RGB values found in the image

What I'm trying to do is:
1) delete all black pixels from the image and only use the unique RGB colors found in the image
2) export each combined RGB color pixels that is unique as a separate 640x480 image
3) Convert / join these images into a movie file.

My thoughts where to split my red, green, and blue channels into there corresponding colors and reshape them into three arrays with one column each. And start deleting the black pixels.

How can I delete all cell values that are black (where the R,G,B channels are 0) and only find / use the RGB values that are unique

I've attached the image below and a snippet of the code:

resize_tmp_red=reshape(fintersect_red',[1,1200*1200])(:); %will cause array to be created / reshaped left to right and top to bottom (like reading a book) into 1 column

%How can I delete all cell values that are black (where the R,G,B channels are 0) and only find / use the RGB values that are unique in the image

for cc=1:10

  repmat_rgb(:,:,1)=uint8(repmat(resize_tmp_red(cc,1),[640,480])); %creates 640x480 image of RGB taken from deleted black and unique color array.


  imwrite(repmat_rgb,strcat('/tmp/',sprintf('%03d', cc),'_img.png')); %creates image file


PS: I'm using Octave 4.0 which is like Matlab. And yes I will no longer see the structure of the original image. I will be creating a movie from just the unique colors found in the image.

As you mentioned in your comment, you would like an output 2D matrix of unique colours where each row is a RGB tuple without the black pixels. What you can do is reshape the RGB image into a 2D matrix where each column is a colour channel, remove all instances where a row is entirely 0, then run it through unique to remove the duplicates. To do this, you will need to perform a per channel transpose by using permute, then exploiting the order of the elements with reshape to finally create a 3 column matrix of RGB values. Once you do this, use any to find all rows that have at least one value (i.e. not a black pixel), filter out the rows of completely zero of your matrix then finally use unique but apply this to the rows so that each row would be considered as one "example", given that your image is stored in im:

Something like this would work:

% Reshape the RGB image into a 3 column matrix
R = reshape(permute(im, [2 1 3]), [], 3);

% Remove black pixels
R = R(any(R, 2), :);

% Remove duplicates
colours = unique(R, 'rows', 'stable');

colours would be the output 2D matrix of colours that are unique without black. Note that I've used the 'stable' flag to maintain the order in which the colours have been encountered. By leaving this out, it would sort the matrix by the rows in the output. As you have noted in your comment, Octave does not support this functionality so you can remove the 'stable' flag if you don't care about the order, which I believe you do not in this case as you only want a matrix of unique colours that don't include black.


colours = unique(R, 'rows');