# Converting 3D vector to scalar output

5 views (last 30 days)
Maggie on 12 Apr 2024
Answered: Benjamin Thompson on 12 Apr 2024
We're recording RGB values from ImageJ, and each RGB value corresponds to a scalar pH. The ultimate goal is to input an RGB vector, and output the corresponding pH value based on the standardized data. How can we take a 3D vector and convert it to a scalar output? I assume I have to do some sort of PCA or regression model, but I'm not sure how to do that. I have uploaded the file with the RGB values that all correspond to specific pHs (Multiple RGB values can correspond to the same pH).

Samuel Sonesson on 12 Apr 2024
Moved: Matt J on 12 Apr 2024
Do you know which pH value each RGB value corresponds to? If you do a lookup table should prove usefull.
Im reducing the total amount of colors to keep it short here... This is by no means the best way of implementing a lookup table, but it can get you started in thinking.
Say the RGB triplet [1,1,0] represents a pH of 8;
We create an array of all rgb triplets
RGB = [ 0 0 0
1 0 0
0 1 0
0 0 1
1 1 0
1 0 1
0 1 1
1 1 1];
We then define a vector of corresponding ph Values
pH = [1
2
4
7
8
9
12];
We find the index in RGB that matches the RGB data from imageJ
imageJ = [1,1,0];
for i = 1:size(RGB,1)
if imageJ == RGB(i,:);
ind = i;
break
end
end
ResultingpH = pH(i)
ResultingpH = 8

Benjamin Thompson on 12 Apr 2024
You can export your data to CSV and import into MATLAB as a table. Then use this script to review it and get some insight. It is not following any nice pattern in the RGB coordinate space. But if all you want is a pH assignment to the nearest 0.5 value, then you could try calculating the centroid for each color group using the "mean" function. Then assign a pH value from the choices of {4.5, 5.0, ..., 7.5, 8.0} based on which centroid the RGB triplet is closest to.
The centroid is also where I plot the "text" value for each pH group in the attached script. Some are very close together but it might work.