Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Float to int transition #134

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions perf/perf_skeleton.cpp
Original file line number Diff line number Diff line change
@@ -77,6 +77,7 @@ PERF_TEST_P(Size_Only, ImageResize, testing::Values(MAT_SIZES))
// Test(s) for the Thinning function
//


PERF_TEST_P(Size_Only, Thinning, testing::Values(MAT_SIZES))
{
Size sz = GetParam();
@@ -190,3 +191,4 @@ TEST(CompleteColorSpace, ConvertColor_fpt)
ASSERT_LT(cv::countNonZero(diff), 7565);
// ASSERT_EQ(0, cv::countNonZero(diff));
}

15 changes: 13 additions & 2 deletions src/convertcolor.cpp
Original file line number Diff line number Diff line change
@@ -52,6 +52,8 @@ void ConvertColor_BGR2GRAY_BT709(const cv::Mat& src, cv::Mat& dst)
}
}



void ConvertColor_BGR2GRAY_BT709_fpt(const cv::Mat& src, cv::Mat& dst)
{
CV_Assert(CV_8UC3 == src.type());
@@ -60,15 +62,24 @@ void ConvertColor_BGR2GRAY_BT709_fpt(const cv::Mat& src, cv::Mat& dst)

const int bidx = 0;

int C1 = 13933;
int C2 = 46871;
int C3 = 4732;
int C4 = 32768;
/*short C1 = 435;
short C2 = 1465;
short C3 = 148;
short C4 = 1024;*/
for (int y = 0; y < sz.height; y++)
{
const cv::Vec3b *psrc = src.ptr<cv::Vec3b>(y);
uchar *pdst = dst.ptr<uchar>(y);

for (int x = 0; x < sz.width; x++)
{
float color = 0.2126 * psrc[x][2-bidx] + 0.7152 * psrc[x][1] + 0.0722 * psrc[x][bidx];
pdst[x] = (int)(color + 0.5);
int color = C1 * psrc[x][2-bidx] + C2 * psrc[x][1] + C3 * psrc[x][bidx];

pdst[x] = (int)(color + C4) * 0.0000152587890625;
}
}
}
48 changes: 26 additions & 22 deletions src/resize.cpp
Original file line number Diff line number Diff line change
@@ -39,7 +39,8 @@ void ImageResize(const cv::Mat &src, cv::Mat &dst, const cv::Size sz)
( (x1 == x2) ? (int)(q11 * (y2 - y) + q22 * (y - y1)) :
( (y1 == y2) ? (int)(q11 * (x2 - x) + q22 * (x - x1)) :
(int)(q11 * (x2 - x) * (y2 - y) + q21 * (x - x1) * (y2 - y) + q12 * (x2 - x) * (y - y1) + q22 * (x - x1) * (y - y1))));
ptr_dst[col] = (temp < 0) ? 0 : ((temp > 255) ? 255 : (uchar)temp);

ptr_dst[col] = (temp < 0) ? 0 : ((temp > 255) ? 255 : (uchar)temp);
}
}
}
@@ -50,39 +51,42 @@ void ImageResize_optimized(const cv::Mat &src, cv::Mat &dst, const cv::Size sz)
cv::Size sz_src = src.size();
dst.create(sz, src.type());

const int src_rows = src.rows;
const int src_cols = src.cols;
int src_rows = src.rows;
int src_cols = src.cols;

const int dst_rows = sz.height;
const int dst_cols = sz.width;
int dst_rows = sz.height;
int dst_cols = sz.width;
float xscale = (float)sz_src.width / sz.width;
float yscale = (float)sz_src.height / sz.height;

