Jelajahi Sumber

上傳檔案到 ''

fatwolf 3 tahun lalu
induk
melakukan
5b7071069e
4 mengubah file dengan 170 tambahan dan 0 penghapusan
  1. 24 0
      opencv_floodfill.py
  2. 18 0
      opencv_resize.py
  3. 112 0
      opencv_size.py
  4. 16 0
      opencv_threshold.py

+ 24 - 0
opencv_floodfill.py

@@ -0,0 +1,24 @@
+import cv2
+import numpy as np
+def fill_color_demo(image):
+    copyIma = image.copy()
+    h, w = image.shape[:2]
+    print(h, w)
+    mask = np.zeros([h+2, w+2], np.uint8)
+    #cv2.floodFill(image, mask, seedPoint, newVal, loDiff, upDiff, flags)
+    cv2.floodFill(copyIma, mask, (30, 30), (0, 0, 0), (100, 100, 100), (50, 50, 50), cv2.FLOODFILL_FIXED_RANGE)
+    cv2.imshow("fill_color", copyIma)
+    copyIma = cv2.cvtColor(copyIma, cv2.COLOR_BGR2GRAY)
+    ret, mask = cv2.threshold(copyIma, 1, 255, cv2.THRESH_BINARY)
+    cv2.imshow("threshold", mask)
+    mask_inv = cv2.bitwise_not(mask)
+    cv2.imshow("bitwise_not", mask_inv)
+
+
+src = cv2.imread("test2.jpg")
+cv2.namedWindow("input image", cv2.WINDOW_AUTOSIZE)
+cv2.imshow("input image", src)
+fill_color_demo(src)
+
+cv2.waitKey(0)
+cv2.destroyAllWindows()

+ 18 - 0
opencv_resize.py

@@ -0,0 +1,18 @@
+import os
+import cv2
+import numpy as np
+
+def main():
+    data_dir_path ="C:\\Users\\User\\Desktop\\test"
+    file_list = os.listdir(r'C:\Users\User\Desktop\test')
+    count = 0
+    for file_name in file_list:
+        root, ext = os.path.splitext(file_name)
+        if ext == '.png' or '.jpeg' or '.jpg':
+            abs_name = data_dir_path + '/' + file_name
+            image = cv2.imread(abs_name)
+            #在下面寫要做的處理 在這邊是做修改圖片大小的處理
+            img = cv2.resize(image, (10, 10)) #把圖片的大小改成150*150
+            cv2.imwrite(abs_name, img)
+if __name__ == '__main__':
+    main()

+ 112 - 0
opencv_size.py

@@ -0,0 +1,112 @@
+# coding=utf-8
+# 导入一些后续需要使用到的python包
+# 可能需要 pip install  imutils
+from scipy.spatial import distance as dist
+from imutils import perspective
+from imutils import contours
+import numpy as np
+import argparse
+import imutils
+import cv2
+
+
+# 定义一个中点函数,后面会用到
+def midpoint(ptA, ptB):
+    return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)
+
+
+# 设置一些需要改变的参数
+ap = argparse.ArgumentParser()
+ap.add_argument("-i", "--image", required=True,
+                help="path to the input image")
+ap.add_argument("-w", "--width", type=float, required=True,
+                help="width of the left-most object in the image (in inches)")
+args = vars(ap.parse_args())
+
+# 读取输入图片
+image = cv2.imread(args["image"])
+# 输入图片灰度化
+gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
+# 对灰度图片执行高斯滤波
+gray = cv2.GaussianBlur(gray, (7, 7), 0)
+
+# 对滤波结果做边缘检测获取目标
+edged = cv2.Canny(gray, 50, 100)
+# 使用膨胀和腐蚀操作进行闭合对象边缘之间的间隙
+edged = cv2.dilate(edged, None, iterations=1)
+edged = cv2.erode(edged, None, iterations=1)
+
+# 在边缘图像中寻找物体轮廓(即物体)
+cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
+                        cv2.CHAIN_APPROX_SIMPLE)
+cnts = imutils.grab_contours(cnts)
+
+# 对轮廓按照从左到右进行排序处理
+(cnts, _) = contours.sort_contours(cnts)
+# 初始化 'pixels per metric' 
+pixelsPerMetric = None
+
+# 循环遍历每一个轮廓
+for c in cnts:
+    # 如果当前轮廓的面积太少,认为可能是噪声,直接忽略掉
+    if cv2.contourArea(c) < 100:
+        continue
+
+    # 根据物体轮廓计算出外切矩形框
+    orig = image.copy()
+    box = cv2.minAreaRect(c)
+    box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
+    box = np.array(box, dtype="int")
+
+    # 按照top-left, top-right, bottom-right, bottom-left的顺序对轮廓点进行排序,并绘制外切的BB,用绿色的线来表示
+    box = perspective.order_points(box)
+    cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
+
+    # 绘制BB的4个顶点,用红色的小圆圈来表示
+    for (x, y) in box:
+        cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1)
+
+    # 分别计算top-left 和top-right的中心点和bottom-left 和bottom-right的中心点坐标
+    (tl, tr, br, bl) = box
+    (tltrX, tltrY) = midpoint(tl, tr)
+    (blbrX, blbrY) = midpoint(bl, br)
+
+    # 分别计算top-left和top-right的中心点和top-righ和bottom-right的中心点坐标
+    (tlblX, tlblY) = midpoint(tl, bl)
+    (trbrX, trbrY) = midpoint(tr, br)
+
+    # 绘制BB的4条边的中心点,用蓝色的小圆圈来表示
+    cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1)
+    cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1)
+    cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1)
+    cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1)
+
+    # 在中心点之间绘制直线,用紫红色的线来表示
+    cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)),
+             (255, 0, 255), 2)
+    cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)),
+             (255, 0, 255), 2)
+
+    # 计算两个中心点之间的欧氏距离,即图片距离
+    dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))
+    dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))
+
+    # 初始化测量指标值,参考物体在图片中的宽度已经通过欧氏距离计算得到,参考物体的实际大小已知
+    if pixelsPerMetric is None:
+        pixelsPerMetric = dB / args["width"]
+
+    # 计算目标的实际大小(宽和高),用英尺来表示
+    dimA = dA / pixelsPerMetric
+    dimB = dB / pixelsPerMetric
+
+    # 在图片中绘制结果
+    cv2.putText(orig, "{:.1f}in".format(dimB),
+                (int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX,
+                0.65, (255, 255, 255), 2)
+    cv2.putText(orig, "{:.1f}in".format(dimA),
+                (int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX,
+                0.65, (255, 255, 255), 2)
+
+    # 显示结果
+    cv2.imshow("Image", orig)
+    cv2.waitKey(0)

+ 16 - 0
opencv_threshold.py

@@ -0,0 +1,16 @@
+import cv2
+def test22():
+  src = cv2.imread("test1.jpg")
+  # 灰度图片转换
+
+  gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
+  # 二进制阈值化处理
+  r, b = cv2.threshold(gray, 155, 255, cv2.THRESH_BINARY)
+  # 显示图像
+  cv2.imshow("src", src)
+  cv2.imshow("result", b)
+  print(r)
+
+  if cv2.waitKey(0) == 27:
+    cv2.destroyAllWindows()
+test22()