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

Practice 4 compiler #105

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ project(${PROJECT_NAME})
set(LIBRARY "${PROJECT_NAME}_filter")
set(TESTS "test_${PROJECT_NAME}")
set(PERF_TESTS "perf_${PROJECT_NAME}")
set(CMAKE_CXX_FLAGS "-std=c++11")

# Find OpenCV
find_package(OpenCV REQUIRED)
Expand Down
1 change: 1 addition & 0 deletions include/skeleton_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ void GuoHallThinning(const cv::Mat& src, cv::Mat& dst);

// Optimized versions
void GuoHallThinning_optimized(const cv::Mat& src, cv::Mat& dst);
void GuoHallThinning_optimized_sq(const cv::Mat& src, cv::Mat& dst);
void ImageResize_optimized(const cv::Mat &src, cv::Mat &dst, const cv::Size sz);
73 changes: 53 additions & 20 deletions perf/perf_skeleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <iostream>

#include "skeleton_filter.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace std;
using namespace perf;
Expand All @@ -13,13 +14,24 @@ using std::tr1::get;
//
// Test(s) for the ConvertColor_BGR2GRAY_BT709 function
//
typedef perf::TestBaseWithParam<std::string> ImageName;

// PERF_TEST(skeleton, ConvertColor_BGR2GRAY_BT709)
// {
// Mat input = cv::imread("./bin/testdata/sla.png");
//
// // Add code here
// }
#define IMAGES testing::Values( std::string("./bin/testdata/sla.png"),\
std::string("./bin/testdata/page.png"),\
std::string("./bin/testdata/schedule.png"))

PERF_TEST_P(ImageName, cvt_color, IMAGES)
{
Mat input = cv::imread(GetParam());

Mat out;

TEST_CYCLE() {
ConvertColor_BGR2GRAY_BT709(input, out);
}

SANITY_CHECK_NOTHING();
}

//
// Test(s) for the ImageResize function
Expand Down Expand Up @@ -60,23 +72,43 @@ PERF_TEST_P(Size_Only, ImageResize, testing::Values(MAT_SIZES))
// Test(s) for the skeletonize function
//

// #define IMAGES testing::Values( std::string("./bin/testdata/sla.png"),\
// std::string("./bin/testdata/page.png"),\
// std::string("./bin/testdata/schedule.png") )
//
// typedef perf::TestBaseWithParam<std::string> ImageName;
//
// PERF_TEST_P(ImageName, skeletonize, IMAGES)
// {
// Mat input = cv::imread(GetParam());
//
// // Add code here
// }
PERF_TEST_P(ImageName, skeletonize, IMAGES)
{
Mat input = cv::imread(GetParam());

Mat out;

TEST_CYCLE() {
skeletonize(input, out, false);
}

SANITY_CHECK_NOTHING();
}


//
// Test(s) for the Thinning function
// Test(s) for the ConvertColor_BGR2GRAY_BT709 function
//

PERF_TEST_P(Size_Only, guo_hall, testing::Values(MAT_SIZES)) {
// Mat input = cv::imread(GetParam());

Mat chb(GetParam(), CV_8UC1); // One color type
randu(chb, Scalar::all(0), Scalar::all(255));

// Timeout without treshold and resize
// ConvertColor_BGR2GRAY_BT709(input, chb);

Mat out;

TEST_CYCLE() {
GuoHallThinning(chb, out);
}

SANITY_CHECK_NOTHING();
}

/*
PERF_TEST_P(Size_Only, Thinning, testing::Values(MAT_SIZES))
{
Size sz = GetParam();
Expand All @@ -94,11 +126,12 @@ PERF_TEST_P(Size_Only, Thinning, testing::Values(MAT_SIZES))
cv::Mat thinned_image;
TEST_CYCLE()
{
GuoHallThinning_optimized(image, thinned_image);
GuoHallThinning_optimized_sq(image, thinned_image);
}

cv::Mat diff; cv::absdiff(thinned_image, gold, diff);
ASSERT_EQ(0, cv::countNonZero(diff));

SANITY_CHECK(image);
}
*/
8 changes: 4 additions & 4 deletions src/convertcolor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ void ConvertColor_BGR2GRAY_BT709(const cv::Mat& src, cv::Mat& dst)

const int bidx = 0;

for (int y = 0; y < sz.height; y++)
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++)
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);
float color = 0.2126f * psrc[x][2-bidx] + 0.7152f * psrc[x][1] + 0.0722f * psrc[x][bidx];
pdst[x] = (uchar)(color + 0.5);
}
}
}
51 changes: 30 additions & 21 deletions src/resize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ void ImageResize(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;
const int src_rows = sz_src.height;
const int src_cols = sz_src.width;

const int dst_rows = sz.height;
const int dst_cols = sz.width;
Expand All @@ -35,9 +35,11 @@ void ImageResize(const cv::Mat &src, cv::Mat &dst, const cv::Size sz)
const uchar q21 = src.at<uchar>(y1, x2);
const uchar q22 = src.at<uchar>(y2, x2);

const int temp = (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);
const int temp = (x1 == x2 && y1 == y2) ? 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) ? uchar(0) : ((temp > 255) ? uchar(255) : (uchar)temp);
}
}
}
Expand All @@ -48,37 +50,44 @@ 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;

const int dst_rows = sz.height;
const int dst_cols = sz.width;

for (int row = 0; row < dst_rows; row++)
const float dw = ((float)sz_src.width) / sz.width;
const float bw = .5f*dw - .5f;

const float dh = ((float)sz_src.height) / sz.height;
const float bh = .5f*dh - .5f;

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

for (int col = 0; col < dst_cols; col++)
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;
const float x = (float)col * dw + bw;
const float y = (float)row * dh + bh;

const int ix = (int)floor(x);
const int iy = (int)floor(y);
const int ix = (int)(x);
const int iy = (int)(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 + 1;

int y1 = iy;
int y2 = 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);

const int temp = (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);
const int temp = (x1 == x2 && y1 == y2) ? 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] = (uchar)temp;
}
}
}
20 changes: 19 additions & 1 deletion src/skeleton_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,45 @@ void skeletonize(const cv::Mat &input, cv::Mat &output, bool save_images)

// Convert to grayscale
cv::Mat gray_image;
TS(cvt_color);
ConvertColor_BGR2GRAY_BT709(input, gray_image);
TE(cvt_color);
TS(imwrite_1);
if (save_images) cv::imwrite("1-convertcolor.png", gray_image);
TE(imwrite_1);

// Downscale input image
cv::Mat small_image;
cv::Size small_size(input.cols / 1.5, input.rows / 1.5);
cv::Size small_size((int)(input.cols / 1.5), (int)(input.rows / 1.5) );
TS(image_resize);
ImageResize(gray_image, small_image, small_size);
TE(image_resize);
TS(imwrite_2);
if (save_images) cv::imwrite("2-resize.png", small_image);
TE(imwrite_2);

// Binarization and inversion
TS(threshold);
cv::threshold(small_image, small_image, 128, 255, cv::THRESH_BINARY_INV);
TE(threshold);
TS(imwrite_3);
if (save_images) cv::imwrite("3-threshold.png", small_image);
TE(imwrite_3);

// Thinning
cv::Mat thinned_image;
TS(guo_hall);
GuoHallThinning(small_image, thinned_image);
TE(guo_hall);
TS(imwrite_4);
if (save_images) cv::imwrite("4-thinning.png", thinned_image);
TE(imwrite_4);

// Back inversion
output = 255 - thinned_image;
TS(imwrite_5);
if (save_images) cv::imwrite("5-output.png", output);
TE(imwrite_5);

TE(total);
}
Loading