Azzera filtri
Azzera filtri

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

3 visualizzazioni (ultimi 30 giorni)
Johnathon Street
Johnathon Street il 9 Feb 2020
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 Commenti
Johnathon Street
Johnathon Street il 9 Feb 2020
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 il 9 Feb 2020
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.

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Image Processing Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by