緑色の矩形内に赤い直線がある場合,白い線としたいと思っています
そこで矩形の頂点(a,b,c,d)の4点と直線の両端(X1,X2)の座標を求め、
条件文で矩形の座標間にX1,X2があった場合を行いたいのですが
頂点 a[170 113] b[167 57] c[317 50] d[319 105]
X1 (263.29733, 104.87856)
X2 (256.93668, 107.05521)
X1 (318.125, 104.85714)
X2 (264.37497, 104.85878)
X1 (256.87796, 107.75887)
X2 (224.3598, 107.49466)
X1 (241.82504, 109.3887)
X2 (318.10205, 106.30674)
X1 (225.59557, 110.03477)
X2 (239.43582, 109.34441)
・
・
・
Traceback (most recent call last):
File "○○○.py", line 101, in <module>
if a < X1 < b:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
というエラーがでてしまいます
これは条件文に対してX1がどの値かが明確ではないため起こる問題であっていますでしょうか
この場合条件文で比較するためのアドバイスをいただけたら幸いです
python
1import cv2 2import math 3import numpy as np 4 5img_src = cv2.imread("./haku/sample/image1.ppm") 6 7gauss = cv2.GaussianBlur(img_src,(11,11),0)#9,9 8gray = cv2.cvtColor(gauss,cv2.COLOR_BGR2GRAY) 9ret,th1 = cv2.threshold(gray,200,255,cv2.THRESH_BINARY) 10edges = cv2.Canny(th1,50,150)#(100,150) 11 12#矩形検出 13img, contours, hierarchy = cv2.findContours(th1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1) 14if len(contours) > 0: 15 for i in range(0, len(contours)): 16 cnt = contours[i] 17 M = cv2.moments(cnt) 18 #print(M) 19 area = cv2.contourArea(cnt) 20 print(area) 21 if 500 < area < 5000: 22 rect = cv2.minAreaRect(contours[i]) 23 box = cv2.boxPoints(rect) 24 box = np.int0(box) 25 x1,x2,x3,x4 = np.int0(box) 26 a = np.array(x1) 27 b = np.array(x2) 28 c = np.array(x3) 29 d = np.array(x4) 30 31 A = np.linalg.norm(a-b) 32 B = np.linalg.norm(a-c) 33 C = np.linalg.norm(a-d) 34 35 print("頂点",x1,x2,x3,x4) 36 #print("abcd", a,b,c,d) 37 #print("Aの長さ", A) 38 #print("Bの長さ",B) 39 #print("Cの長さ",C) 40 41 if A < B: 42 min = A 43 else: 44 min = B 45 if C < min: 46 min = C 47 print("最小",min) 48 if 5 < min < 25: 49 im = cv2.drawContours(img_src,[box],0,(0,255,0),2) 50 51#直線検出 52LSD = cv2.createLineSegmentDetector() 53lines, width, prec, nfa = LSD.detect(edges) 54if lines is not None: 55 for i in range(len(lines)): 56 57 for x1,y1,x2,y2 in lines[i] : 58 cv2.line(img_src,(x1,y1),(x2,y2),(0,0,255),2) 59 X1 = (x1, y1) 60 X2 = (x2, y2) 61 print("X1", X1) 62 print("X2", X2) 63 64jug1 = cv2.pointPolygonTest(rect, X1, False) 65jug2 = cv2.pointPolygonTest(rect, X2, False) 66print(jug1) 67print(jug2) 68. 69. 70.
コードが見づらくて申し訳ありません
python
1import cv2 2import math 3import numpy as np 4 5img_src = cv2.imread("./haku/sample/image1.ppm") 6 7gauss = cv2.GaussianBlur(img_src,(11,11),0)#9,9 8gray = cv2.cvtColor(gauss,cv2.COLOR_BGR2GRAY) 9ret,th1 = cv2.threshold(gray,200,255,cv2.THRESH_BINARY) 10edges = cv2.Canny(th1,50,150)#(100,150) 11 12 13img, contours, hierarchy = cv2.findContours(th1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1) 14if len(contours) > 0: 15 for i in range(0, len(contours)): 16 cnt = contours[i] 17 M = cv2.moments(cnt) 18 #print(M) 19 area = cv2.contourArea(cnt) 20 print(area) 21 if 500 < area < 5000: 22 rect = cv2.minAreaRect(contours[i]) 23 box = cv2.boxPoints(rect) 24 box = np.int0(box) 25 x1,x2,x3,x4 = np.int0(box) 26 a = np.array(x1) 27 b = np.array(x2) 28 c = np.array(x3) 29 d = np.array(x4) 30 31 A = np.linalg.norm(a-b) 32 B = np.linalg.norm(a-c) 33 C = np.linalg.norm(a-d) 34 35 print("頂点",x1,x2,x3,x4) 36 #print("abcd", a,b,c,d) 37 #print("Aの長さ", A) 38 #print("Bの長さ",B) 39 #print("Cの長さ",C) 40 41 if A < B: 42 min = A 43 else: 44 min = B 45 if C < min: 46 min = C 47 print("最小",min) 48 if 5 < min < 25: 49 im = cv2.drawContours(img_src,[box],0,(0,255,0),2) 50 51 52def within(line, rect): 53 """line が rect に含まれるかどうか 54 """ 55 p1, p2 = tuple(line[:2]), tuple(line[2:]) 56 # 線の始点と終点が長方形内に含まれるかどうか 57 return cv2.pointPolygonTest(box, p1, False) >= 0 and \ 58 cv2.pointPolygonTest(box, p2, False) >= 0 59 60LSD = cv2.createLineSegmentDetector() 61lines, width, prec, nfa = LSD.detect(edges) 62print(lines.shape) 63lines = np.squeeze(lines) 64 65if lines is not None: 66 for i in range(len(lines)): 67 68 #for x1,y1,x2,y2 in lines[i] : 69 # cv2.line(img_src,(x1,y1),(x2,y2),(0,0,255),2) 70 # X1 = (x1, y1) 71 # X2 = (x2, y2) 72 # print("X1", X1) 73 #print("X2", X2) 74 for line in lines: 75 p1, p2 = tuple(lines[:2]), tuple(line[2:]) 76 if within(line, rect): 77 # 長方形に含まれる場合は赤色で描画 78 cv2.line(img_src, p1, p2, (0, 0, 255), 2) 79 else: 80 # 長方形に含まれない場合は赤色で描画 81 cv2.line(img_src, p1, p2, (255, 0, 0), 2) 82 83 84#cv2.imwrite("teratail.jpg", img_src) 85cv2.imshow('src', img_src) 86cv2.waitKey() 87cv2.destroyAllWindows() 88
修正後
python
1import cv2 2import math 3import numpy as np 4 5img_src = cv2.imread("test.jpg") 6 7gauss = cv2.GaussianBlur(img_src, (11, 11), 0) 8gray = cv2.cvtColor(gauss, cv2.COLOR_BGR2GRAY) 9ret,th1 = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY) 10edges = cv2.Canny(th1, 50, 150) 11 12img, contours, hierarchy = cv2.findContours(th1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1) 13for i in range(0, len(contours)): 14#for cnt in contours: 15 cnt = contours[i] 16 area = cv2.contourArea(cnt) 17 #print(area) 18 19 if 500 < area < 5000: 20 rect = cv2.minAreaRect(cnt) 21 box = cv2.boxPoints(rect) 22 23 (cx, cy), (width, height), angle = rect 24 diag = np.linalg.norm([width, height]) 25 #print(width, height, diag) 26 # 161.91236877441406 16.144147872924805 162.715237986 27 28 # 29 A = width 30 B = height 31 C = diag 32 33 if A < B: 34 min = A 35 else: 36 min = B 37 if C < min: 38 min = C 39 print("min", min) 40 if 5 < min < 25: 41 # 42 cv2.drawContours(img_src, [box.astype(int)], -1, (0, 255, 0), 2) 43 44def within(line, rect): 45 46 p1, p2 = tuple(line[:2]), tuple(line[2:]) 47 #print("p1",p1) 48 #print("p2",p2) 49 #print("line",line) 50 return cv2.pointPolygonTest(box, p1, False) >= 0 and \ 51 cv2.pointPolygonTest(box, p2, False) >= 0 52 53LSD = cv2.createLineSegmentDetector() 54lines, width, prec, nfa = LSD.detect(edges) 55if lines is not None: 56 lines = np.squeeze(lines,axis=1) 57 #if lines.dims == 1: 58 # lines = np.expand_dims(lines, axis=0) 59 for line in lines: 60 p1, p2 = tuple(line[:2]), tuple(line[2:]) 61 print("line",line) 62 63 if within(line, rect): 64 65 cv2.line(img_src, p1, p2, (0, 0, 255), 2) 66 #else: 67 68 # cv2.line(img_src, p1, p2, (255, 0, 0), 2) 69 70 71 72cv2.imshow('src', img_src) 73cv2.waitKey() 74cv2.destroyAllWindows() 75
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/12 11:37
2018/09/12 11:57
2018/09/12 12:20
2018/09/12 12:20
2018/09/12 13:29
2018/09/13 02:15
2018/09/13 03:10
2018/09/13 04:33
2018/09/13 04:42
2018/09/13 05:22
2018/09/13 07:34
2018/09/13 08:04
2018/09/13 11:06
2018/09/13 11:14
2018/09/13 11:25