使用cv2.HoughLines()的效果是错误的

时间:2018-01-29 04:51:42

标签: python python-3.x opencv

我将此link称为实验。

这是原始图片:

Original

我的测试代码:

import cv2
import numpy as np

img = cv2.imread('E:/image/sudoku.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)

lines = cv2.HoughLines(edges,1,np.pi/180,200)
for rho,theta in lines[0]:
    a = np.cos(theta)
    b = np.sin(theta)
    x0 = a*rho
    y0 = b*rho
    x1 = int(x0 + 1000*(-b))
    y1 = int(y0 + 1000*(a))
    x2 = int(x0 - 1000*(-b))
    y2 = int(y0 - 1000*(a))

    cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)

cv2.imwrite('E:/image/myhoughlines.jpg',img)
cv2.imshow('1',img)
cv2.waitKey(0)

我的代码运行结果:

my

但我想要这个效果:

want

哪里错了?

1 个答案:

答案 0 :(得分:1)

我知道哪里出错了! 官方网站的代码不是一个循环。 代码更改为:

class IntSet:
  m: int
  arr: array[int] 
  map: array[int]

  init(k):
    arr: int[k] # allocate, but don't initialize
    map: int[k+1] # allocate, but don't initialize
    m = 0

  size(): m

  contains(x: int): map[x] < m and arr[map[x]] == x

  insert(x: int):
    if not contains(x):
      arr[m] = x
      map[x] = m
      m += 1

  remove(x: int):
    if contains(x):
      m -= 1
      arr[map[x]] = arr[m] # move x off the end
      map[arr[m]] = map[x]

  clear(): m = 0

  union(s: IntSet):
    for i in 0..s.m:
      if not contains(s.arr[i]):
        insert(s.arr[i])

  items():
    for i in 0..m:
      yield arr[i]