Compare commits

...

6 commits

5 changed files with 93 additions and 13 deletions

View file

@ -22,8 +22,8 @@ unsigned* max2_distance(std::vector<cv::Point> hull)
for(auto p: hull) {
std::cout << p << " ";
}
#endif
std::cout << std::endl;
#endif
unsigned *idx = (unsigned*) calloc(2, sizeof(int));
double distance[2] = {0};
for( unsigned i=0; hull.size()>i; i++ )
@ -126,11 +126,11 @@ find_longest_line(std::vector<cv::Point> hull, unsigned begin, unsigned end) //t
thisline.push_back(hull[(begin+1)%hull.size()]);
for(unsigned i=(begin+2)%hull.size(); i!=end; i++)
{
assert(2<=thisline.size());
assert(2<=thisline.size());
if(i==hull.size()) {
i=0;
}
assert(i < hull.size());
}
assert(i < hull.size());
if(similar_fit(thisline, hull[i])) {
thisline.push_back(hull[i]);
} else { // considering if the just-finished line is the best
@ -145,9 +145,10 @@ find_longest_line(std::vector<cv::Point> hull, unsigned begin, unsigned end) //t
thisline.push_back(hull[i]);
};
}
if(bestline.size()==0) { // this is the case only when the first line is the best line
assert(0==bestdistance);
double thisdistance = dist(thisline[0],thisline[thisline.size()-1]);
if(thisdistance>bestdistance) { // this is relevant if the best line ends at the last point
bestline = thisline;
bestdistance = thisdistance;
}
assert(bestline.size()>=2);
return bestline;

53
edge_finder.py Executable file
View file

@ -0,0 +1,53 @@
#!/usr/bin/env python2
import numpy as np
import sys
import cv2
cv_version = int(cv2.__version__.split('.')[0])
GRAY = cv2.CV_LOAD_IMAGE_GRAYSCALE if cv_version == 2 else cv2.IMREAD_GRAYSCALE
COLOR = cv2.CV_LOAD_IMAGE_COLOR if cv_version == 2 else cv2.IMREAD_COLOR
def show(img):
#cv2.namedWindow("immagine",cv2.WINDOW_AUTOSIZE)
cv2.imshow("immagine", img)
cv2.waitKey(0)
def contourIndex(lista):
# ritorna l'indice del contorno con perimetro maggiore
val=-1
index=0
for num, cont in enumerate(lista):
value=cv2.arcLength(cont,True)
if val<value:
val=value
index=num
return index
img=cv2.imread(sys.argv[1], GRAY)
#img=cv2.imread(sys.argv[1],cv2.CV_LOAD_IMAGE_COLOR)
#la riga successiva serve a rendere visualizzabile l immagine sul nostro pc
#img=cv2.resize(img,(350,262))
#assumendo che il background e nero nelle condizioni attuali (27/12/2016) del bookscanner il valore buono e 100 (su 255)
buono=100
mask=cv2.inRange(img,buono,255)
if cv_version == 2:
contorni, hier = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
else:
_, contorni, hier = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
bianco=np.zeros(mask.shape,dtype=np.uint8) # crea un coso nero
# indexImportante indica il contorno piu grande, che crediamo sia quello del libro
indexImportante=contourIndex(contorni)
cv2.drawContours(img,contorni,indexImportante,255,thickness=3)
cv2.fillPoly(bianco,[contorni[indexImportante]], 255)
img=cv2.imread(sys.argv[1], COLOR)
img = cv2.bitwise_and(img, img, mask=bianco)
if len(sys.argv) > 2:
cv2.imwrite(sys.argv[2], img)
else:
show(img)
# vim: set ts=4 sw=4 et:

View file

@ -105,13 +105,17 @@ get_line(Point p, Point q)
bool
is_in_line(cv::Vec<double,3> line, Point p)
{
return line(0)*p.x + line(1)*p.y + line(2) == 0;
return line_value(line, p) == 0;
}
bool
is_above_line(cv::Vec<double,3> line, Point p)
{
return line(0)*p.x + line(1)*p.y + line(2) > 0;
return line_value(line, p) > 0;
}
double line_value(cv::Vec<double,3> line, Point p)
{
return line(0)*p.x + line(1)*p.y + line(2);
}
cv::Point

View file

@ -11,6 +11,7 @@ cv::Point unmap_point(double, cv::Point, cv::Point);
cv::Vec<double,3> get_line(cv::Point, cv::Point);
bool is_in_line(cv::Vec<double,3> line, cv::Point p);
bool is_above_line(cv::Vec<double,3> line, cv::Point p);
double line_value(cv::Vec<double,3>, cv::Point);
cv::Point further_point_from_line(cv::Vec<double,3>, std::vector<cv::Point>);
class CoordinateSystem

View file

@ -119,6 +119,8 @@ class BookShape {
double ysize();
cv::Mat* getTrasfs();
};
//topleft topmiddle topright
//botleft botmiddle botright
BookShape::BookShape(cv::Point tl, cv::Point tm, cv::Point tr, cv::Point bl, cv::Point bm, cv::Point br)
{
trapezoids[0][0] = tl;
@ -195,14 +197,32 @@ BookShape get_book_shape(cv::Mat img)
corn_3 = hull[maxdistances[1]];
corn_4 = hull[(maxdistances[1]+1)%hull.size()];
std::vector<cv::Point> verticals[2];
std::vector<cv::Point> vertical_points[2];
cv::Vec<double,3> verticals[2];
// Between the two corners on the same side, the longest line is the vertical border of the book
verticals[0] = find_longest_line(hull, (maxdistances[0]+1)%hull.size(), maxdistances[1]);
vertical_points[0] = find_longest_line(hull, (maxdistances[0]+1)%hull.size(), maxdistances[1]);
std::cout << maxdistances[1] << std::endl;
std::cout << maxdistances[1]+1 << std::endl;
std::cout << (maxdistances[1]+1)%hull.size() << std::endl;
verticals[1] = find_longest_line(hull, (maxdistances[1]+1)%hull.size(), maxdistances[0]);
vertical_points[1] = find_longest_line(hull, (maxdistances[1]+1)%hull.size(), maxdistances[0]);
free(maxdistances);
#ifdef _DEBUG
img = cv::imread("files/masckera.png", CV_LOAD_IMAGE_COLOR);
cv::circle(img, vertical_points[0][0], img.cols>>7, cv::Scalar(200, 0, 0), -1);
cv::circle(img, vertical_points[0][vertical_points[0].size()-1], img.cols>>7, cv::Scalar(200, 0, 0), -1);
cv::circle(img, vertical_points[1][0], img.cols>>7, cv::Scalar(200, 200, 0), -1);
cv::circle(img, vertical_points[1][vertical_points[1].size()-1], img.cols>>7, cv::Scalar(200, 0, 0), -1);
cv::circle(img, corn_1, img.cols>>7, cv::Scalar(200, 0, 200), -1);
cv::namedWindow("aaa", CV_GUI_NORMAL);
cv::imshow("aaa", img);
cv::waitKey(0);
#endif
verticals[0] = get_line(vertical_points[0][0], vertical_points[0][vertical_points[0].size()-1]);
std::cout << verticals[0] << std::endl;
std::cout << line_value(verticals[0], corn_1) << std::endl;
verticals[0][2] -= line_value(verticals[0], corn_1);
std::cout << line_value(verticals[0], corn_1) << "=0?" << std::endl;
verticals[1] = get_line(vertical_points[1][0], vertical_points[1][vertical_points[1].size()-1]);
// theta is the angle of the line connecting point 1 and 2; it will be the
// rotation of our new coordinate system
@ -236,8 +256,9 @@ BookShape get_book_shape(cv::Mat img)
cv::Point middle2 = further_point_from_line(get_line(corn_3, corn_4), points1);
BookShape bs = BookShape(
corn_1, middle1, corn_2,
corn_4, middle2, corn_3);
vertical_points[1][vertical_points[1].size()-1], middle1, vertical_points[0][0],
vertical_points[1][0], middle2, vertical_points[0][vertical_points[0].size()-1]
);
return bs;
}