如何将多个坐标用列表保存
发布网友
发布时间:2023-08-25 03:29
我来回答
共2个回答
热心网友
时间:2024-03-17 15:34
利用数据提取工具提取坐标,数据提取工具是根据图层来提取数据的,所以在提取数据前,务必保证需提取数据在同一个图层,这样就能方便同时保存了。
热心网友
时间:2024-03-17 15:34
import cv2
import pytesseract
coordinates = []
# Defining the event listener (callback function)
def shape_selection(event, x, y, flags, param):
# making coordinates global
global coordinates
# Storing the (x1,y1) coordinates when left mouse button is pressed
if event == cv2.EVENT_LBUTTONDOWN:
coordinates = [(x, y)]
# Storing the (x2,y2) coordinates when the left mouse button is released and make a rectangle on the selected region
elif event == cv2.EVENT_LBUTTONUP:
coordinates.append((x, y))
# Drawing a rectangle around the region of interest (roi)
cv2.rectangle(image, coordinates[0], coordinates[1], (0,0,255), 2)
cv2.imshow("image", image)
# load the image, clone it, and setup the mouse callback function
image = cv2.imread(r'C:\Users\User\Desktop\ocr template\Sample_Invoice.jpg')
image = cv2.resize(image,(1000,1000))
image_copy = image.copy()
cv2.namedWindow("image")
cv2.setMouseCallback("image", shape_selection)
f = open(r'C:\Users\User\Desktop\ocr template\data.txt', "a")
# keep looping until the 'q' key is pressed
while True:
# display the image and wait for a keypress
cv2.imshow("image", image)
key = cv2.waitKey(1) & 0xFF
if key==13:
image_roi = image_copy[coordinates[0][1]:coordinates[1][1],
coordinates[0][0]:coordinates[1][0]]
text = pytesseract.image_to_string(image_roi).replace(',', ' ').replace('\f', '')
print(text)
f.write(text + '\n')
if key == ord("c"):
image = image_copy.copy()
if key == ord("q"):
f.close()
break
if len(coordinates) == 2:
image_roi = image_copy[coordinates[0][1]:coordinates[1][1],
coordinates[0][0]:coordinates[1][0]]
cv2.imshow("Selected Region of Interest - Press any key to proceed", image_roi)
cv2.waitKey(0)
# closing all open windows
cv2.destroyAllWindows()
为了将所选部分的坐标存储为列表,我应该对代码做什么更改