Sorting unique colors from the image (much simplified now)

20 次查看(过去 30 天)
Hello everyone,
Code uses rgb2hex and hex2rgb from FEX, I might be doing unecessary work with this, but let it be for now - https://ch.mathworks.com/matlabcentral/fileexchange/46289-rgb2hex-and-hex2rgb.
clc;
clear;
fclose all;
File_Name = 'https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/161px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg'; % Image name
Image_Data = imread(File_Name);
[V_Pixels, H_Pixels, ~] = size(Image_Data);
Num_Points = H_Pixels * V_Pixels;
Colors = cell(V_Pixels, H_Pixels);
% Getting colors as hex values
for ii = 1 : 1 : V_Pixels
for jj = 1 : 1 : H_Pixels
Colors {ii, jj} = rgb2hex([Image_Data(ii,jj,1) Image_Data(ii,jj,2) Image_Data(ii,jj,3)]); % RGB values
end
end
Color_Vector = (unique(reshape(Colors, [1, Num_Points])))'; %Removes duplicates and sorts
Color_RGB = hex2rgb(Color_Vector);
x = 1:1:length(Color_Vector);
y = ones(1,length(Color_Vector));
figure(1);
hb = bar(x, y);
hb.FaceColor = 'flat';
for ii = 1 : 1 : length(Color_Vector)
hb.CData(ii,:) = Color_RGB(ii,:);
end
A graph is attached here, the colors are not sorted well. Obviously it means that colors can't be sorted this way.
I found some posts on sorting the image by HSV, but also realised that I am out of depth when it comes to image processing. I found an example here https://stackoverflow.com/a/2246189/14022732, but I am a bit lost when I have to convert back to rgb.
If you could share some tips on this, it would be great.
EDIT: I would like to sort unique colors so that they appear like a gradient, see my reply below.

采纳的回答

Image Analyst
Image Analyst 2020-7-30
Mario:
Here is the color gamut of that image in RGB color space:
and here is the gamut in HSV color space:
Each dot in the conical gamut represents the color of one pixel in the image. You can see that there is no real sensible way to sort that. What you're doing has nothing to do with HSV color space. You're all in RGB color space and you're sorting by Blue first, then green, and finally by red. Not sure that makes any sense as far as sorting goes, and I don't see how sorting by anything in HSV color space makes sense either. Do you just want to see unique colors?
Also I don't know what you mean by converting back to RGB. Not sure exactly what would get converted back - all the values, or just the unique values. If you keep all the values, of course you can convert back but if you throw out some because they're duplicated, then you can convert back to colors but you won't have an image, list a list of RGB values because you threw out some pixels and we don't know what to put there. For example if we have an aray m=[1,2,3,3,4] and we multiply by 2 we get m2 = [2,4,6,6,8]. Now if we throw out duplicates we get m2 = [2,4,6,8]. So you can divide by 2 to get [1,2,3,4] but that is not what you originally started with.
  9 个评论
Image Analyst
Image Analyst 2020-8-2
There is no "right" way to do it. There are several ways to do it, according to the respective algorithms, but it's not like one is more correct than another - they're just different.
Mario Malic
Mario Malic 2020-8-2
I meant, the right way to solve my problem, not the right way to sort the colors.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Blue 的更多信息

产品


版本

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by