I want to crop the image between two lines, as shown in the image below. But the bottom line is not recognized well with HoughLinesP
.
The bottom line points are not really edged because of eroding, but is it important?
How can I detect the bottom line, and then crop the image according to these 2 lines?
Original image:
Processed image:
Canny edged:
Lines detected:
Code For Line Detection :
Mat dst, cdst,src2;
cv::blur( src, src2, cv::Size(5,5) );
Canny(src2, dst, 150, 300, 5);
cvtColor(dst, cdst, CV_GRAY2BGR);
//Mat original = imread("final_sample1.png",0);
vector<Vec4i> lines;
HoughLinesP(dst, lines, 1, 2*CV_PI/180, 100,1000, 50 );
For for displaying lines :
for( size_t i = 0; i < lines.size(); i++ )
{
Vec4i l = lines[i];
// oran = float(l[1] / col_size );
double angle = atan2(l[3] - l[1], l[2] - l[0]) * 180.0 / CV_PI;
if(angle < 5 && angle >=-5 ){
//if(1){
cout << l[0] << "," << l[1] << "," << l[2] << "," << l[3] << endl;
line( cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3, CV_AA);
}
}
EDIT:
For line detection appliying adaptive tresholding to original image gives more reliable results.
adaptiveThreshold(img,adaptiveTresholded,255,ADAPTIVE_THRESH_GAUSSIAN_C,CV_THRESH_BINARY,75,15);
I tested on 20 samples which has different number of rows , and thanks to Micka, with his modification, I got good results. To detect the right lines I put an if statement.
"ratio" variable is the y1 / row size of the image. And checking the line angle to prevent irrelevant lines.
for( size_t i = 0; i < lines.size(); i++ )
{
Vec4i l = lines[i];
raito = float(l[1] / row_size );
double angle = atan2(l[3] - l[1], l[2] - l[0]) * 180.0 / CV_PI;
if(angle < 10 && angle >=- 10 && ratio > 0.15 && ratio< 0.8){
//if(1){
cout <<"Here: " << l[0] << "," << l[1] << "," << l[2] << "," << l[3] <<
", Oran: " << oran << endl;
line( cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3);
}
}