Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions mahotas/tests/LineDetectorByHough.py
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():
Copy link
Owner

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?

Copy link
Author

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!

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):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this just collections.Counter?

Copy link
Author

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 try to using collections.Counter, it do better!

def __missing__(self, key):
return 0

class LineDetectorByHough:
countersDICT = Counter(dict())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes the countsDICT be a class-level variable, you must assign to it inside the __init__ constructor

Copy link
Author

Choose a reason for hiding this comment

The 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):
Copy link
Owner

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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)"
Because I am a new python programmer, mostly a Java programmer for OOP, maybe I will do later.
Thank you very much!

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)
40 changes: 40 additions & 0 deletions mahotas/tests/test_hough_transform_line.py
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))