From 5b34f21293e6262a82be8c1fac645beebffcf0a6 Mon Sep 17 00:00:00 2001 From: 88IO <8810aoba@gmail.com> Date: Fri, 14 Jun 2019 08:15:32 +0900 Subject: [PATCH] suggest --- Canny.py | 11 ---- approximate_circle.py | 50 +++++++++++++++ canny.py | 7 ++ ellispe_fitting.py | 64 +++++++++++++++++++ ...34\345\207\2721.py" => extract_contour1.py | 13 ++-- extract_contour2.py | 12 ++++ line_fitting.py | 63 ++++++++++++++++++ ...06\343\202\243\343\203\263\343\202\260.py" | 62 ------------------ ...06\343\202\243\343\203\263\343\202\260.py" | 61 ------------------ ...55\346\244\234\345\207\272\357\274\222.py" | 15 ----- "\350\277\221\344\274\274\345\206\206.py" | 49 -------------- 11 files changed, 203 insertions(+), 204 deletions(-) delete mode 100644 Canny.py create mode 100644 approximate_circle.py create mode 100644 canny.py create mode 100644 ellispe_fitting.py rename "\350\274\252\351\203\255\346\244\234\345\207\2721.py" => extract_contour1.py (74%) create mode 100644 extract_contour2.py create mode 100644 line_fitting.py delete mode 100644 "\346\245\225\345\206\206\343\203\225\343\202\243\343\203\203\343\203\206\343\202\243\343\203\263\343\202\260.py" delete mode 100644 "\347\233\264\347\267\232\343\203\225\343\202\243\343\203\203\343\203\206\343\202\243\343\203\263\343\202\260.py" delete mode 100644 "\350\274\252\351\203\255\346\244\234\345\207\272\357\274\222.py" delete mode 100644 "\350\277\221\344\274\274\345\206\206.py" diff --git a/Canny.py b/Canny.py deleted file mode 100644 index 94a5c28..0000000 --- a/Canny.py +++ /dev/null @@ -1,11 +0,0 @@ -import cv2 -import matplotlib.pyplot as plt -import numpy as np -from matplotlib.patches import Polygon - - -img = cv2.imread("C:\\Users\\tuzuk\\Desktop\\point.JPG") -gray = cv2.cvtColor(img, cv2.COLOR_RGBA2GRAY) -dst = cv2.Canny(gray,350,600) - -cv2.imwrite("C:\\Users\\tuzuk\\Desktop\\point.JPG", dst) diff --git a/approximate_circle.py b/approximate_circle.py new file mode 100644 index 0000000..13be42a --- /dev/null +++ b/approximate_circle.py @@ -0,0 +1,50 @@ +import cv2 +import numpy as np + +img = cv2.imread("images/CIMG4555.JPG") + +gau = cv2.GaussianBlur(img, (21, 21), 0) + +gray = cv2.cvtColor(gau, cv2.COLOR_BGR2GRAY) + +ret, th = cv2.threshold(gray, 230, 255, cv2.THRESH_BINARY) + +src = np.array(th, dtype="float32") + +cv2.imwrite("images/CIMG2478.JPG", src) + +if len(src.shape) == 3: + height, width, channels = src.shape[:3] +else: + height, width = src.shape[:2] + + channels = 1 + + print("dtype(src) " + str(src.dtype)) + + +image, contours, hierarchy = cv2.findContours( + th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) + +cnt = contours[0] + +print("dtype(cnt): " + str(cnt.dtype)) + +cnt_ = np.array(cnt, dtype="float32") + +print("dtype(cnt_): " + str(cnt_.dtype)) + +(x, y), radius = cv2.minEnclosingCircle(cnt) +center = (int(x), int(y)) +radius = int(radius) + + +img = cv2.circle(img, center, 200, (0, 0, 255), 2) + +cv2.imwrite("images/CIMG4555_1.JPG", img) + +print(center) +print(radius) + + +print("OK") diff --git a/canny.py b/canny.py new file mode 100644 index 0000000..b87f23a --- /dev/null +++ b/canny.py @@ -0,0 +1,7 @@ +import cv2 + +img = cv2.imread("images/point.JPG") +gray = cv2.cvtColor(img, cv2.COLOR_RGBA2GRAY) +dst = cv2.Canny(gray, 350, 600) + +cv2.imwrite("images/point.JPG", dst) diff --git a/ellispe_fitting.py b/ellispe_fitting.py new file mode 100644 index 0000000..a74942c --- /dev/null +++ b/ellispe_fitting.py @@ -0,0 +1,64 @@ +import cv2 +import numpy as np + +# 画像の読み込み +img = cv2.imread("images/volto_copy.png") + + +# 塗りつぶし +height, width, channels = img.shape[:3] + +print("width: " + str(width)) +print("height: " + str(height)) + +cv2.rectangle(img, (1, height), (width, 1), (0, 0, 0), + thickness=30, lineType=cv2.LINE_4) + +cv2.imwrite("images/CIMG4192.JPG", img) + + +# ガウスのぼかし処理 +gau = cv2.GaussianBlur(img, (5, 5), 0) + +# グレースケール化 +gray = cv2.cvtColor(gau, cv2.COLOR_BGR2GRAY) + +# 2値化 +ret, th = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU) +cv2.imwrite("images/volto_out2.png", th) + +# +src = np.array(th, dtype="float32") + + +if len(src.shape) == 3: + height, width, channels = src.shape[:3] +else: + height, width = src.shape[:2] + + channels = 1 + + print("dtype(src) " + str(src.dtype)) + +# 輪郭抽出 +image, contours, hierarchy = cv2.findContours( + th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) + +cnt = contours[0] + +print("dtype(cnt): " + str(cnt.dtype)) + +cnt_ = np.array(cnt, dtype="float32") + +print("dtype(cnt_): " + str(cnt_.dtype)) + +# 楕円フィッティング +ellipse = cv2.fitEllipse(cnt) +img = cv2.ellipse(img, ellipse, (0, 255, 0), 2) + +# 焦点 +print(ellipse) + +cv2.imwrite("images/volto_out1.png", img) + +print("OK") diff --git "a/\350\274\252\351\203\255\346\244\234\345\207\2721.py" b/extract_contour1.py similarity index 74% rename from "\350\274\252\351\203\255\346\244\234\345\207\2721.py" rename to extract_contour1.py index df0a887..bdf8beb 100644 --- "a/\350\274\252\351\203\255\346\244\234\345\207\2721.py" +++ b/extract_contour1.py @@ -3,15 +3,15 @@ import numpy as np from matplotlib.patches import Polygon -img = cv2.imread("C:\\Users\\tuzuk\\Desktop\\point.JPG") +img = cv2.imread("images/point.JPG") gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) -ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY) +ret, thresh1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) + +contours = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) -contours = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) - def draw_contours(ax, img, contours): ax.imshow(img) ax.axis('off') @@ -21,7 +21,8 @@ def draw_contours(ax, img, contours): ax.add_patch(Polygon(cnt, color='b', fill=None, lw=2)) # 輪郭の点を描画する。 ax.plot(cnt[:, 0], cnt[:, 1], 'ro', mew=0, ms=4) - + + fig, ax = plt.subplots(figsize=(6, 6)) draw_contours(ax, img, contours) -plt.show() \ No newline at end of file +plt.show() diff --git a/extract_contour2.py b/extract_contour2.py new file mode 100644 index 0000000..eb782bf --- /dev/null +++ b/extract_contour2.py @@ -0,0 +1,12 @@ +import cv2 + +img = cv2.imread("images/point.JPG") + +gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) + +contours = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) + +cnt = contours[0] +result = cv2.drawContours(img, [cnt], 0, (0, 255, 0), 3) + +cv2.imwrite("images/point_canny.JPG", result) diff --git a/line_fitting.py b/line_fitting.py new file mode 100644 index 0000000..b117742 --- /dev/null +++ b/line_fitting.py @@ -0,0 +1,63 @@ +import cv2 +import numpy as np + +img = cv2.imread("images/hand_gau_out1.JPG") + +# 塗りつぶし +height, width, channels = img.shape[:3] + +print("width: " + str(width)) +print("height: " + str(height)) + +cv2.rectangle(img, (1, height), (width, 1), (0, 0, 0), + thickness=100, lineType=cv2.LINE_4) + +cv2.imwrite("images/hand_gau_draw.JPG", img) + + +# ガウスのぼかし処理 +gau = cv2.GaussianBlur(img, (99, 99), 0) + +# グレースケール化 +gray = cv2.cvtColor(gau, cv2.COLOR_BGR2GRAY) + +# 2値化 +ret, th = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY) +cv2.imwrite("images/hand_gau_out2.JPG", th) + +src = np.array(th, dtype="float32") + +# cv2.imwrite("images/CIMG2478.JPG", src) + +if len(src.shape) == 3: + height, width, channels = src.shape[:3] +else: + height, width = src.shape[:2] + + channels = 1 + + print("dtype(src) " + str(src.dtype)) + + +image, contours, hierarchy = cv2.findContours( + th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) + +cnt = contours[0] + +print("dtype(cnt): " + str(cnt.dtype)) + +cnt_ = np.array(cnt, dtype="float32") + +print("dtype(cnt_): " + str(cnt_.dtype)) + +rows, cols = img.shape[:2] +[vx, vy, x, y] = cv2.fitLine(cnt, cv2.DIST_L2, 0, 0.01, 0.01) +lefty = int((-x*vy/vx) + y) +righty = int(((cols-x)*vy/vx)+y) +img = cv2.line(img, (cols-1, righty), (0, lefty), (0, 255, 0), 2) + + +print(lefty) +print(righty) + +cv2.imwrite("images/hand_gau_out1.JPG", img) diff --git "a/\346\245\225\345\206\206\343\203\225\343\202\243\343\203\203\343\203\206\343\202\243\343\203\263\343\202\260.py" "b/\346\245\225\345\206\206\343\203\225\343\202\243\343\203\203\343\203\206\343\202\243\343\203\263\343\202\260.py" deleted file mode 100644 index a8a5ca9..0000000 --- "a/\346\245\225\345\206\206\343\203\225\343\202\243\343\203\203\343\203\206\343\202\243\343\203\263\343\202\260.py" +++ /dev/null @@ -1,62 +0,0 @@ -import cv2 -import numpy as np - -#画像の読み込み -img = cv2.imread("C:\\Users\\tuzuk\\Desktop\\volto_copy.png") - - -#塗りつぶし -height, width, channels = img.shape[:3] - -print("width: " + str(width)) -print("height: " + str(height)) - -cv2.rectangle(img, (1, height), (width, 1), (0, 0, 0), thickness=30, lineType=cv2.LINE_4) - -cv2.imwrite("C:\\Users\\tuzuk\\Desktop\\CIMG4192.JPG", img) - - -#ガウスのぼかし処理 -gau=cv2.GaussianBlur(img,(5,5),0) - -#グレースケール化 -gray = cv2.cvtColor(gau, cv2.COLOR_BGR2GRAY) - -#2値化 -ret, th = cv2.threshold(gray,0,255,cv2.THRESH_OTSU) -cv2.imwrite("C:\\Users\\tuzuk\\Desktop\\volto_out2.png", th) - -# -src = np.array(th, dtype = "float32") - - -if len(src.shape) == 3: - height, width, channels = src.shape[:3] -else: - height, width = src.shape[:2] - - channels = 1 - - print("dtype(src) " + str(src.dtype)) - -#輪郭抽出 -image, contours, hierarchy = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) - -cnt = contours[0] - -print("dtype(cnt): " + str(cnt.dtype)) - -cnt_ = np.array(cnt, dtype = "float32") - -print("dtype(cnt_): " + str(cnt_.dtype)) - -#楕円フィッティング -ellipse = cv2.fitEllipse(cnt) -img = cv2.ellipse(img,ellipse,(0,255,0),2) - -#焦点 -print(ellipse) - -cv2.imwrite("C:\\Users\\tuzuk\\Desktop\\volto_out1.png",img) - -print("OK") \ No newline at end of file diff --git "a/\347\233\264\347\267\232\343\203\225\343\202\243\343\203\203\343\203\206\343\202\243\343\203\263\343\202\260.py" "b/\347\233\264\347\267\232\343\203\225\343\202\243\343\203\203\343\203\206\343\202\243\343\203\263\343\202\260.py" deleted file mode 100644 index 69982b6..0000000 --- "a/\347\233\264\347\267\232\343\203\225\343\202\243\343\203\203\343\203\206\343\202\243\343\203\263\343\202\260.py" +++ /dev/null @@ -1,61 +0,0 @@ -import cv2 -import numpy as np - -img = cv2.imread("C:\\Users\\tuzuk\\Desktop\\hand_gau_out1.JPG") - -#塗りつぶし -height, width, channels = img.shape[:3] - -print("width: " + str(width)) -print("height: " + str(height)) - -cv2.rectangle(img, (1, height), (width, 1), (0, 0, 0), thickness=100, lineType=cv2.LINE_4) - -cv2.imwrite("C:\\Users\\tuzuk\\Desktop\\hand_gau_draw.JPG", img) - - -#ガウスのぼかし処理 -gau=cv2.GaussianBlur(img,(99,99),0) - -#グレースケール化 -gray = cv2.cvtColor(gau, cv2.COLOR_BGR2GRAY) - -#2値化 -ret, th = cv2.threshold(gray,50,255,cv2.THRESH_BINARY) -cv2.imwrite("C:\\Users\\tuzuk\\Desktop\\hand_gau_out2.JPG", th) - -src = np.array(th, dtype = "float32") - -#cv2.imwrite("C:\\Users\\tuzuk\\Desktop\\Hands\\CIMG2478.JPG", src) - -if len(src.shape) == 3: - height, width, channels = src.shape[:3] -else: - height, width = src.shape[:2] - - channels = 1 - - print("dtype(src) " + str(src.dtype)) - - -image, contours, hierarchy = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) - -cnt = contours[0] - -print("dtype(cnt): " + str(cnt.dtype)) - -cnt_ = np.array(cnt, dtype = "float32") - -print("dtype(cnt_): " + str(cnt_.dtype)) - -rows,cols = img.shape[:2] -[vx,vy,x,y] = cv2.fitLine(cnt, cv2.DIST_L2,0,0.01,0.01) -lefty = int((-x*vy/vx) + y) -righty = int(((cols-x)*vy/vx)+y) -img = cv2.line(img,(cols-1,righty),(0,lefty),(0,255,0),2) - - -print(lefty) -print(righty) - -cv2.imwrite("C:\\Users\\tuzuk\\Desktop\\hand_gau_out1.JPG",img) \ No newline at end of file diff --git "a/\350\274\252\351\203\255\346\244\234\345\207\272\357\274\222.py" "b/\350\274\252\351\203\255\346\244\234\345\207\272\357\274\222.py" deleted file mode 100644 index c639ce1..0000000 --- "a/\350\274\252\351\203\255\346\244\234\345\207\272\357\274\222.py" +++ /dev/null @@ -1,15 +0,0 @@ -import cv2 -import matplotlib.pyplot as plt -import numpy as np -from matplotlib.patches import Polygon - -img = cv2.imread("C:\\Users\\tuzuk\\Desktop\\point.JPG") - -gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) - -contours = cv2.findContours(gray,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) - -cnt = contours[0] -result = cv2.drawContours(img, [cnt], 0, (0,255,0), 3) - -cv2.imwrite("C:\\Users\\tuzuk\\Desktop\\point.JPG",result) \ No newline at end of file diff --git "a/\350\277\221\344\274\274\345\206\206.py" "b/\350\277\221\344\274\274\345\206\206.py" deleted file mode 100644 index ce6fdfa..0000000 --- "a/\350\277\221\344\274\274\345\206\206.py" +++ /dev/null @@ -1,49 +0,0 @@ -import cv2 -import numpy as np - -img = cv2.imread("C:\\Users\\tuzuk\\Desktop\\CIMG4555.JPG") - -gau=cv2.GaussianBlur(img,(21,21),0) - -gray = cv2.cvtColor(gau, cv2.COLOR_BGR2GRAY) - -ret, th = cv2.threshold(gray,230,255,cv2.THRESH_BINARY) - -src = np.array(th, dtype = "float32") - -cv2.imwrite("C:\\Users\\tuzuk\\Desktop\\Hands\\CIMG2478.JPG", src) - -if len(src.shape) == 3: - height, width, channels = src.shape[:3] -else: - height, width = src.shape[:2] - - channels = 1 - - print("dtype(src) " + str(src.dtype)) - - -image, contours, hierarchy = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) - -cnt = contours[0] - -print("dtype(cnt): " + str(cnt.dtype)) - -cnt_ = np.array(cnt, dtype = "float32") - -print("dtype(cnt_): " + str(cnt_.dtype)) - -(x,y), radius = cv2.minEnclosingCircle(cnt) -center = (int(x),int(y)) -radius = int(radius) - - -img = cv2.circle(img,center,200,(0,0,255),2) - -cv2.imwrite("C:\\Users\\tuzuk\\Desktop\\CIMG4555_1.JPG", img) - -print(center) -print(radius) - - -print("OK") \ No newline at end of file