2017-12-26 27 views
-1

제로 파이썬 오류 및 OpenCV의에 의한 부문 : 나는 이유를 모르는얻기 나는 다음과 같은 이미지에서 라인을 제거하려면이 코드를 사용하고

src

하지만 출력 ZeroDivisionError: division by zero error 나를 준다 온라인 34 - x0, x1, y0, y1 = (0, im_wb.shape[1], sum(y0_list)/len(y0_list), sum(y1_list)/len(y1_list)).

왜 그럴까요? 어떻게 해결할 수 있습니까? 이 다른 하나

import cv2 
import numpy as np 

img = cv2.imread('lines.png',0) 

# Applies threshold and inverts the image colors 
(thresh, im_bw) = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) 
im_wb = (255-im_bw) 

# Line parameters 
minLineLength = 100 
maxLineGap = 10 
color = 255 
size = 1 

# Substracts the black line 
lines = cv2.HoughLinesP(im_wb,1,np.pi/180,minLineLength,maxLineGap)[0] 

# Makes a list of the y's located at position x0 and x1 
y0_list = [] 
y1_list = [] 
for x0,y0,x1,y1 in lines: 
    if x0 == 0: 
     y0_list.append(y0) 
    if x1 == im_wb.shape[1]: 
     y1_list.append(y1) 

# Calculates line thickness and its half 
thick = max(len(y0_list), len(y1_list)) 
hthick = int(thick/2) 

# Initial and ending point of the full line 
x0, x1, y0, y1 = (0, im_wb.shape[1], sum(y0_list)/len(y0_list), sum(y1_list)/len(y1_list)) 

# Iterates all x's and prints makes a vertical line with the desired thickness 
# when the point is surrounded by white pixels 
for x in range(x1): 
    y = int(x*(y1-y0)/x1) + y0 
    if im_wb[y+hthick+1, x] == 0 and im_wb[y-hthick-1, x] == 0: 
     cv2.line(img,(x,y-hthick),(x,y+hthick),colour,size) 

cv2.imshow('clean', img) 
cv2.waitKey(0) 

질문 reffers : Python: How to OCR characters crossed by a horizontal line

+0

이유가 무엇입니까? y_0 목록이나 y1_list가 비어있는 것은 어떨까요? len()은 0을 반환합니다. –

+0

예,하지만 비어있는 방법은 무엇입니까? 내 말은, 이미지가로드 중입니다 ... – Link

+1

'HoughLine'이 좋은 선택이라고 생각하지 않습니다. 나는 morph-op를 선호합니다. 이것은 제 결과입니다. https://i.stack.imgur.com/IBpyo.png. 도 살펴볼 수 있습니다. https://stackoverflow.com/questions/47667238/find-single-color-horizontal-spaces-in-image/47675213#47675213 – Silencer

답변

0

음 오류의 원인은 길이가 하나 y0_list 또는 y1_list (또는 둘 다)의 0 점이다. 당신이 루프이 그들을 초기화 이후 :

for x0,y0,x1,y1 in lines: 
    if x0 == 0: 
     y0_list.append(y0) 
    if x1 == im_wb.shape[1]: 
     y1_list.append(y1) 

은 당신이 중 하나 lines가 예상 값 또는 결함이있는 당신이 if 문을 가지고 있지 아래로 당신의 오류를 좁힐 수 있습니다. 나는 문제가 후자에 의해 발생한다고 생각하지만 가장 쉬운 수표는 lines을 출력하고 if 문장이 실행되는지 수동으로 확인하는 것입니다.

+0

일 것입니다. 여전히 이상합니다. 여전히 오류가 있습니다. 여전히 0 ~ y0 및 y1 목록에 있습니다. – Link

+0

제 대답이 약간 업데이트되었습니다. 'lines' 변수의 내용을 보여줄 수 있습니까? 'x = 0'에서 줄의 시작을 발견 할 것으로 예상되는 것 같습니다. –

+0

라인에 값이있는 것 같습니다 : [debug img] (https://imgur.com/a/YrG02) – Link