How to convert m-by-n-by-3 array to m-by-n?

3 次查看(过去 30 天)
I am attempting to manually convert RGB values to HSI. I do not wish to use the rgb2hsv code included in MATLAB. Also, the hope is to manually recreate other color spaces I have found in research articles. Therefore, solving this problem would assist me immensly in those areas. Here is my code for the HUE component...
for i = 1:x
for j = 1:y
TH(i,j) = acos((((1/2).*((R(i,j) - G(i,j)) + (R(i,j) - B(i,j))))./(sqrt((R(i,j) - G(i,j)).^2 + ((R(i,j) - B(i,j)).*(G(i,j) - B(i,j)))))));
if B(i,j) <= G(i,j)
H(i,j) = TH(i,j);
elseif B(i,j) > G(i,j)
H(i,j) = ((2*pi) - TH(i,j));
end
end
end
...
The idea is that it will loop through each pixel and create THETA based on the formula, then assign a value to the HUE component image at that pixel based on the comparison of blue and green pixel values at that point (as shown in the code). My issue is that it does create H; however, H is an m-by-n-by-3 array, and i would like to have a m-by-n array. Any assistance in the matter would be appreciated and will serve as foundation for me to try an emulate other color spaces. Also if there is a cleaner way beside a for loop feel free to share.
THANK YOU
  5 个评论
Johnathon Street
Johnathon Street 2020-2-9
im = Colorcloud;
im = rescale(im);
R = im(:,:,1);
G = im(:,:,2);
B = im(:,:,3);
TH = (acos((1/2).*((R - G) + (R - B)))./(sqrt((R - G).^2 + (R - B).*(G - B))));
mask = (B <= G);
H(mask) = TH(mask);
mask = (B > G);
H(mask) = ((2*pi) - TH(mask));
H = (H./(2*pi));
Previous effort using different image was successful. However, I did not get the same H image as i had when running the for loop. The results were slightly different. When running with different image i recieve this error...
Attempt to grow array along ambiguous dimension.
Error in code (line 11)
H(mask) = TH(mask);
Do you see where i may have made an error in my code?
Walter Roberson
Walter Roberson 2020-2-9
H = TH;
Then skip the first test. The second test will overwrite all relevant parts of H.
If I recall correctly for computing hue you need tests involving different color channels not just B and G.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by