python 打开xml文件修改其中的值,通过点选图像获得其像素值

import cv2
import xml.etree.ElementTree as ET
import matplotlib.pyplot as plt
import numpy as np

# 图片路径
img = cv2.imread("photos/Check_021.jpg",1)
img = cv2.resize(img, (img.shape[1]*2,img.shape[0]*2))
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(imgRGB, cv2.COLOR_BGR2GRAY)


def on_EVENT_LBUTTONDOWN(event, x, y, flags, param):
    pre_pt = (0, 0)
    cur_pt = (0, 0)
    if event == cv2.EVENT_LBUTTONDOWN:
        # pre_pt = x,y
        # print("pre_pt", (x, y))

        thresholdValue = gray[y,x]

        configFile_xml = "config.xml"
        tree = ET.parse(configFile_xml)
        root = tree.getroot()
        secondRoot = root.find("thresholdValue")
        print(secondRoot.text)

        secondRoot.text = str(thresholdValue)
        tree.write("wellConfig.xml")

        print(thresholdValue)
        cv2.circle(img, (x, y), 1, (0, 0, 255), thickness=8)
        cv2.putText(img, str(thresholdValue), (x, y), cv2.FONT_HERSHEY_PLAIN,
                    1.0, (0, 0, 0), thickness=1)
        cv2.imshow("image", gray)
        # plt.imshow(imgRGB)
        # plt.show()


        # if event == cv2.EVENT_LBUTTONDOWN and event == cv2.EVENT_MOUSEMOVE:
        #     cur_pt = x, y
        #     print("cur", (x,y))


# cv2.namedWindow("image")
# cv2.setMouseCallback("image", on_EVENT_LBUTTONDOWN)
# cv2.imshow("image", gray)
# cv2.waitKey(0)



 # 可以放大读像素
fig = plt.figure()
def call_back(event):
    # 获取的是plt上的实际坐标
    x =int(event.xdata)
    y = int(event.ydata)

    # print(x,y)
    thresholdValue = gray[y, x]

    configFile_xml = "config.xml"
    tree = ET.parse(configFile_xml)
    root = tree.getroot()
    secondRoot = root.find("thresholdValue")
    print(secondRoot.text)

    secondRoot.text = str(thresholdValue)
    tree.write("wellConfig.xml")
    print("thresholdValue", thresholdValue)
    fig.canvas.draw_idle()  # 绘图动作实时反映在图像上


fig.canvas.mpl_connect('button_press_event', call_back)
plt.imshow(imgRGB)
plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值