Skip to content

Commit

Permalink
adding BGR2LUV
Browse files Browse the repository at this point in the history
  • Loading branch information
youssef-shaban committed May 3, 2023
1 parent f8faba1 commit ba64a25
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 10 deletions.
69 changes: 69 additions & 0 deletions CV/segmentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,3 +551,72 @@ void Segmentation::regionGrowing(cv::Mat inputImage, cv::Mat& outputImage, std::
}
outputImage = inputImage;
}

void Segmentation::bgr_to_luv(cv::Mat& img_bgr, cv::Mat& output) {


// Split channels
std::vector<cv::Mat> bgr;
cv::split(img_bgr, bgr);

const double ref_X = 0.95047;
const double ref_Y = 1.0;
const double ref_Z = 1.08883;

// Convert BGR to XYZ
cv::Mat XYZ(img_bgr.size(), CV_64FC3);
for (int i = 0; i < img_bgr.rows; i++) {
for (int j = 0; j < img_bgr.cols; j++) {
double B = bgr[0].at<uchar>(i, j) / 255.0;
double G = bgr[1].at<uchar>(i, j) / 255.0;
double R = bgr[2].at<uchar>(i, j) / 255.0;

double X = 0.412453 * R + 0.357580 * G + 0.180423 * B;
double Y = 0.212671 * R + 0.715160 * G + 0.072169 * B;
double Z = 0.019334 * R + 0.119193 * G + 0.950227 * B;

XYZ.at<cv::Vec3d>(i, j) = cv::Vec3d(X, Y, Z);
}
}

// Convert XYZ to LUV
cv::Mat img_luv = cv::Mat(img_bgr.size(), CV_64FC3);
for (int i = 0; i < XYZ.rows; i++) {
for (int j = 0; j < XYZ.cols; j++) {
double X = XYZ.at<cv::Vec3d>(i, j)[0];
double Y = XYZ.at<cv::Vec3d>(i, j)[1];
double Z = XYZ.at<cv::Vec3d>(i, j)[2];

double var_U = (4 * X) / (X + 15 * Y + 3 * Z);
double var_V = (9 * Y) / (X + 15 * Y + 3 * Z);

double var_Y = Y / ref_Y;
if (var_Y > 0.008856) {
var_Y = std::pow(var_Y, 1.0 / 3.0);
}
else {
var_Y = (7.787 * var_Y) + (16.0 / 116.0);
}

double ref_U = (4 * ref_X) / (ref_X + 15 * ref_Y + 3 * ref_Z);
double ref_V = (9 * ref_Y) / (ref_X + 15 * ref_Y + 3 * ref_Z);

double L = (116 * var_Y) - 16;
double u = 13 * L * (var_U - ref_U);
double v = 13 * L * (var_V - ref_V);

img_luv.at<cv::Vec3d>(i, j) = cv::Vec3d(L - 16, u, v);
}
}

std::vector<cv::Mat> channels;
cv::split(img_luv, channels);


channels[0] = channels[0] * (255.0f / 100.0f);
channels[1] = (channels[1] + 134) * (255.0f / 354.0f);
channels[2] = (channels[2] + 140) * (255.0f / 262.0f);
cv::merge(channels, output);

output.convertTo(output, CV_8UC3);
}
1 change: 1 addition & 0 deletions CV/segmentation.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ class Segmentation
static void closetClusters(std::vector<cv::Vec3b> clusters, std::vector<cv::Vec3d>& minDistances, std::mutex& myMutex, int start_i, int end_i);
static double generateKClusters(cv::InputArray _data, int K, cv::InputOutputArray _bestLabels, cv::TermCriteria criteria, int attempts, cv::OutputArray _centers);
static void createRegion(cv::Mat input, cv::Mat& output, std::vector<std::vector<cv::Point>> &regions, std::vector<cv::Point> seeds, int threshold);
static void bgr_to_luv(cv::Mat& img_bgr, cv::Mat& output);
};
#endif // SEGMENTATION_H
5 changes: 3 additions & 2 deletions CV/thresholding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ void Thresholding::localThreshold(cv::Mat& img, cv::Mat& output, int pieceSize)
cv::Rect area(col, row, width, height);
cv::Mat piece = img(area);
cv::Mat outputPiece(piece.size(), piece.type());
Thresholding::otsuThresholding(piece, outputPiece);
Thresholding::multiLevelOtsu(piece, outputPiece, 2);

outputPiece.copyTo(output(area));

Expand Down Expand Up @@ -199,7 +199,8 @@ void Thresholding::multiLevelOtsu(cv::Mat img, cv::Mat& output, int M)
}
if (i == thresholds.size())
i--;
output.at<char>(row, col) = thresholds[i - 1];

output.at<uchar>(row, col) = 255 * (((double)i - 1) / (thresholds.size() - 1));
}

}
Expand Down
14 changes: 7 additions & 7 deletions a01-team06.pro
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ FORMS += \



INCLUDEPATH += C:\opencv\build\include
INCLUDEPATH += C:\Users\youss\Documents\libraries\opencv\build\include

LIBS += C:\opencv\release\bin\libopencv_core470.dll
LIBS += C:\opencv\release\bin\libopencv_highgui470.dll
LIBS += C:\opencv\release\bin\libopencv_imgcodecs470.dll
LIBS += C:\opencv\release\bin\libopencv_imgproc470.dll
LIBS += C:\opencv\release\bin\libopencv_features2d470.dll
LIBS += C:\opencv\release\bin\libopencv_calib3d470.dll
LIBS += C:\Users\youss\Documents\libraries\opencv\release\bin\libopencv_core470.dll
LIBS += C:\Users\youss\Documents\libraries\opencv\release\bin\libopencv_highgui470.dll
LIBS += C:\Users\youss\Documents\libraries\opencv\release\bin\libopencv_imgcodecs470.dll
LIBS += C:\Users\youss\Documents\libraries\opencv\release\bin\libopencv_imgproc470.dll
LIBS += C:\Users\youss\Documents\libraries\opencv\release\bin\libopencv_features2d470.dll
LIBS += C:\Users\youss\Documents\libraries\opencv\release\bin\libopencv_calib3d470.dll
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
Expand Down
2 changes: 1 addition & 1 deletion utilities/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void Scene::mousePressEvent(QGraphicsSceneMouseEvent *event)
if(!this->drawFlag){
QPointF point = event->scenePos();
this->center= point;
this->drawFlag=true;
// this->drawFlag=true;
drawCircle();
}
}
Expand Down

0 comments on commit ba64a25

Please sign in to comment.