# Signature detection over a black line

**URL:** <https://forum.opencv.org/t/signature-detection-over-a-black-line/6014>\
**Category:** Android/Java\
**Created:** [November 2, 2021, 3:31pm UTC](https://forum.opencv.org/t/signature-detection-over-a-black-line/6014 "2021-11-02T15:31:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fabrizio\_Ciutti](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/fabrizio_ciutti/32/3665_2.png) [@Fabrizio\_Ciutti](https://forum.opencv.org/u/Fabrizio_Ciutti)\
**Post date:** [November 2, 2021, 3:31pm UTC](https://forum.opencv.org/t/signature-detection-over-a-black-line/6014/1 "2021-11-02T15:31:52Z")

</div>

Hi,  
I’m fairly new to OpenCV and need some advice on the best pattern to follow to detect if a straight black line has some signature over.  
I’ve tried using template matching but the amount of false positives/negatives It’s high.  
Actually I’m matching the empty sign-zone with the images I have and if I find a match over a threshold I mark the document as unsigned.  
Here is the relevant part:

```auto
String sourcefile="document.png";
String template ="unsigned.png";
Mat template = Imgcodecs.imread(templatefile);
Mat source = Imgcodecs.imread(sourcefile);
int machMethod=Imgproc.TM_CCOEFF_NORMED;
Imgproc.matchTemplate(source, template, outputImage, machMethod);
        
//Localizing the best match with minMaxLoc
MinMaxLocResult mmr = Core.minMaxLoc(outputImage);
Point matchLoc=mmr.maxLoc;
//Draw rectangle on result image
Imgproc.rectangle(source, matchLoc, new Point(matchLoc.x + template.cols(),matchLoc.y + template.rows()), new Scalar(0, 255, 0),4);

boolean signed=true;
if(mmr.maxVal>=0.75) {
   	signed=false;
}

```

I’ve tried improving the algorithm using the Core.compare() functionality between the template and the rectangle defined by the best match, and then using Core.countNonZero() to have another threshold but I don’t get significative estimates of the equality:

```auto
Rect rect = new Rect((int)matchLoc.x,(int)matchLoc.y,template.cols(),template.rows());
Mat rectangle= source.submat(rect);
Mat mat = new Mat();
Mat templatetemp = template.clone();
Imgproc.cvtColor(templatetemp, templatetemp, Imgproc.COLOR_BGR2GRAY);
Imgproc.cvtColor(rectangle, rectangle, Imgproc.COLOR_BGR2GRAY);
Core.compare(rectangle, templatetemp, mat, Core.CMP_NE);
int equalsScore = Core.countNonZero(mat);
System.out.println(equalsScore);

```

Thanks in advance for any help/suggestion  
Edit:code formatting
