Home > other >  C + + seal extraction function using the python language transformation, give advice or comments ple
C + + seal extraction function using the python language transformation, give advice or comments ple

Time:10-13

/* *
* official seal in the contract image extraction
* 1) input and output is PNG high-fidelity image
* 2) compile: g + + - g - o sealcut sealcut. CPP ` PKG - config -- cflags - libs opencv `
* 3) usage: sealcut filename
*
* @ yangxf
* @ the date 2018-03
*/

#include
#include
 
#include
#include

#include
#include
#include
#include
#include

using namespace std;
using namespace cv;

Const char * windowname="sealcut Demo";
Const char * sealimgpath="./sample ";
Const char * benchmarkpath="./benchmark ";

//red threshold
Int hsvredlovalue=https://bbs.csdn.net/topics/140;
Int hsvredhivalue=https://bbs.csdn.net/topics/250;

//seal radius
Int sealredius=154;

//image preprocessing
Mat initimage imgname (char *);

//the outline of the pentagram seal image positioning: the algorithm for the star is not obvious, the seal of the effect not beautiful
Vector Locsealbycontours (Mat hqimg);

//the hoff circle fitting suborbicular seal image positioning: high universality, the algorithm can locate the image of all round
Vector Locsealbyhough (Mat hqimg);

//seal image cutting
Void cutseal (Mat srcimg vector Rects, char * imgname);

//hoff circular transformation
Void houghseal (Mat srcimg, int index, char * imgname);

//threshold value method is used to eliminate the red official seal, free text and signed contract page
Mat maskimage imgname (char *);


Int main (int arg c, char * * argv) {
If (arg c!=2) {
cout <"The usage: sealocr filename, both please input image file name!" return 0;
}

Srcimgname char imgpath [200], [200].
Memset (imgpath, 0, sizeof (imgpath));
Memset (srcimgname, 0, sizeof (srcimgname));

Sprintf (srcimgname, "% s", argv [1]).
/sprintf (imgpath, "% s % s", sealimgpath, srcimgname);

cout
//image enhancement
Mat imageGamma=initimage (imgpath);

//the hoff circle fitting suborbicular positioning seal image
Vector Rects=locsealbyhough (imageGamma);
//the outline of the pentagram seal image positioning, poor performance of the algorithm, temporarily not
//vector Rects=locseal (imageGamma);

//according to seal image to extract grabcut implementation
Cutseal (imageGamma rects, srcimgname);

//if you need to debug, please open the following statement
//waitKey ();
return 1;
}

/* *
* image preprocessing
* 1) load the image file
* 2) image enhancement
*
*/
Mat initimage (char * imgname) {
Mat srcimg=imread (imgname);
NamedWindow (" srcimg CV_WINDOW_NORMAL);
Imshow (" srcimg srcimg);

//Gamma image enhancement: to solve the problem of the unclear seal
Mat imageGamma (srcimg. The size (), CV_32FC3);
for (int i=0; I & lt; Srcimg. Rows; I++) {
For (int j=0; J & lt; Srcimg. Cols; J++) {
ImageGamma. AtImageGamma. AtImageGamma. At}
}
//the normalized to 0 ~ 255
The normalize (imageGamma imageGamma, 0, 255, CV_MINMAX);

//into 8 bit image
ConvertScaleAbs (imageGamma imageGamma);
//namedWindow (" imageGamma, "CV_WINDOW_NORMAL);
//imshow (" imageGamma, "imageGamma);

Return imageGamma;
}

//the hoff circle fitting suborbicular positioning seal image
Vector Locsealbyhough (Mat hqimg) {
//RGB color converted to HSV
Mat hsvimage;
CvtColor (hqimg hsvimage, COLOR_BGR2HSV);

//threshold operation: find the color of the specified range
Mat dstimage;
InRange (hsvimage, Scalar (hsvredlovalue, 40, 40), Scalar (hsvredhivalue, 255, 255), dstimage);

//find outline
Vector Contours.
FindContours (dstimage, contours, CV_RETR_CCOMP CV_CHAIN_APPROX_SIMPLE);
NamedWindow (" findContours CV_WINDOW_NORMAL);
Imshow (" findContours dstimage);

//the contour rendering for red
Vector Rects;
Mat drawimage=Mat: : zeros (dstimage. The size (), CV_8UC3);
For (int index=0; The index & lt; Contours. The size (); Index + +) {
Vector Dstcontour=contours [index];

DrawContours (drawimage, contours, index, Scalar (0, 0, 255), 3, 8).
}
NamedWindow (" drawimage, "CV_WINDOW_NORMAL);
Imshow (" drawimage drawimage);

//into gray image, the image smooth
Mat graymage;
CvtColor (drawimage graymage, CV_BGR2GRAY);
GaussianBlur (graymage graymage, Size (9, 9), 2, 2);

//hoff circular transformation: remove the seal round external noise block
Mat houghimage=Mat: : zeros (drawimage. The size (), CV_8UC3);

VectorHoughCircles (graymage, circles, CV_HOUGH_GRADIENT, 2 *//* hoff space resolution, 300/*/minimum distance between two circle * and */100/* Canny threshold, 25, 100/* */minimum radius, 300/* */maximum radius);

Point center;
Int the radius=0;

cout For (size_t I=0; I & lt; Circles. The size (); I++) {
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related