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

How can I remove all black pixels from an image and use only 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 are where you can divide my red, green and blue channels into the corresponding colors and convert them into three arrays with one column each. And start removing black pixels.

How to remove all black cell values ​​(where R, G, B channels are 0) and only find / use unique RGB values

I added the image below and the code snippet:

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
resize_tmp_green=reshape(fintersect_green',[1,1200*1200])(:); 
resize_tmp_blue=reshape(fintersect_blue',[1,1200*1200])(:); 

%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.
  repmat_rgb(:,:,2)=uint8(repmat(resize_tmp_green(cc,1),[640,480]));
  repmat_rgb(:,:,3)=uint8(repmat(resize_tmp_blue(cc,1),[640,480]));

  %imshow(repmat_rgb)


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

end

PS: I am using Octave 4.0, which is similar to Matlab. And yes, I will no longer see the structure of the original image. I will create a film only from the unique colors found in the image.

image

+4
source share
4

, 2D- , RGB . , RGB 2D-, , , 0, unique, . permute, reshape, 3 RGB. , any, , (.. ), , , , unique, , "", , im:

- :

% 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 2D- , . , 'stable' , . , . , Octave , 'stable', , , , , , .

:

colours = unique(R, 'rows');
+3

, , . , sum . , .

im = imread('example.png');    
black_mask = sum(im, 3) == 0;

2d-, .

red_mask = ~cat(3, black_mask, ones(size(black_mask)), ones(size(black_mask)))); %The inverse of the black mask in the red channel and any pixel in all other channels
green_mask = ~cat(3, ones(size(black_mask)), black_mask, ones(size(black_mask))));%For the other channels just change the order.
blue_mask= ~cat(3, ones(size(black_mask)), ones(size(black_mask)), black_mask));

, .

red = im(red_mask);
blue = im(blue_mask);
green = im(green_mask);

,

non_unique = cat(3, red, green, blue);

non_unique - , . , unique 'row.

non_unique = permute(non_unique, [1 3 2]);
output = unique(non_unique, 'rows'); %Get unique rows
output = permute(output, [1 3 2]); %Permute it back to recover color channels

1 94 . 640x480, . , , 94: 2 47.

reshaped_output = reshape(output, 2, 47, 3);
reshaped_output = imresize(reshaped_output, [480, 640]);

.

Counts showing all non-black pixels, unique non-black pixels and the final resized image

+2

, , :

R = double(im(:,:,1))*255*255;
G = double(im(:,:,2))*255;
B = double(im(:,:,3));
RGB = R+G+B; % Now you have 2D matrix

unq = sort(unique(RGB));
unq = unq(2:end); %remove (0,0,0)

B2 = rem(unq(:),255);
G2 = rem((unq-B2)/255,255);
R2 = (unq-G2*255 - B2)/255/255;
% You can make this part more efficient probably. and make sure there is no error

colors_you_want = [R2 G2 B2];

, , , . unq (0,0,0), . 2D-, R - , G - , B - .

0

I do not use the language that you put in your code, but if I did it in c / C ++, I would iterate over the data and count the non-black pixels, create a new array of this size and repeat the loop and copy them one by one to the new an array. It can be done?

-2
source

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


All Articles