Skip to content

Commit

Permalink
0.20200319: add blur args
Browse files Browse the repository at this point in the history
  • Loading branch information
zvezdochiot committed Mar 19, 2020
1 parent 3d4c4e2 commit 1a7d6f3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 49 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Build:

Usage:

./aithreshold in.png out.png [T=0.15] [part=8] [blur=0]

Example:

./aithreshold images/image.png images/output.png

image.png
Expand Down
26 changes: 26 additions & 0 deletions man/man1/aithreshold.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.TH "AIThreshold" 1 "19 Mar 2020" 0.20200319 " User Manual"

.SH NAME
aithreshold

.SH SYNOPSIS
aithreshold <image_in> <image_out> [T=0.15] [part=8] [blur=0]

.SH DESCRIPTION
Adaptive Thresholding Using the Integral Image

.SH EXAMPLE
aithreshold image.png output.png 0.1 16 1

.SH COPYRIGHT
Copyright 2017-2020 zvezdochiot.
All rights reserved.

.SH SEE ALSO
convert(1),
imgtxtenh(1),
imthreshold(1)

.SH CONTACTS
Homepage: https://github.com/zvezdochiot/aithreshold
Email: zvezdochiot@users.sourceforge.net
79 changes: 30 additions & 49 deletions src/aithreshold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@

using namespace std;


void thresholdIntegral(cv::Mat &inputMat, cv::Mat &outputMat, float T, int part)
{
int nRows, nCols, S, s2, i, j;
int x1, y1, x2, y2, count, sum, ip, it;
cv::Mat sumMat;
int *p_y1, *p_y2;
uchar *p_inputMat, *p_outputMat;

// accept only char type matrices
CV_Assert(!inputMat.empty());
CV_Assert(inputMat.depth() == CV_8U);
Expand All @@ -17,91 +22,64 @@ void thresholdIntegral(cv::Mat &inputMat, cv::Mat &outputMat, float T, int part)
CV_Assert(outputMat.channels() == 1);

// rows -> height -> y
int nRows = inputMat.rows;
nRows = inputMat.rows;
// cols -> width -> x
int nCols = inputMat.cols;
nCols = inputMat.cols;

// create the integral image
cv::Mat sumMat;
cv::integral(inputMat, sumMat);

CV_Assert(sumMat.depth() == CV_32S);
CV_Assert(sizeof(int) == 4);

int S = MAX(nRows, nCols) / part;
S = MAX(nRows, nCols);
S = (S > (part + part)) ? (S / part) : 2;

// perform thresholding
int s2 = S/2;
int x1, y1, x2, y2, count, sum;

// CV_Assert(sizeof(int) == 4);
int *p_y1, *p_y2;
uchar *p_inputMat, *p_outputMat;
s2 = S/2;

for( int i = 0; i < nRows; ++i)
for(i = 0; i < nRows; ++i)
{
y1 = i-s2;
y2 = i+s2;

if (y1 < 0){
y1 = 0;
}
if (y2 >= nRows) {
y2 = nRows-1;
}
y1 = (i < s2) ? 0 : (i - s2);
y2 = ((i + s2) < nRows) ? (i + s2) : (nRows - 1);

p_y1 = sumMat.ptr<int>(y1);
p_y2 = sumMat.ptr<int>(y2);
p_inputMat = inputMat.ptr<uchar>(i);
p_outputMat = outputMat.ptr<uchar>(i);

for ( int j = 0; j < nCols; ++j)
for (j = 0; j < nCols; ++j)
{
// set the SxS region
x1 = j-s2;
x2 = j+s2;
x1 = (j < s2) ? 0 : (j - s2);
x2 = ((j + s2) < nCols) ? (j + s2) : (nCols - 1);

if (x1 < 0) {
x1 = 0;
}
if (x2 >= nCols) {
x2 = nCols-1;
}

count = (x2-x1)*(y2-y1);
count = (x2 - x1) * (y2 - y1);

// I(x,y)=s(x2,y2)-s(x1,y2)-s(x2,y1)+s(x1,x1)
sum = p_y2[x2] - p_y1[x2] - p_y2[x1] + p_y1[x1];

if ((int)(p_inputMat[j] * count) < (int)(sum * (1.0f - T)))
p_outputMat[j] = 0;
else
p_outputMat[j] = 255;
ip = (int)(p_inputMat[j] * count);
it = (int)(sum * (1.0f - T));
p_outputMat[j] = (ip < it) ? 0 : 255;
}
}
}


int main(int argc, char *argv[])
{
float T = 0.15f;
int part = 8;
int part = 8, blur = 0;
printf("Adaptive Integral Threshold.\n");
if (argc < 3)
{
printf("Usage: aithreshold image_in image_out [T=0.15] [part=8]\n");
printf("Usage: aithreshold image_in image_out [T=0.15] [part=8] [blur=0]\n");
return 0;
}
// Load the image
cv::Mat src = cv::imread(argv[1]);
if (argc > 3)
{
T = atof(argv[3]);
}
if (argc > 4)
{
part = atoi(argv[4]);
}
if (argc > 3) T = atof(argv[3]);
if (argc > 4) part = atoi(argv[4]);
if (argc > 5) blur = atoi(argv[5]);

// Check if image is loaded fine
if(src.empty()) {
Expand All @@ -121,11 +99,14 @@ int main(int argc, char *argv[])
gray = src;
}

printf("T: %f, part: %d, blur: %d\n", T, part, blur);
blur++;
if (blur > 1) cv::blur(gray, gray, cv::Size(blur, blur));
cv::Mat bw2 = cv::Mat::zeros(gray.size(), CV_8UC1);
thresholdIntegral(gray, bw2, T, part);
printf("%s -> %s\n", argv[1], argv[2]);

// Write threshold image
cv::imwrite(argv[2],bw2);
cv::imwrite(argv[2], bw2);
return 0;
}

0 comments on commit 1a7d6f3

Please sign in to comment.