Ask Your Question

Signature Segmentation

asked 2020-09-16 01:16:57 -0500

I am fairly new to OpenCV and try to segment out signature from various as part ml pipeline. Can any of you guys please suggest me a way to segment out the same

These are the various inputs

image description

image description

image description

And these are the required outputs

image description image description image description

Thanks in advance for your time – if I’ve missed out anything, over- or under-emphasized a specific point let me know in the comments.

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2020-09-16 07:52:08 -0500

Naser gravatar image

updated 2020-09-16 07:56:24 -0500

This is what i have done on your first image , i haven't test it on others.

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;

bool compareContourAreas ( std::vector<cv::Point> contour1, std::vector<cv::Point> contour2 ) {
double i = fabs( contourArea(cv::Mat(contour1)) );
double j = fabs( contourArea(cv::Mat(contour2)) );
return ( i > j );

int main(int argc, char *argv[])
//Read image
Mat image = imread("signiture.jpg");
//Make a clone from original image
Mat src = image.clone();
//Convert image to gray scale;
//Run a threshold on image

//Run some morphological operations on thresholded image
int kernel_size = image.rows / 100;
Mat Structure = getStructuringElement(MORPH_RECT, Size(kernel_size, kernel_size));
dilate(image, image, Structure, Point(-1, -1));
erode(image, image, Structure, Point(-1, -1));

//Find all contours of image
std::vector<std::vector<cv::Point> > contours;
cv::Mat contourOutput = image.clone();
cv::findContours( contourOutput, contours, RETR_LIST,CHAIN_APPROX_NONE );

//Sort contours vector by the size of areas from biggest to smallest area
std::sort(contours.begin(), contours.end(), compareContourAreas);
cv::Scalar colors[3];

//Convert gray image to bgr for drawing operations.
colors[0] = cv::Scalar(255, 0, 0);
colors[1] = cv::Scalar(0, 255, 0);
colors[2] = cv::Scalar(0, 0, 255);

//0 is to index of the biggest contour so we can get rect of it
Rect r = boundingRect(contours[0]);

//Crop the image of the biggest contour

Mat signiture = src(r).clone();

// Draw a rectangle

//Show the resluts
return 0;


And these are the results : image description

image description

You can play with Structure value and threshold value if didn't get good results on your other images.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2020-09-16 01:16:57 -0500

Seen: 354 times

Last updated: Sep 16 '20