xyz to rgb & rgb to xyz

This commit is contained in:
novak_99 2022-02-11 23:25:07 -08:00
parent 453b1a8648
commit c21c750727
7 changed files with 54 additions and 13 deletions

View File

@ -159,8 +159,8 @@ namespace MLPP{
LinAlg alg;
std::vector<std::vector<std::vector<double>>> YCbCr;
YCbCr = alg.resize(YCbCr, input);
for(int i = 0; i < YCbCr.size(); i++){
for(int j = 0; j < YCbCr[i].size(); j++){
for(int i = 0; i < YCbCr[0].size(); i++){
for(int j = 0; j < YCbCr[0][i].size(); j++){
YCbCr[0][i][j] = 0.299 * input[0][i][j] + 0.587 * input[1][i][j] + 0.114 * input[2][i][j];
YCbCr[1][i][j] = -0.169 * input[0][i][j] - 0.331 * input[1][i][j] + 0.500 * input[2][i][j];
YCbCr[2][i][j] = 0.500 * input[0][i][j] - 0.419 * input[1][i][j] - 0.081 * input[2][i][j];
@ -169,12 +169,14 @@ namespace MLPP{
return YCbCr;
}
// Conversion formulas available here:
// https://www.rapidtables.com/convert/color/rgb-to-hsv.html
std::vector<std::vector<std::vector<double>>> Data::rgb2hsv(std::vector<std::vector<std::vector<double>>> input){
LinAlg alg;
std::vector<std::vector<std::vector<double>>> HSV;
HSV = alg.resize(HSV, input);
for(int i = 0; i < HSV.size(); i++){
for(int j = 0; j < HSV[i].size(); j++){
for(int i = 0; i < HSV[0].size(); i++){
for(int j = 0; j < HSV[0][i].size(); j++){
double rPrime = input[0][i][j] / 255;
double gPrime = input[1][i][j] / 255;
double bPrime = input[2][i][j] / 255;
@ -211,6 +213,23 @@ namespace MLPP{
}
return HSV;
}
// http://machinethatsees.blogspot.com/2013/07/how-to-convert-rgb-to-xyz-or-vice-versa.html
std::vector<std::vector<std::vector<double>>> Data::rgb2xyz(std::vector<std::vector<std::vector<double>>> input){
LinAlg alg;
std::vector<std::vector<std::vector<double>>> XYZ;
XYZ = alg.resize(XYZ, input);
std::vector<std::vector<double>> RGB2XYZ = {{0.4124564, 0.3575761, 0.1804375}, {0.2126726, 0.7151522, 0.0721750}, {0.0193339, 0.1191920, 0.9503041}};
return alg.vector_wise_tensor_product(input, RGB2XYZ);
}
std::vector<std::vector<std::vector<double>>> Data::xyz2rgb(std::vector<std::vector<std::vector<double>>> input){
LinAlg alg;
std::vector<std::vector<std::vector<double>>> XYZ;
XYZ = alg.resize(XYZ, input);
std::vector<std::vector<double>> RGB2XYZ = alg.inverse({{0.4124564, 0.3575761, 0.1804375}, {0.2126726, 0.7151522, 0.0721750}, {0.0193339, 0.1191920, 0.9503041}});
return alg.vector_wise_tensor_product(input, RGB2XYZ);
}
// TEXT-BASED & NLP
std::string Data::toLower(std::string text){

View File

@ -32,6 +32,8 @@ class Data{
std::vector<std::vector<double>> rgb2gray(std::vector<std::vector<std::vector<double>>> input);
std::vector<std::vector<std::vector<double>>> rgb2ycbcr(std::vector<std::vector<std::vector<double>>> input);
std::vector<std::vector<std::vector<double>>> rgb2hsv(std::vector<std::vector<std::vector<double>>> input);
std::vector<std::vector<std::vector<double>>> rgb2xyz(std::vector<std::vector<std::vector<double>>> input);
std::vector<std::vector<std::vector<double>>> xyz2rgb(std::vector<std::vector<std::vector<double>>> input);
// Text-Based & NLP
std::string toLower(std::string text);

View File

@ -1205,4 +1205,27 @@ namespace MLPP{
}
return std::sqrt(sum);
}
// Bad implementation. Change this later.
std::vector<std::vector<std::vector<double>>> LinAlg::vector_wise_tensor_product(std::vector<std::vector<std::vector<double>>> A, std::vector<std::vector<double>> B){
std::vector<std::vector<std::vector<double>>> C;
C = resize(C, A);
for(int i = 0; i < A[0].size(); i++){
for(int j = 0; j < A[0][i].size(); j++){
std::vector<double> currentVector;
currentVector.resize(A.size());
for(int k = 0; k < C.size(); k++){
currentVector[k] = A[k][i][j];
}
currentVector = mat_vec_mult(B, currentVector);
for(int k = 0; k < C.size(); k++){
C[k][i][j] = currentVector[k];
}
}
}
return C;
}
}

View File

@ -226,6 +226,8 @@ namespace MLPP{
double norm_2(std::vector<std::vector<std::vector<double>>> A);
std::vector<std::vector<std::vector<double>>> vector_wise_tensor_product(std::vector<std::vector<std::vector<double>>> A, std::vector<std::vector<double>> B);
private:
};

View File

@ -202,6 +202,8 @@ The result will be the model's predictions for the entire dataset.
- RGB to Grayscale
- RGB to HSV
- RGB to YCbCr
- RGB to XYZ
- XYZ to RGB
18. ***Utilities***
1. TP, FP, TN, FN function
2. Precision

BIN
a.out

Binary file not shown.

View File

@ -467,14 +467,7 @@ int main() {
// // CONVOLUTION, POOLING, ETC..
std::vector<std::vector<double>> input = {
{255,255,255,255,0,0,0,0},
{1,1,1,1,0,0,0,0},
{1,1,1,1,0,0,0,0},
{1,1,1,1,0,0,0,0},
{1,1,1,1,0,0,0,0},
{1,1,1,1,0,0,0,0},
{1,1,1,1,0,0,0,0},
{1,1,1,1,0,0,0,0}
{1},
};
std::vector<std::vector<std::vector<double>>> tensorSet;
@ -482,7 +475,7 @@ int main() {
tensorSet.push_back(input);
tensorSet.push_back(input);
alg.printTensor(data.rgb2hsv(tensorSet));
alg.printTensor(data.rgb2xyz(tensorSet));
// alg.printMatrix(conv.convolve(input, conv.getPrewittVertical(), 1)); // Can use padding
// alg.printMatrix(conv.pool(input, 4, 4, "Max")); // Can use Max, Min, or Average pooling.