Fix out-of-gamut RGB colors
5 ビュー (過去 30 日間)
古いコメントを表示
I'm going in circle... I have this code, which converts from Lab to RGB :
rawRGB = lab2rgb(Lab,'WhitePoint','d50')
Trouble is, there are loads of RGB colors that are out of gamut and come out with either negative values or values greater than one.
So I use this code to hunt down for out-of-gamut values :
isoog_1 = any(rawRGB>1 ,3);
isoog_0 = any(rawRGB<0 ,3);
But I'm lost when it comes time to use this information to substitue out-of-gamut colors in my rawRGB matrix for 0s and 1s. I tried this :
rawRGB(repmat(isoog_1,[1 1 3])) = 1;
rawRGB(:,4:9) = []
rawRGB(repmat(isoog_0,[1 1 3])) = 0;
rawRGB(:,4:9) = []
But I must be doing something 'illegal' which I can't figure out, because I get this error :
Attempt to grow array along ambiguous dimension.
This logic worked perfectly fine on this code though (I extracted row 1062 from the Lab matrix to experiment with) :
Test = Lab(1062,:) % 7.5PB 4/26
% Test = 38.57 54.97 -100.01
TestRGB = lab2rgb(Test,'WhitePoint','d50')
% TestRGB = 0.2537 0.2216 1.0216
isoog_1 = any(TestRGB>1 ,3);
isoog_0 = any(TestRGB<0 ,3);
TestRGB(repmat(isoog_1,[1 1 3])) = 1;
TestRGB(:,4:9) = []
TestRGB2 = [0.34 -0.34 0.98]
isoog_0 = any(TestRGB2<0 ,3);
TestRGB2(repmat(isoog_0,[1 1 3])) = 0;
I guess I don't see how to go about conditionnaly substituting values inside a matrix...
Any help is appreciated.
0 件のコメント
採用された回答
その他の回答 (1 件)
Roger Breton
2022 年 3 月 31 日
3 件のコメント
DGM
2022 年 4 月 1 日
編集済み: DGM
2022 年 4 月 1 日
I mentioned MIMT maxchroma() earlier. That would allow you to find the gamut extents in LCHab and then simply clamp chroma prior to conversion. It might not be the universally best approach, but it at least keeps colors from migrating to the corners of the RGB cube during truncation. In practice, you wouldn't need to use it directly, as MIMT lch2rgb() uses it internally when the 'truncatelch' option is specified.
EDIT: wait. If you're using d50, then maxchroma won't work. I never bothered to set it up for d50, but there's no reason that the same approach couldn't be used.
参考
カテゴリ
Help Center および File Exchange で Color についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!