-
Notifications
You must be signed in to change notification settings - Fork 148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
simple detect line using hough transform #99
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (C) 2008-2014, Jasson <[email protected]> | ||
# License: MIT (see COPYING file) | ||
|
||
import math | ||
import numpy as np | ||
|
||
class Line(): | ||
def __init__(self,r,angle): | ||
self.r = r | ||
self.angle = angle | ||
def __eq__(self, other): | ||
if self.r == other.r and self.angle == other.angle: | ||
return True | ||
else: | ||
return False | ||
def __hash__(self): | ||
result = 31 + int(self.r) | ||
result = 31 * result + int(self.angle * 100) | ||
#print result | ||
return result | ||
|
||
class Counter(dict): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I read the API, and try to using collections.Counter, it do better! |
||
def __missing__(self, key): | ||
return 0 | ||
|
||
class LineDetectorByHough: | ||
countersDICT = Counter(dict()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I try to do it, but it does not work well - unit test failed |
||
def __init__(self): | ||
pass | ||
|
||
def find(self,img): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The rest of mahotas uses a simpler functional interface. It seems to me that this one function could be spun out so that you call "find_hough_lines(im)" and get back the counter? Am I wrong? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, I am enjoy the style of "simpler functional interface", so I add a functional interface, find_hough_lines(im)" |
||
rows, cols = img.shape | ||
|
||
points = 0 | ||
for x in range(0, rows): | ||
for y in range(0, cols): | ||
if (img[x, y] == 1): | ||
points = points + 1 | ||
for angle in np.arange(0, 2 * np.pi, 2 * np.pi / 40): | ||
# print 'angel=',x, y, angel | ||
angle = round(angle, 2) | ||
angle = angle % round(np.pi, 2) | ||
r = round(self.calcR(x, y, angle),1) | ||
#print 'r,angel=', r, angle | ||
self.countersDICT[Line(r, angle)] += 1 | ||
#print "points=", points | ||
#print "line (r ,angle) count" | ||
|
||
for (k, v) in self.countersDICT.items(): | ||
if(v > rows/2): | ||
return k.r, k.angle | ||
def calcR(self,x,y,angle): | ||
return x* math.cos(angle) + y * math.sin(angle) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (C) 2008-2014, Jasson <[email protected]> | ||
# License: MIT (see COPYING file) | ||
import unittest | ||
import numpy as np | ||
import math | ||
|
||
from LineDetectorByHough import LineDetectorByHough | ||
from LineDetectorByHough import Line | ||
|
||
|
||
class test_hough_transform_line(unittest.TestCase): | ||
|
||
def test_simpleImg_hough_detect(self): | ||
img = np.zeros((80, 80), bool) | ||
img[6,:] = 1 | ||
|
||
c = LineDetectorByHough() | ||
lines = c.find(img) | ||
|
||
self.assertEquals(lines,Line(6,0)) | ||
|
||
def test_counter(self): | ||
c = LineDetectorByHough | ||
|
||
line = Line(10,np.pi/4) | ||
c.countersDICT[line] += 1 | ||
|
||
line = Line(10,np.pi/4) | ||
c.countersDICT[line] += 2 | ||
|
||
self.assertEquals(c.countersDICT[line], 3) | ||
|
||
def test_line_r_calc(self): | ||
c = LineDetectorByHough() | ||
self.assertEquals(c.calcR(10,10,np.pi/4), math.sqrt(10*10 + 10*10)) | ||
|
||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be replaced with a
namedtuple
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I read the API, and replaced with a namedtuple, it do better!