当前位置:   article > 正文

【丁丁历险记】Opencv+tkinter 做一个图传上位机_上位机和opencv库怎么结合

上位机和opencv库怎么结合


一、图传上位机界面设计

  • 许多同学在制作图传或者使用opencv-python的包时,往往因为不会做界面而导致不能实时进行与图像的交互功能。因此一个简单稳定的上位机界面变得十分重要了。
  • 先做一个界面看看
    1. from tkinter import *
    2. def tap():
    3. e2Text.set("点我,点我")
    4. root = Tk()
    5. root.title("如易科技公众平台")
    6. root.geometry('850x500')
    7. """
    8. 图像画布设置
    9. """
    10. panel = Label(root)
    11. panel.place(x=1,y=1,anchor='nw')
    12. root.config(cursor="arrow")
    13. """
    14. 右侧组件
    15. """
    16. l1Text = StringVar()
    17. l1 = Label(root,text = "按钮点击测试",bg='green',font=('Arial', 12), width=22, height=2)
    18. l1.place(x=642,y=2,anchor='nw')
    19. e1 = Entry(root,width = 10)
    20. e1.place(x=680,y=55,anchor='nw')
    21. b1 = Button(root, text="确定!", command=tap)
    22. b1.place(x=780,y=50,anchor='nw')
    23. l2Text = StringVar()
    24. l2 = Label(root,text = "输出窗口测试",bg='red',font=('Arial', 12), width=22, height=2)
    25. l2.place(x=642,y=90 ,anchor='nw')
    26. l3Var = StringVar()
    27. e2Text = StringVar()
    28. e2 = Entry(root,textvariable = e2Text,state='disabled',bg='white',bd=2, width=20)
    29. e2.place(x=665,y=135 ,anchor='nw')
    30. root.mainloop()

    运行界面后

 二、添加摄像头的动态显示

  • 添加opencv的视频流捕获函数
  • 将视频现实窗口放在左侧的空白部分
    1. from tkinter import *
    2. import cv2
    3. from PIL import Image,ImageTk
    4. grakMark = False
    5. def tap():
    6. global grakMark
    7. if grakMark:
    8. grakMark = False
    9. else:
    10. grakMark = True
    11. def video_loop(): #动态图像现实窗口
    12. success, img = camera.read() # 从摄像头读取照片
    13. global grakMark
    14. if success:
    15. """
    16. 摄像头图像
    17. """
    18. img = cv2.resize(img, (640, 480))
    19. if grakMark:
    20. img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    21. cv2.waitKey(10)
    22. cv2image = cv2.cvtColor(img, cv2.COLOR_BGR2RGBA)#转换颜色从BGR到RGBA
    23. current_image = Image.fromarray(cv2image)#将图像转换成Image对象
    24. imgtk = ImageTk.PhotoImage(image=current_image)
    25. panel.imgtk = imgtk
    26. panel.config(image=imgtk)
    27. root.after(5, video_loop)
    28. camera = cv2.VideoCapture(0)
    29. root = Tk()
    30. root.title("如易科技公众平台")
    31. root.geometry('850x500')
    32. """
    33. 图像画布设置
    34. """
    35. panel = Label(root)
    36. panel.place(x=1,y=1,anchor='nw')
    37. root.config(cursor="arrow")
    38. """
    39. 右侧组件
    40. """
    41. l1Text = StringVar()
    42. l1 = Label(root,text = "按钮点击测试",bg='green',font=('Arial', 12), width=22, height=2)
    43. l1.place(x=642,y=2,anchor='nw')
    44. e1 = Entry(root,width = 10)
    45. e1.place(x=680,y=55,anchor='nw')
    46. b1 = Button(root, text="确定!", command=tap)
    47. b1.place(x=780,y=50,anchor='nw')
    48. l2Text = StringVar()
    49. l2 = Label(root,text = "输出窗口测试",bg='red',font=('Arial', 12), width=22, height=2)
    50. l2.place(x=642,y=90 ,anchor='nw')
    51. l3Var = StringVar()
    52. e2Text = StringVar()
    53. e2 = Entry(root,textvariable = e2Text,state='disabled',bg='white',bd=2, width=20)
    54. e2.place(x=665,y=135 ,anchor='nw')
    55. video_loop()
    56. root.mainloop()
    57. # 当一切都完成后,关闭摄像头并释放所占资源
    58. camera.release()
    59. cv2.destroyAllWindows()

    点击确定按钮,图像则会变成灰度图

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/112177?site
推荐阅读
相关标签
  

闽ICP备14008679号