#include
#include<opencv.hpp>
using namespace cv;
using namespace std;
/*
Function: Find a straight line in the input image
Input and output: The input image is grayscale raw, the return value is dst, and the return value is a white line
lines = cvHoughLines2(canny, stor, CV_HOUGH_PROBABILISTIC, 1, CV_PI / 180, 80, 200, 30);
The 200 in the parameter means that the length of the line to be searched is more than 200 pixels;
The 30 in the parameter refers to two line segments on the same line, if they are less than 30 apart, then connect them
*/
void findLines(IplImage* raw, IplImage* dst)
{
IplImage* src = cvCloneImage(raw); // clone the input image
IplImage* canny = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1); // create a tmp image head to save gradient image
cvCanny(src, canny, 20, 200, 3); // Generate its gradient image
CvMemStorage* stor = cvCreateMemStorage(0);
CvSeq* lines = NULL;
// find a line whose length bigger than 200 pixels
lines = cvHoughLines2(canny, stor, CV_HOUGH_PROBABILISTIC, 1, CV_PI / 180, 80, 200, 30);
cvZero(dst);
CvPoint maxStart, maxEnd; // save the coordinate of the head and rear of the line we want
int maxDistance = 0; // The maximum distance of all lines found by [cvHoughLines2]
for (int i = 0; i < lines->total; i++) // lines->total: the number of lines
{
// variable ‘lines’ is a sequence, [cvGetSeqElem] gets the (i)th line, and it returns its head and rear.
CvPoint* line = (CvPoint*)cvGetSeqElem(lines, i);
// line[0] and line[1] is respectively the line’s coordinate of its head and rear
if (abs(line[0].x - line[1].x) > maxDistance)
{/* It’s a trick because the line is almost horizontal.
strictly, it should be
sqrt((line[0].x - line[1].x)(line[0].x - line[1].x)+(line[0].y - line[1].y)(line[0].x - line[1].x))
/
maxDistance = abs(line[0].x - line[1].x);
maxStart = line[0];
maxEnd = line[1];
}
}
cvLine(dst, maxStart, maxEnd, cvScalar(255), 1); // draw the white line[cvScalar(255)] in a black background
cvReleaseImage(&src); // free the memory
cvReleaseMemStorage(&stor);
}
/
Function: erase small blocks with area less than [15 pixels]
Input and output: no return value, directly operate on the input image
/
void erase(IplImage raw)
{
IplImage* src = cvCloneImage(raw);
/Binarization and inverse the black and white because the function next only find white area while
the word in image is black./
cvThreshold(src, src, 120, 255, CV_THRESH_BINARY_INV);
// create some space to save the white areas but we access it via variable ‘cont’
CvMemStorage* stor = cvCreateMemStorage(0);
CvSeq* cont;
cvFindContours(src, stor, &cont, sizeof(CvContour), CV_RETR_EXTERNAL); // find the white regions
for (; cont; cont = cont->h_next) // Traversal
{
if (fabs(cvContourArea(cont)) < 15) // if its Area smaller than 15, we fill it with white[cvScalar(255)]
cvDrawContours(raw, cont, cvScalar(255), cvScalar(255), 0, CV_FILLED, 8);
}
cvReleaseImage(&src);
}
int main()
{
IplImage* src = cvLoadImage(“E:\fengce\0726\1.png”);
cvNamedWindow(“Original”, 1);
cvShowImage(“Original”, src);
IplImage* gray = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
IplImage* canny = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
IplImage* dst = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
IplImage* binary = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
cvCvtColor(src, gray, CV_RGB2GRAY);
cvThreshold(gray, binary, 120, 255, CV_THRESH_OTSU);
findLines(gray, dst);
cvNamedWindow("dst", 1);
cvShowImage("dst", dst);
for (int row = 0; row < binary->height; row++)
for (int col = 0; col < binary->width; col++)
{
if (cvGet2D(dst, row, col).val[0] == 255)
{
int up = 0, down = 0;
int white = 0;
for (int i = row; i >= 0; i--)
{
if (cvGet2D(binary, i, col).val[0] == 0)
{
up++;
white = 0;
}
else white++;
if (white > 2) break;
}
white = 0;
for (int i = row; i < binary->height; i++)
{
if (cvGet2D(binary, i, col).val[0] == 0)
{
down++;
white = 0;
}
else white++;
if (white > 2) break;
}
if (up + down < 8)
{
for (int i = -up; i <= down; i++) cvSet2D(binary, row + i, col, cvScalar(255));
}
}
}
cvNamedWindow("result", 1);
cvShowImage("result", binary);
erase(binary);
cvDilate(binary, binary, NULL, 1);
cvErode(binary, binary, NULL, 1);
cvNamedWindow("Expansion corrosion", 1);
cvShowImage("Expansion corrosion", binary);
cvSaveImage("D:/result.png", binary);
cvReleaseImage(&src);
cvReleaseImage(&canny);
cvReleaseImage(&gray);
cvReleaseImage(&dst);
cvReleaseImage(&binary);
cvWaitKey(0);
return 0;
}