reduces class usage and impl. img diff function

This commit is contained in:
Dennis Eichhorn 2022-11-20 15:46:44 +01:00
parent 65f3b94cdb
commit 200a154874
8 changed files with 196 additions and 160 deletions

47
Image/Diff.h Normal file
View File

@ -0,0 +1,47 @@
/**
* Karaka
*
* @package Image
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://karaka.app
*/
#ifndef IMAGE_DIFF_H
#define IMAGE_DIFF_H
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include "../Utils/MathUtils.h"
namespace Image {
namespace ImageUtils {
cv::Mat find_diff (cv::Mat in1, cv::Mat in2)
{
cv::Mat diff;
cv::absdiff(in1, in2, diff);
int th = 10;
cv::Mat mask(in1.size(), CV_8UC1);
for (int i = 0; i < diff.rows; ++i) {
for (int j = 0; j < diff.cols; ++j) {
cv::Vec3b px = diff.at<cv::Vec3b>(i, j);
int value = px[0] + px[1] + px[2];
if (value > th) {
mask.at<unsigned char>(i, j) = 255;
}
}
}
cv::Mat out;
cv::bitwise_and(in2, in2, out, mask);
return out;
}
}
}
#endif

View File

@ -14,37 +14,34 @@
#include <math.h> #include <math.h>
namespace Image { namespace Image {
class ImageUtils { namespace ImageUtils {
private: inline
float lightnessFromRgb(int r, int g, int b)
{
float vR = r / 255.0;
float vG = g / 255.0;
float vB = b / 255.0;
public: float lR = vR <= 0.04045 ? vR / 12.92 : pow(((vR + 0.055) / 1.055), 2.4);
static inline float lG = vG <= 0.04045 ? vG / 12.92 : pow(((vG + 0.055) / 1.055), 2.4);
float lightnessFromRgb(int r, int g, int b) float lB = vB <= 0.04045 ? vB / 12.92 : pow(((vB + 0.055) / 1.055), 2.4);
{
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 y = 0.2126 * lR + 0.7152 * lG + 0.0722 * lB;
float lG = vG <= 0.04045 ? vG / 12.92 : pow(((vG + 0.055) / 1.055), 2.4); float lStar = y <= 216.0 / 24389.0 ? y * 24389.0 / 27.0 : pow(y, (1.0 / 3.0)) * 116.0 - 16.0;
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; return lStar / 100.0;
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; inline
} int rgbToInt(int r, int g, int b)
{
int rgb = r;
rgb = (rgb << 8) + g;
rgb = (rgb << 8) + b;
static inline return rgb;
int rgbToInt(int r, int g, int b) }
{ }
int rgb = r;
rgb = (rgb << 8) + g;
rgb = (rgb << 8) + b;
return rgb;
}
};
} }
#endif #endif

View File

@ -61,23 +61,19 @@ namespace Image {
{-1.0 / 256.0, -4.0 / 256.0, -6.0 / 256.0, -4.0 / 256.0, -1.0 / 256.0}, {-1.0 / 256.0, -4.0 / 256.0, -6.0 / 256.0, -4.0 / 256.0, -1.0 / 256.0},
}; };
class Kernel { namespace Kernel {
private: inline
cv::Mat convolve(cv::Mat in, const float kernel[][3])
{
cv::Size dim = in.size();
cv::Mat out(in.size(), in.type());
public: cv::Mat mKernel(3, 3, CV_32F, (float *) kernel);
cv::filter2D(in, out, -1, mKernel);
static return out;
cv::Mat convolve(cv::Mat in, const float kernel[][3]) }
{ }
cv::Size dim = in.size();
cv::Mat out(in.size(), in.type());
cv::Mat mKernel(3, 3, CV_32F, (float *) kernel);
cv::filter2D(in, out, -1, mKernel);
return out;
}
};
} }
#endif #endif

View File

@ -17,79 +17,75 @@
#include "../Utils/MathUtils.h" #include "../Utils/MathUtils.h"
namespace Image { namespace Image {
class Skew { namespace Skew {
private: cv::Mat deskewHoughLines(cv::Mat in, int maxDegree = 45)
{
cv::Size dim = in.size();
public: cv::Mat inv;
static cv::cvtColor(in, inv, cv::COLOR_BGR2GRAY);
cv::Mat deskewHoughLines(cv::Mat in, int maxDegree = 45) cv::threshold(inv, inv, 0, 255, cv::THRESH_BINARY_INV | cv::THRESH_OTSU);
{
cv::Size dim = in.size();
cv::Mat inv; std::vector<cv::Vec4i> lines;
cv::cvtColor(in, inv, cv::COLOR_BGR2GRAY); cv::HoughLinesP(inv, lines, 1.0, OMS_PI / 180, 200, dim.width / 12, dim.width / 150);
cv::threshold(inv, inv, 0, 255, cv::THRESH_BINARY_INV | cv::THRESH_OTSU);
std::vector<cv::Vec4i> lines; int imageOrientation = 0; // > 0 -> image is horizontal
cv::HoughLinesP(inv, lines, 1.0, OMS_PI / 180, 200, dim.width / 12, dim.width / 150);
int imageOrientation = 0; // > 0 -> image is 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);
std::vector<float> tmpAngles; imageOrientation += oms_abs(angle) > OMS_PI / 4 ? 1 : -1;
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 += oms_abs(angle) > OMS_PI / 4 ? 1 : -1; std::vector<float> angles;
} for (int i = 0; i < tmpAngles.size(); ++i) {
std::vector<float> angles;
for (int i = 0; i < tmpAngles.size(); ++i) {
if (imageOrientation > 0) {
if (oms_deg2rad(90 - maxDegree) < oms_abs(tmpAngles[i]) && oms_abs(tmpAngles[i]) < oms_deg2rad(90 + maxDegree)) {
angles.push_back(tmpAngles[i]);
}
} else {
if (oms_abs(tmpAngles[i]) < oms_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 = oms_rad2deg(median / angles.size());
cv::Mat orientFix;
if (imageOrientation > 0) { if (imageOrientation > 0) {
if (angleDeg < 0) { if (oms_deg2rad(90 - maxDegree) < oms_abs(tmpAngles[i]) && oms_abs(tmpAngles[i]) < oms_deg2rad(90 + maxDegree)) {
cv::rotate(in, orientFix, cv::ROTATE_90_CLOCKWISE); angles.push_back(tmpAngles[i]);
angleDeg += 90.0;
} else if (angleDeg > 0) {
cv::rotate(in, orientFix, cv::ROTATE_90_COUNTERCLOCKWISE);
angleDeg -= 90.0;
} else {
orientFix = in;
} }
} else {
if (oms_abs(tmpAngles[i]) < oms_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 = oms_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 { } else {
orientFix = in; orientFix = in;
} }
} else {
cv::Mat rot = cv::getRotationMatrix2D(cv::Point2f(dim.width / 2, dim.height / 2), angleDeg, 1.0); orientFix = in;
cv::Mat out;
cv::warpAffine(orientFix, out, rot, dim, cv::INTER_LINEAR, cv::BORDER_REPLICATE);
return out;
} }
};
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 #endif

View File

@ -17,67 +17,63 @@
#include "../Utils/MathUtils.h" #include "../Utils/MathUtils.h"
namespace Image { namespace Image {
class Thresholding { namespace Thresholding {
private: cv::Mat integralThresholding(cv::Mat in)
{
cv::Size dim = in.size();
cv::Mat out(dim, in.type());
public: float *intImg = (float *) malloc(dim.width * dim.height * sizeof(float));
static float sum;
cv::Mat integralThresholding(cv::Mat in)
{
cv::Size dim = in.size();
cv::Mat out(dim, in.type());
float *intImg = (float *) malloc(dim.width * dim.height * sizeof(float)); cv::Vec3b bgr;
float sum; for (int i = 0; i < dim.width; ++i) {
sum = 0.0;
cv::Vec3b bgr; for (int j = 0; j < dim.height; ++j) {
for (int i = 0; i < dim.width; ++i) { bgr = in.at<cv::Vec3b>(j, i);
sum = 0.0; sum += Image::ImageUtils::lightnessFromRgb(bgr[2], bgr[1], bgr[0]);
for (int j = 0; j < dim.height; ++j) { intImg[i * j] = i == 0 ? sum : intImg[(i - 1) * j] + sum;
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 = oms_max(1, i - s / 2.0);
x2 = oms_min(i + s / 2.0, dim.width - 1);
y1 = oms_max(1, j - s / 2.0);
y2 = oms_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) && brightness < 0.95 ? 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;
}
}
free(intImg);
return out;
} }
};
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 = oms_max(1, i - s / 2.0);
x2 = oms_min(i + s / 2.0, dim.width - 1);
y1 = oms_max(1, j - s / 2.0);
y2 = oms_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) && brightness < 0.95 ? 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;
}
}
free(intImg);
return out;
}
}
} }
#endif #endif

View File

@ -39,6 +39,7 @@ namespace Utils {
return cwd; return cwd;
} }
inline
void chdir_application(char *cwd, char *arg) void chdir_application(char *cwd, char *arg)
{ {
#ifdef _WIN32 #ifdef _WIN32
@ -80,6 +81,7 @@ namespace Utils {
#endif #endif
} }
inline
const char *compile_arg_line(int argc, char **argv) const char *compile_arg_line(int argc, char **argv)
{ {
size_t max = 512; size_t max = 512;

View File

@ -10,6 +10,9 @@
#ifndef UTILS_FILE_UTILS_H #ifndef UTILS_FILE_UTILS_H
#define UTILS_FILE_UTILS_H #define UTILS_FILE_UTILS_H
#include <stdio.h>
#include <string.h>
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#include <stdio.h> #include <stdio.h>
@ -88,6 +91,7 @@ namespace Utils {
#endif #endif
} }
inline
const char* file_extension (const char *filename) const char* file_extension (const char *filename)
{ {
char *dot = strrchr((char *) filename, '.'); char *dot = strrchr((char *) filename, '.');
@ -104,7 +108,6 @@ namespace Utils {
int size; int size;
} file_body; } file_body;
inline
file_body read_file (const char *filename) file_body read_file (const char *filename)
{ {
file_body file = {0}; file_body file = {0};

View File

@ -19,7 +19,6 @@
namespace Utils { namespace Utils {
namespace WebUtils { namespace WebUtils {
static
int write_download_data (void *ptr, size_t size, size_t nmeb, void *stream) int write_download_data (void *ptr, size_t size, size_t nmeb, void *stream)
{ {
Utils::FileUtils::file_body *out = (Utils::FileUtils::file_body *) stream; Utils::FileUtils::file_body *out = (Utils::FileUtils::file_body *) stream;
@ -42,7 +41,7 @@ namespace Utils {
return out->size; return out->size;
} }
static inline
Utils::FileUtils::file_body download (char *url) Utils::FileUtils::file_body download (char *url)
{ {
Utils::FileUtils::file_body page = {0}; Utils::FileUtils::file_body page = {0};