Skip to content

Commit

Permalink
new convert2Grayscale function to avoid repetitions
Browse files Browse the repository at this point in the history
  • Loading branch information
kalwalt committed Nov 11, 2023
1 parent 218eda8 commit 022b559
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,7 @@ class WebARKitTracker::WebARKitTrackerImpl {
void initTracker(uchar* refData, size_t refCols, size_t refRows, ColorSpace colorSpace) {
WEBARKIT_LOGi("Init Tracker!\n");

cv::Mat refGray;

if (colorSpace == ColorSpace::RGBA) {
cv::Mat colorFrame(refRows, refCols, CV_8UC4, refData);
refGray.create(refRows, refCols, CV_8UC1);
cv::cvtColor(colorFrame, refGray, cv::COLOR_RGBA2GRAY);
} else if (colorSpace == ColorSpace::RGB ) {
cv::Mat colorFrame(refRows, refCols, CV_8UC3, refData);
refGray.create(refRows, refCols, CV_8UC1);
cv::cvtColor(colorFrame, refGray, cv::COLOR_RGB2GRAY);
} else if (colorSpace == ColorSpace::GRAY) {
refGray = cv::Mat(refRows, refCols, CV_8UC1, refData);
}
cv::Mat refGray = convert2Grayscale(refData, refCols, refRows, colorSpace);

cv::Mat trackerFeatureMask = createTrackerFeatureMask(refGray);

Expand Down Expand Up @@ -111,18 +99,7 @@ class WebARKitTracker::WebARKitTrackerImpl {
};

void processFrameData(uchar* frameData, size_t frameCols, size_t frameRows, ColorSpace colorSpace) {
cv::Mat grayFrame;
if (colorSpace == ColorSpace::RGBA) {
cv::Mat colorFrame(frameRows, frameCols, CV_8UC4, frameData);
grayFrame.create(frameRows, frameCols, CV_8UC1);
cv::cvtColor(colorFrame, grayFrame, cv::COLOR_RGBA2GRAY);
} else if (colorSpace == ColorSpace::RGB) {
cv::Mat colorFrame(frameRows, frameCols, CV_8UC3, frameData);
grayFrame.create(frameRows, frameCols, CV_8UC1);
cv::cvtColor(colorFrame, grayFrame, cv::COLOR_RGB2GRAY);
} else if (colorSpace == ColorSpace::GRAY) {
grayFrame = cv::Mat(frameRows, frameCols, CV_8UC1, frameData);
}
cv::Mat grayFrame = convert2Grayscale(frameData, frameCols, frameRows, colorSpace);
cv::blur(grayFrame, grayFrame, blurSize);
buildImagePyramid(grayFrame);
processFrame(grayFrame);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <WebARKitCamera.h>
#include <WebARKitGL.h>
#include <WebARKitPattern.h>
#include <WebARKitTrackers/WebARKitOpticalTracking/WebARKitUtils.h>
#include <opencv2/xfeatures2d.hpp>

namespace webarkit {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef WEBARKIT_UTILS_H
#define WEBARKIT_UTILS_H

#include <WebARKitTrackers/WebARKitOpticalTracking/WebARKitEnums.h>
#include <WebARKitTrackers/WebARKitOpticalTracking/WebARKitConfig.h>
#include <WebARKitTrackers/WebARKitOpticalTracking/WebARKitEnums.h>
#include <iostream>

namespace webarkit {
Expand All @@ -28,6 +28,31 @@ namespace webarkit {
return cv::Mat(rows, cols, CV_8UC1, gray);
}*/

static auto convert2Grayscale(uchar* refData, size_t refCols, size_t refRows, ColorSpace colorSpace) {
cv::Mat refGray;

switch (colorSpace) {
case ColorSpace::RGBA: {
cv::Mat colorFrame(refRows, refCols, CV_8UC4, refData);
refGray.create(refRows, refCols, CV_8UC1);
cv::cvtColor(colorFrame, refGray, cv::COLOR_RGBA2GRAY);
} break;
case ColorSpace::RGB: {
cv::Mat colorFrame(refRows, refCols, CV_8UC3, refData);
refGray.create(refRows, refCols, CV_8UC1);
cv::cvtColor(colorFrame, refGray, cv::COLOR_RGB2GRAY);
} break;
case ColorSpace::GRAY: {
refGray = cv::Mat(refRows, refCols, CV_8UC1, refData);
} break;
default: {
refGray = cv::Mat(refRows, refCols, CV_8UC1, refData);
}
}

return refGray;
}

static cv::Mat grayscale(uchar data[], size_t cols, size_t rows, ColorSpace colorType) {
int cn;
switch (colorType) {
Expand Down Expand Up @@ -60,9 +85,7 @@ static cv::Mat grayscale(uchar data[], size_t cols, size_t rows, ColorSpace colo
return cv::Mat(cols, rows, CV_8UC1, gray.data());
}

std::string inline webarkitGetVersion() {
return WEBARKIT_HEADER_VERSION_STRING;
}
std::string inline webarkitGetVersion() { return WEBARKIT_HEADER_VERSION_STRING; }

unsigned int inline webarkitGetVersion(char** versionStringRef) {
std::string version = WEBARKIT_HEADER_VERSION_STRING;
Expand Down

0 comments on commit 022b559

Please sign in to comment.