- ©«¤l
- 254
- ¥DÃD
- 6
- ºëµØ
- 0
- ¿n¤À
- 310
- ÂI¦W
- 0
- §@·~¨t²Î
- W10
- ³nÅ骩¥»
- Excel 2016
- ¾\ŪÅv
- 20
- ©Ê§O
- ¨k
- ¨Ó¦Û
- ¥xÆW
- µù¥U®É¶¡
- 2019-6-16
- ³Ì«áµn¿ý
- 2024-9-23
|
[µo°Ý] ·s¤â°ÝÃD python3 & OpenCV 4.5.3 ª©¥» Ãö©ó°õ¦æµ{¦¡·|¥d¹yªº°ÝÃD
³o¬O¤@Ó¦b·Ó¤ù¤Wµeª½½u µM«á¶q´úȽuªø«×ªºµ{¦¡½m²ß
³o¬qµ{¦¡¥N½X¦³¤@Ó°ÝÃD ¦b°õ¦æµ{¦¡ªº®ÉÔ ·|¥d¹y
¦b·QÀ³¸Ó¬O °j°éªº°ÝÃD ¨ç¦¡¤¤ ¦³¤@¬q :
directory = 'C:/VSC_PY/GUI_AI/sand_train/text_dir/' # ¥Ø¿ý¸ô®|
total_items = count_files(directory)
if total_items > 1:
print("¦¹¥Ø¿ý¤Uªº¶µ¼ÆÁ`p¤j©ó 2¡C")
print(total_items)
# Ū¨ú·Ó¤ù
img1 = cv2.imread('C:/VSC_PY/GUI_AI/sand_train/text_dir/TEST1.jpg')
#½Æ»s·Ó¤ùÁקK´Ý¼v²{¶H None
img1 = img1.copy()
else:
print("¦¹¥Ø¿ý¤Uªº¶µ¼ÆÁ`p¤£¨¬ 2¡C")
img1 = img.copy()
À³¸Ó¬O³o¤@¬q³y¦¨ªº¥d¹y ¤£ª¾¹D ¦³¨S¦³¤j¤j¥i¥H§iª¾¤p§Ì
¸Ó¦p¦óקï¤~¤£·|¥d¹y- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- import cv2 #OpenCV version: 4.5.3 print("OpenCV version:", cv2.__version__)
- # import numpy as np
- import math
- import os
- # ¦C¥X«ü©w¥Ø¿ý¤¤ªº©Ò¦³ÀɮשM¤l¥Ø¿ý¡C
- def count_files(directory):
- total = 0
- for root, dirs, files in os.walk(directory):
- total += len(files) + len(dirs)
- return total
- # §R°£ªº¹Ï¹³
- def delete_image(directory, filename):
- filepath = os.path.join(directory, filename)
- if os.path.isfile(filepath):
- os.remove(filepath)
- print(f"Deleted {filename}.")
- else:
- print(f"{filename} ¤£¦s¦b")
- # ·í¥ªÁä«ö¤U¨Ã²¾°Ê®Éø»s¹Ï§Î¡Aevent¥i¥H¬d¬Ý²¾°Ê¡Aflag¬d¬Ý¬O§_«ö¤U
- # ©w¸q `draw_line()` ¨ç¼Æ¨Ó³B²z·Æ¹«¨Æ¥ó¡A¨Ò¦p¦b¹Ï¹³¤Wø»s½u±ø
- def draw_line(event, x, y, flags, param):
- if event == cv2.EVENT_FLAG_LBUTTON: # ¥ªÁä«ö¤U
- global start_point, isDrawing ,img1
- start_point = (x, y) # ©ì¦²·Æ¹«®É¡A¤£Â_°O¿ý®y¼Ð
- isDrawing = True
- print("¥ªÁä«ö¤U")
- print(start_point)
- elif event == cv2.EVENT_MOUSEMOVE and isDrawing: # ²¾°Ê·Æ¹«®Éø¹Ï
- # end_point = (x,y) # ©ì¦²·Æ¹«®É¡A¤£Â_°O¿ý®y¼Ð
- end_point = (x,y)
- print("²¾°Ê·Æ¹«")
- print(end_point)
- directory = 'C:/VSC_PY/GUI_AI/sand_train/text_dir/' # ¥Ø¿ý¸ô®|
- total_items = count_files(directory)
- if total_items > 1:
- print("¦¹¥Ø¿ý¤Uªº¶µ¼ÆÁ`p¤j©ó 2¡C")
- print(total_items)
- # Ū¨ú·Ó¤ù
- img1 = cv2.imread('C:/VSC_PY/GUI_AI/sand_train/text_dir/TEST1.jpg')
- #½Æ»s·Ó¤ùÁקK´Ý¼v²{¶H None
- img1 = img1.copy()
- else:
- print("¦¹¥Ø¿ý¤Uªº¶µ¼ÆÁ`p¤£¨¬ 2¡C")
- img1 = img.copy()
-
- # P1 »P P2 ¬O¤wª¾ªºÂI
- # P1 = (start_point[0], start_point[1])
- # P2 = (end_point[0], end_point[1])
- P1 = (start_point[-2], start_point[-1])
- P2 = (end_point[-2], end_point[-1])
- cv2.line(img1, P1, P2, (0, 255, 0), 3)
-
- # pºâª½½uªø«×¤½¦¡¬°¡Glength = sqrt((x2-x1)^2 + (y2-y1)^2)
- length = math.sqrt((P2[0] - P1[0])**2 + (P2[1] - P1[1])**2)
- # length = np.sqrt((P2[0]-P1[0])**2 + (P2[1]-P1[1])**2) # pºâª½½uªºªø«×
- # ªø«×³æ¦ì micrometer¡]um¡^
- length = length * 1000
- # Åã¥Ü®y¼Ð cv2.threshold() strXY=str(x)+','+str(y)+"um"
- strXY=str(x)+','+str(y)+','+str(length)+"um"
- font=cv2.FONT_HERSHEY_PLAIN
- cv2.putText(img1,strXY,(x+10,y+10),font,1,(0,255,0),2)
- cv2.imshow("Image", img1)
- elif event == cv2.EVENT_LBUTTONUP: # ¥ªÁäÄÀ©ñ True False
- isDrawing = False
- print("¥ªÁäÄÀ©ñ")
- print(isDrawing)
- #«O¦s·Ó¤ù
- cv2.imwrite('C:/VSC_PY/GUI_AI/sand_train/text_dir/TEST1.jpg', img1)
-
- elif event == cv2.EVENT_RBUTTONDOWN:
- print("¥kÁä«ö¤U")
- if __name__ == "__main__":
- directory = 'C:/VSC_PY/GUI_AI/sand_train/text_dir/' # ¥Ø¿ý¸ô®|
- filename = "TEST1.jpg" # ¹Ï¤ùªº¦WºÙ
- delete_image(directory, filename) #§R°£·Ó¤ù
- img3 = img.copy()
- cv2.imshow("Image", img3)
- # ¸ü¤J¹Ï¤ù 'C:/VSC_PY/GUI_AI/sand_train/text_dir/TEST.jpg'
- # ¹w³]¨Ï¥Î cv2.IMREAD_COLOR ¼Ò¦¡
- img = cv2.imread('C:/VSC_PY/GUI_AI/sand_train/text_dir/TEST.jpg')
- # cv2.imshow('oxxostudio', img) # ¨Ï¥Î¦W¬° oxxostudio ªºµøµ¡¶}±Ò¹Ï¤ù
- # «Ø¥ß¤@Óµøµ¡¥HÅã¥Ü¼v¹³¨Ã³]©w¨Æ¥óºÊÅ¥¾¹
- cv2.namedWindow("Image")
- # °»´ú«ü©wµøµ¡¤Uªº·Æ¹«¨Æ¥ó
- cv2.setMouseCallback("Image", draw_line)
- # ±Ò°Ê¥D°j°é¥HÅã¥Ü©M§ó·s¹Ï¹³¡A¥]¬Aø»sªº½u±ø
- while True:
- cv2.imshow("Image", img)
- # «ö¤U¥ô·NÁä°±¤î
- if cv2.waitKey(0):
- cv2.destroyAllWindows() # µ²§ô©Ò¦³¹Ï¤ùµøµ¡
- break
½Æ»s¥N½X |
|