mirror of
https://github.com/Karaka-Management/cOMS.git
synced 2026-02-07 14:28:40 +00:00
init c/c++ repo
This commit is contained in:
parent
84c736b55c
commit
92a9bec278
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
*.out
|
||||
.directory
|
||||
*.cmake
|
||||
CMakeCache.txt
|
||||
Makefile
|
||||
CMakeFiles
|
||||
App
|
||||
42
Image/ImageUtils.h
Normal file
42
Image/ImageUtils.h
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
/**
|
||||
* Karaka
|
||||
*
|
||||
* PHP Version 8.0
|
||||
*
|
||||
* @package Image
|
||||
* @copyright Dennis Eichhorn
|
||||
* @license OMS License 1.0
|
||||
* @version 1.0.0
|
||||
* @link https://karaka.app
|
||||
*/
|
||||
#ifndef IMAGE_IMAGE_UTILS_H
|
||||
#define IMAGE_IMAGE_UTILS_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
namespace Image {
|
||||
class ImageUtils {
|
||||
private:
|
||||
|
||||
public:
|
||||
static inline
|
||||
float lightnessFromRgb(int r, int g, int b)
|
||||
{
|
||||
float vR = r / 255.0;
|
||||
float vG = g / 255.0;
|
||||
float vB = b / 255.0;
|
||||
|
||||
float lR = vR <= 0.04045 ? vR / 12.92 : pow(((vR + 0.055) / 1.055), 2.4);
|
||||
float lG = vG <= 0.04045 ? vG / 12.92 : pow(((vG + 0.055) / 1.055), 2.4);
|
||||
float lB = vB <= 0.04045 ? vB / 12.92 : pow(((vB + 0.055) / 1.055), 2.4);
|
||||
|
||||
float y = 0.2126 * lR + 0.7152 * lG + 0.0722 * lB;
|
||||
float lStar = y <= 216.0 / 24389.0 ? y * 24389.0 / 27.0 : pow(y, (1.0 / 3.0)) * 116.0 - 16.0;
|
||||
|
||||
return lStar / 100.0;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
104
Image/Skew.h
Normal file
104
Image/Skew.h
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
/**
|
||||
* Karaka
|
||||
*
|
||||
* PHP Version 8.0
|
||||
*
|
||||
* @package Image
|
||||
* @copyright Dennis Eichhorn
|
||||
* @license OMS License 1.0
|
||||
* @version 1.0.0
|
||||
* @link https://karaka.app
|
||||
*/
|
||||
#ifndef IMAGE_SKEW_H
|
||||
#define IMAGE_SKEW_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include <vector>
|
||||
|
||||
#define deg2rad(angle) \
|
||||
({ __typeof__ (angle) _angle = (angle); \
|
||||
(_angle) * M_PI / 180.0; })
|
||||
|
||||
#define rad2deg(angle) \
|
||||
({ __typeof__ (angle) _angle = (angle); \
|
||||
(_angle) * 180.0 / M_PI; })
|
||||
|
||||
namespace Image {
|
||||
class Skew {
|
||||
private:
|
||||
|
||||
public:
|
||||
static inline
|
||||
cv::Mat deskewHoughLines(cv::Mat in, int maxDegree = 45)
|
||||
{
|
||||
cv::Size dim = in.size();
|
||||
|
||||
cv::Mat inv;
|
||||
cv::cvtColor(in, inv, cv::COLOR_BGR2GRAY);
|
||||
cv::threshold(inv, inv, 0, 255, cv::THRESH_BINARY_INV | cv::THRESH_OTSU);
|
||||
|
||||
std::vector<cv::Vec4i> lines;
|
||||
cv::HoughLinesP(inv, lines, 1.0, M_PI / 180, 200, dim.width / 12, dim.width / 150);
|
||||
|
||||
int imageOrientation = 0; // > 0 -> horizontal
|
||||
|
||||
std::vector<float> tmpAngles;
|
||||
for (int i = 0; i < lines.size(); ++i) {
|
||||
float angle = atan2(lines[i][3] - lines[i][1], lines[i][2] - lines[i][0]);
|
||||
tmpAngles.push_back(angle);
|
||||
|
||||
imageOrientation += abs(angle) > M_PI / 4 ? 1 : -1;
|
||||
}
|
||||
|
||||
std::vector<float> angles;
|
||||
for (int i = 0; i < tmpAngles.size(); ++i) {
|
||||
if (imageOrientation > 0) {
|
||||
if (deg2rad(90 - maxDegree) < abs(tmpAngles[i]) < deg2rad(90 + maxDegree)) {
|
||||
angles.push_back(tmpAngles[i]);
|
||||
}
|
||||
} else {
|
||||
if (abs(tmpAngles[i]) < deg2rad(maxDegree)) {
|
||||
angles.push_back(tmpAngles[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (angles.size() < 5) {
|
||||
return in;
|
||||
}
|
||||
|
||||
float median = 0.0;
|
||||
for (int i = 0; i < angles.size(); ++i) {
|
||||
median += angles[i];
|
||||
}
|
||||
|
||||
float angleDeg = rad2deg(median / angles.size());
|
||||
|
||||
cv::Mat orientFix;
|
||||
if (imageOrientation > 0) {
|
||||
if (angleDeg < 0) {
|
||||
cv::rotate(in, orientFix, cv::ROTATE_90_CLOCKWISE);
|
||||
angleDeg += 90.0;
|
||||
} else if (angleDeg > 0) {
|
||||
cv::rotate(in, orientFix, cv::ROTATE_90_COUNTERCLOCKWISE);
|
||||
angleDeg -= 90.0;
|
||||
} else {
|
||||
orientFix = in;
|
||||
}
|
||||
} else {
|
||||
orientFix = in;
|
||||
}
|
||||
|
||||
cv::Mat rot = cv::getRotationMatrix2D(cv::Point2f(dim.width / 2, dim.height / 2), angleDeg, 1.0);
|
||||
|
||||
cv::Mat out;
|
||||
cv::warpAffine(orientFix, out, rot, dim, cv::INTER_LINEAR, cv::BORDER_REPLICATE);
|
||||
|
||||
return out;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
92
Image/Thresholding.h
Normal file
92
Image/Thresholding.h
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
/**
|
||||
* Karaka
|
||||
*
|
||||
* PHP Version 8.0
|
||||
*
|
||||
* @package Image
|
||||
* @copyright Dennis Eichhorn
|
||||
* @license OMS License 1.0
|
||||
* @version 1.0.0
|
||||
* @link https://karaka.app
|
||||
*/
|
||||
#ifndef IMAGE_THRESHOLDING_H
|
||||
#define IMAGE_THRESHOLDING_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <opencv2/opencv.hpp>
|
||||
|
||||
#include "ImageUtils.h"
|
||||
|
||||
#define max(a, b) \
|
||||
({ __typeof__ (a) _a = (a); \
|
||||
__typeof__ (b) _b = (b); \
|
||||
_a > _b ? _a : _b; })
|
||||
|
||||
#define min(a, b) \
|
||||
({ __typeof__ (a) _a = (a); \
|
||||
__typeof__ (b) _b = (b); \
|
||||
_a < _b ? _a : _b; })
|
||||
|
||||
namespace Image {
|
||||
class Thresholding {
|
||||
private:
|
||||
|
||||
public:
|
||||
static inline
|
||||
cv::Mat integralThresholding(cv::Mat in)
|
||||
{
|
||||
cv::Size dim = in.size();
|
||||
cv::Mat out(in.size(), in.type());
|
||||
|
||||
float intImg[dim.width][dim.height];
|
||||
float sum;
|
||||
|
||||
cv::Vec3b bgr;
|
||||
for (int i = 0; i < dim.width; ++i) {
|
||||
sum = 0.0;
|
||||
|
||||
for (int j = 0; j < dim.height; ++j) {
|
||||
bgr = in.at<cv::Vec3b>(j, i);
|
||||
sum += Image::ImageUtils::lightnessFromRgb(bgr[2], bgr[1], bgr[0]);
|
||||
|
||||
intImg[i][j] = i == 0 ? sum : intImg[i - 1][j] + sum;
|
||||
}
|
||||
}
|
||||
|
||||
int s = dim.width / 96.0;
|
||||
int t = 30;
|
||||
|
||||
int x1, x2;
|
||||
int y1, y2;
|
||||
int count;
|
||||
float brightness;
|
||||
int color;
|
||||
|
||||
for (int i = 0; i < dim.width; ++i) {
|
||||
for (int j = 0; j < dim.height; ++j) {
|
||||
x1 = max(1, i - s / 2.0);
|
||||
x2 = min(i + s / 2.0, dim.width - 1);
|
||||
|
||||
y1 = max(1, j - s / 2.0);
|
||||
y2 = min(j + s / 2.0, dim.height - 1);
|
||||
|
||||
count = (x2 - x1) * (y2 - y1);
|
||||
sum = intImg[x2][y2] - intImg[x2][y1 - 1] - intImg[x1 - 1][y2] + intImg[x1 - 1][y1 - 1];
|
||||
|
||||
bgr = in.at<cv::Vec3b>(j, i);
|
||||
brightness = Image::ImageUtils::lightnessFromRgb(bgr[2], bgr[1], bgr[0]);
|
||||
|
||||
color = brightness * count <= (sum * (100.0 - t) / 100.0) ? 0 : 255;
|
||||
|
||||
out.at<cv::Vec3b>(j, i)[0] = color;
|
||||
out.at<cv::Vec3b>(j, i)[1] = color;
|
||||
out.at<cv::Vec3b>(j, i)[2] = color;
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
6
Tools/InvoicePreprocessing/CMakeLists.txt
Normal file
6
Tools/InvoicePreprocessing/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
cmake_minimum_required(VERSION 2.8)
|
||||
project( App )
|
||||
find_package( OpenCV REQUIRED )
|
||||
include_directories( ${OpenCV_INCLUDE_DIRS} )
|
||||
add_executable( App main.cpp )
|
||||
target_link_libraries( App ${OpenCV_LIBS} )
|
||||
46
Tools/InvoicePreprocessing/main.cpp
Normal file
46
Tools/InvoicePreprocessing/main.cpp
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
/**
|
||||
* Karaka
|
||||
*
|
||||
* PHP Version 8.0
|
||||
*
|
||||
* @package App
|
||||
* @copyright Dennis Eichhorn
|
||||
* @license OMS License 1.0
|
||||
* @version 1.0.0
|
||||
* @link https://karaka.app
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <opencv2/opencv.hpp>
|
||||
|
||||
#include "../../Image/Skew.h"
|
||||
#include "../../Image/Thresholding.h"
|
||||
|
||||
int main(int argc, char** argv )
|
||||
{
|
||||
if (argc != 3) {
|
||||
printf("A input image and a output image is required\n");
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
cv::Mat in;
|
||||
in = cv::imread(argv[1], cv::IMREAD_UNCHANGED);
|
||||
if (!in.data) {
|
||||
printf("Couldn't read image.\n");
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
cv::Mat out = Image::Thresholding::integralThresholding(in);
|
||||
out = Image::Skew::deskewHoughLines(out);
|
||||
|
||||
cv::imwrite(argv[2], out);
|
||||
|
||||
/*
|
||||
cv::imshow("in", in);
|
||||
cv::imshow("out", out);
|
||||
cv::waitKey(0);
|
||||
*/
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user