for (int row = 0; row < dst_rows; row++)
{
uchar *ptr_dst = dst.ptr<uchar>(row);

for (int col = 0; col < dst_cols; col++)
{
const float x = (((float)col) + .5f) * sz_src.width / sz.width - .5f;
const float y = (((float)row) + .5f) * sz_src.height / sz.height - .5f;
float x = (((float)col) + .5f) * xscale - .5f;
float y = (((float)row) + .5f) * yscale - .5f;

const int ix = (int)floor(x);
const int iy = (int)floor(y);
int ix = (x > 0) ? (int)x : (int)floor(x);
int iy = (y > 0) ? (int)y : (int)floor(y);

const int x1 = (ix < 0) ? 0 : ((ix >= src_cols) ? src_cols - 1 : ix);
const int x2 = (ix < 0) ? 0 : ((ix >= src_cols - 1) ? src_cols - 1 : ix + 1);
const int y1 = (iy < 0) ? 0 : ((iy >= src_rows) ? src_rows - 1 : iy);
const int y2 = (iy < 0) ? 0 : ((iy >= src_rows - 1) ? src_rows - 1 : iy + 1);
int x1 = ix;
int x2 = (ix >= src_cols - 1) ? src_cols - 1 : ix + 1;
int y1 = iy;
int y2 = (iy >= src_rows - 1) ? src_rows - 1 : iy + 1;

const uchar q11 = src.at<uchar>(y1, x1);
const uchar q12 = src.at<uchar>(y2, x1);
const uchar q21 = src.at<uchar>(y1, x2);
const uchar q22 = src.at<uchar>(y2, x2);
int q11 = src.at<uchar>(y1, x1);
int q12 = src.at<uchar>(y2, x1);
int q21 = src.at<uchar>(y1, x2);
int q22 = src.at<uchar>(y2, x2);

const int temp = ((x1 == x2) && (y1 == y2)) ? (int)q11 :
( (x1 == x2) ? (int)(q11 * (y2 - y) + q22 * (y - y1)) :
( (y1 == y2) ? (int)(q11 * (x2 - x) + q22 * (x - x1)) :
(int)(q11 * (x2 - x) * (y2 - y) + q21 * (x - x1) * (y2 - y) + q12 * (x2 - x) * (y - y1) + q22 * (x - x1) * (y - y1))));
ptr_dst[col] = (temp < 0) ? 0 : ((temp > 255) ? 255 : (uchar)temp);
int temp = ((x1 == x2) && (y1 == y2)) ? q11 :
( (x1 == x2) ? (q11 * (y2 - y) + q22 * (y - y1)) :
( (y1 == y2) ? (q11 * (x2 - x) + q22 * (x - x1)) :
(q11 * (x2 - x) * (y2 - y) + q21 * (x - x1) * (y2 - y) + q12 * (x2 - x) * (y - y1) + q22 * (x - x1) * (y - y1))));

ptr_dst[col] = (uchar)temp;
}
}
}
40 changes: 37 additions & 3 deletions src/thinning.cpp
Original file line number Diff line number Diff line change
@@ -58,7 +58,41 @@ void GuoHallThinning(const cv::Mat& src, cv::Mat& dst)
// Place optimized version here
//

static void GuoHallIteration_optimized(cv::Mat& im, int iter)
static void FormTable(std::vector<uchar>& markerTable)
{
for (int code = 0; code < 256; code++)
{
uchar p2 = code & 1;
uchar p3 = code & 2;
uchar p4 = code & 4;
uchar p5 = code & 8;
uchar p6 = code & 16;
uchar p7 = code & 32;
uchar p8 = code & 64;
uchar p9 = code & 128;
p2 == 0 ? p2 = 0 : p2 = 1;
p3 == 0 ? p3 = 0 : p3 = 1;
p4 == 0 ? p4 = 0 : p4 = 1;
p5 == 0 ? p5 = 0 : p5 = 1;
p6 == 0 ? p6 = 0 : p6 = 1;
p7 == 0 ? p7 = 0 : p7 = 1;
p8 == 0 ? p8 = 0 : p8 = 1;
p9 == 0 ? p9 = 0 : p9 = 1;
int C = (!p2 & (p3 | p4)) + (!p4 & (p5 | p6)) +
(!p6 & (p7 | p8)) + (!p8 & (p9 | p2));
int N1 = (p9 | p2) + (p3 | p4) + (p5 | p6) + (p7 | p8);
int N2 = (p2 | p3) + (p4 | p5) + (p6 | p7) + (p8 | p9);
int N = N1 < N2 ? N1 : N2;
int m = (p2 | p3 | !p5) & p4;

if (C == 1 && (N >= 2 && N <= 3) & (m == 0))
markerTable.push_back(1);
else
markerTable.push_back(0);
}
}

static void GuoHallIteration_optimized(cv::Mat& im, int iter, std::vector<uchar>& markerTable)
{
cv::Mat marker = cv::Mat::zeros(im.size(), CV_8UC1);

@@ -101,8 +135,8 @@ void GuoHallThinning_optimized(const cv::Mat& src, cv::Mat& dst)

do
{
GuoHallIteration_optimized(dst, 0);
GuoHallIteration_optimized(dst, 1);
GuoHallIteration(dst, 0);
GuoHallIteration(dst, 1);
cv::absdiff(dst, prev, diff);
dst.copyTo(prev);
}