1 | initial version |
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("/home/eagle-soft/Desktop/signiture.jpg");
//Make a clone from original image
Mat src = image.clone();
//Convert image to gray scale;
cvtColor(image,image,COLOR_BGR2GRAY);
//Run a threshold on image
cv::threshold(image,image,100,255,THRESH_BINARY_INV);
//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 image from gray to gbr to draw operation
cvtColor(image,image,COLOR_GRAY2BGR);
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
rectangle(image,r,Scalar(0,0,255),2,LINE_8);
//Show the resluts
imshow("image",image);
imshow("signiture",signiture);
waitKey(0);
return 0;
}
And these are the results :
You can play with Structure value and threshold value if didn't get good results on your other images.
2 | No.2 Revision |
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("/home/eagle-soft/Desktop/signiture.jpg");
imread("signiture.jpg");
//Make a clone from original image
Mat src = image.clone();
//Convert image to gray scale;
cvtColor(image,image,COLOR_BGR2GRAY);
//Run a threshold on image
cv::threshold(image,image,100,255,THRESH_BINARY_INV);
//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 image from gray to gbr to draw operation
cvtColor(image,image,COLOR_GRAY2BGR);
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
rectangle(image,r,Scalar(0,0,255),2,LINE_8);
//Show the resluts
imshow("image",image);
imshow("signiture",signiture);
waitKey(0);
return 0;
}
And these are the results :
You can play with Structure value and threshold value if didn't get good results on your other images.
3 | No.3 Revision |
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;
cvtColor(image,image,COLOR_BGR2GRAY);
//Run a threshold on image
cv::threshold(image,image,100,255,THRESH_BINARY_INV);
//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 image from gray to gbr to draw operation
image to bgr for drawing operations.
cvtColor(image,image,COLOR_GRAY2BGR);
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
rectangle(image,r,Scalar(0,0,255),2,LINE_8);
//Show the resluts
imshow("image",image);
imshow("signiture",signiture);
waitKey(0);
return 0;
}
And these are the results :
You can play with Structure value and threshold value if didn't get good results on your other images.