Skip to content

Instantly share code, notes, and snippets.

@chfanghr
Created November 5, 2018 06:24
Show Gist options
  • Save chfanghr/b83f279f1aa3f3a879cdd85428375e85 to your computer and use it in GitHub Desktop.
Save chfanghr/b83f279f1aa3f3a879cdd85428375e85 to your computer and use it in GitHub Desktop.
边缘平滑
//去除二值图像边缘的突出部
//uthreshold、vthreshold分别表示突出部的宽度阈值和高度阈值
//type代表突出部的颜色,0表示黑色,1代表白色
void delete_jut(Mat& src, Mat& dst, int uthreshold, int vthreshold, int type)
{
int threshold;
src.copyTo(dst);
int height = dst.rows;
int width = dst.cols;
int k; //用于循环计数传递到外部
for (int i = 0; i < height - 1; i++)
{
uchar* p = dst.ptr<uchar>(i);
for (int j = 0; j < width - 1; j++)
{
if (type == 0)
{
//行消除
if (p[j] == 255 && p[j + 1] == 0)
{
if (j + uthreshold >= width)
{
for (int k = j + 1; k < width; k++)
p[k] = 255;
}
else
{
for (k = j + 2; k <= j + uthreshold; k++)
{
if (p[k] == 255) break;
}
if (p[k] == 255)
{
for (int h = j + 1; h < k; h++)
p[h] = 255;
}
}
}
//列消除
if (p[j] == 255 && p[j + width] == 0)
{
if (i + vthreshold >= height)
{
for (k = j + width; k < j + (height - i)*width; k += width)
p[k] = 255;
}
else
{
for (k = j + 2 * width; k <= j + vthreshold*width; k += width)
{
if (p[k] == 255) break;
}
if (p[k] == 255)
{
for (int h = j + width; h < k; h += width)
p[h] = 255;
}
}
}
}
else //type = 1
{
//行消除
if (p[j] == 0 && p[j + 1] == 255)
{
if (j + uthreshold >= width)
{
for (int k = j + 1; k < width; k++)
p[k] = 0;
}
else
{
for (k = j + 2; k <= j + uthreshold; k++)
{
if (p[k] == 0) break;
}
if (p[k] == 0)
{
for (int h = j + 1; h < k; h++)
p[h] = 0;
}
}
}
//列消除
if (p[j] == 0 && p[j + width] == 255)
{
if (i + vthreshold >= height)
{
for (k = j + width; k < j + (height - i)*width; k += width)
p[k] = 0;
}
else
{
for (k = j + 2 * width; k <= j + vthreshold*width; k += width)
{
if (p[k] == 0) break;
}
if (p[k] == 0)
{
for (int h = j + width; h < k; h += width)
p[h] = 0;
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment