10-7FindLines_2.py

000: import cv2
001: import numpy as np
002:
003: def __main():
004: img = cv2.imread(‘../20200905_163733.jpg’)
005: img = getResize(img)
006:
007: gray = cv2.cvtColor(src=img, code=cv2.COLOR_BGR2GRAY)
008: ret, bw = cv2.threshold(src=gray, thresh=0, maxval=255, type=cv2.THRESH_OTSU)
009: bw = cv2.Canny(bw, 10, 200, apertureSize=3)
010:
011: # 入力画像,距離分解能,角度分解能,閾値
012: lines = cv2.HoughLinesP(image=bw, rho=1, theta=np.pi/180, threshold=100, maxLineGap=10, minLineLength=100)
013:
014: if lines is not None:
015: for x1, y1, x2, y2 in lines[:, 0]:
016:
017: cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2, cv2.LINE_AA)
018:
019: cv2.imshow(‘Edges’, bw)
020: cv2.imshow(‘Final result’, img)
021: cv2.waitKey(0)
022: cv2.destroyAllWindows()
023: def getResize(src):
024: basePixSize = 1280 # 縦横で大きい辺の変更したいサイズ
025: height = src.shape[0]
026: width = src.shape[1]
027:
028: largeSize = max(height, width) # 大きい方の辺のサイズ
029: resizeRate = basePixSize / largeSize # 変更比率を計算
030: shotSize = min(height, width) * resizeRate
031: dst = cv2.resize(src, (int(width * resizeRate), int(height * resizeRate)))
032:
033: return dst
034:
035: if __name__ == ‘__main__’:
036: print(cv2.__version__)
037:
038: __main()