plt.plot(…)没有显示窗口。我错过了什么吗?

huangapple go评论111阅读模式
英文:

plt.plot(...) doesn't show a window. what am I missing?

问题

Here's the translated code portion:

  1. 我正在使用VScode进行OpenCV练习
  2. 我习惯于使用以下结构显示图像
  3. import cv2
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6. puppy = cv2.imread('./DATA/00-puppy.jpg')
  7. horse = cv2.imread('./DATA/horse.jpg')
  8. rainbow = cv2.imread('./DATA/rainbow.jpg')
  9. # 对这些图像进行“inplace”操作,例如
  10. # puppy = cv2.cvtColor(puppy, cv2.COLOR_BGR2RGB)
  11. # 等...
  12. img_to_show_1 = puppy
  13. img_to_show_2 = horse
  14. img_to_show_3 = rainbow
  15. # ---> 开始“while True ... - if 0xFF == 27: break”结构
  16. cv2.namedWindow(winname='window_1')
  17. cv2.namedWindow(winname='window_2')
  18. cv2.namedWindow(winname='window_3')
  19. while True:
  20. cv2.imshow('window_1', img_to_show_1)
  21. cv2.imshow('window_2', img_to_show_2)
  22. cv2.imshow('window_3', img_to_show_3)
  23. if cv2.waitKey(1) & 0xFF == 27: # 当按下'esc'键时停止循环
  24. break
  25. cv2.destroyAllWindows()
  26. # ---> 结束“while True ... - if 0xFF == 27: break”结构
  27. 现在我已计算存储在puppy变量中的图像的值的直方图
  28. hist_values = cv2.calcHist([puppy], channels=[0], mask=None, histSize=[256], ranges=[0,256])
  29. Jupyter笔记本上可以通过简单执行以下操作来显示它
  30. plt.plot(hist_values)
  31. 但在VScode这不会显示任何内容
  32. 是否有一种方法可以在VScode上显示此图像而不需要安装任何其他图形扩展
  33. 是否有一种方法可以利用相同的while True ... - if 0xFF == 27: break结构以便在按下'esc'键时也使此图像消失就像其他图像一样

This is the translated code portion you requested.

英文:

I am practicing with open cv on VScode.

I am used to display images by using the following structure:

  1. import cv2
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. puppy = cv2.imread('./DATA/00-puppy.jpg')
  5. horse = cv2.imread('./DATA/horse.jpg')
  6. rainbow = cv2.imread('./DATA/rainbow.jpg')
  7. # "inplace" operations on these images such as
  8. # puppy = cv2.cvtColor(puppy, cv2.COLOR_BGR2RGB)
  9. # etc...
  10. img_to_show_1 = puppy
  11. img_to_show_2 = horse
  12. img_to_show_3 = rainbow
  13. # ---> beginning of the "while True ... - if 0xFF == 27: break" structure
  14. cv2.namedWindow(winname='window_1')
  15. cv2.namedWindow(winname='window_2')
  16. cv2.namedWindow(winname='window_3')
  17. while True:
  18. cv2.imshow('window_1', img_to_show_1)
  19. cv2.imshow('window_2', img_to_show_2)
  20. cv2.imshow('window_3', img_to_show_3)
  21. if cv2.waitKey(1) & 0xFF == 27: # stop loop when 'esc' key is pressed
  22. break
  23. cv2.destroyAllWindows()
  24. # ---> end of the "while True ... - if 0xFF == 27: break" structure

Now I have calculated the histogram of the values of an image stored in puppy variable

  1. hist_values = cv2.calcHist([puppy], channels=[0], mask=None, histSize=[256], ranges=[0,256])

On jupyter notebooks, this can be showed by simply doing:

  1. plt.plot(hist_values)

But on VScode, this does not show anything.

Is there a way to show this image on VScode and without intalling any other graphical extention?

Is there a way to exploit the same "while True ... - if 0xFF == 27: break" structure, in order to have this image also disappear with the others as I press 'esc' key?

答案1

得分: 0

我找到了一个解决方法。

这基于使用OpenCV模块方法保存图像,然后使用Matplotlib方法重新加载图像:

  1. import cv2
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. puppy = cv2.imread('./DATA/00-puppy.jpg')
  5. horse = cv2.imread('./DATA/horse.jpg')
  6. rainbow = cv2.imread('./DATA/rainbow.jpg')
  7. hist_values = cv2.calcHist([puppy], channels=[0], mask=None, histSize=[256], ranges=[0, 256])
  8. plt.plot(hist_values)
  9. plt.plot([1, 2])
  10. mypath = 'my/path/hist_values.jpg'
  11. plt.savefig(mypath)
  12. hist_values_img = cv2.imread(mypath)
  13. import os
  14. try:
  15. os.remove(mypath)
  16. except:
  17. print("无法删除{}".format(mypath))
  18. img_to_show_1 = puppy
  19. img_to_show_2 = hist_values_img
  20. img_to_show_3 = rainbow
  21. cv2.namedWindow(winname='window_1')
  22. cv2.namedWindow(winname='window_2')
  23. cv2.namedWindow(winname='window_3')
  24. while True:
  25. cv2.imshow('window_1', img_to_show_1)
  26. cv2.imshow('window_2', img_to_show_2)
  27. cv2.imshow('window_3', img_to_show_3)
  28. plt.show()
  29. if cv2.waitKey(1) & 0xFF == 27: # 如果按下esc键
  30. break
  31. cv2.destroyAllWindows()

通过这种方式,我没有混合GUI,并且当我按下'esc'键时,直方图图像会与其他图像一起关闭。

感谢@ChristophRackwitz。

另外,将Matplotlib格式的元素转换为OpenCV格式的元素的代码段可以缩短为以下函数:

  1. def convert_matplotlib_to_opencv_img(matplotlib_format_img, clear=False, transitory_img_path='my_transitory_img.jpg'):
  2. if clear:
  3. plt.clf()
  4. plt.plot(matplotlib_format_img)
  5. try:
  6. plt.savefig(transitory_img_path)
  7. except:
  8. raise OSError("无法保存图像到{}".format(transitory_img_path))
  9. cv2_imread_format_img = cv2.imread(transitory_img_path)
  10. import os
  11. try:
  12. os.remove(transitory_img_path)
  13. except:
  14. print("无法删除{}".format(transitory_img_path))
  15. return cv2_imread_format_img

希望这对你有所帮助。

英文:

I found a workaround to do it.

This is based on saving the image with opencv module method and to reload it with the matplotlib method:

  1. import cv2
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. puppy = cv2.imread('./DATA/00-puppy.jpg')
  5. horse = cv2.imread('./DATA/horse.jpg')
  6. rainbow = cv2.imread('./DATA/rainbow.jpg')
  7. # "inplace" operations on these images such as
  8. # puppy = cv2.cvtColor(puppy, cv2.COLOR_BGR2RGB)
  9. # etc...
  10. hist_values = cv2.calcHist([puppy], channels=[0], mask=None, histSize=[256], ranges=[0,256])
  11. plt.plot(hist_values)
  12. plt.plot([1, 2])
  13. mypath = 'my/path/hist_values.jpg'
  14. plt.savefig(mypath)
  15. hist_values_img = cv2.imread(mypath)
  16. import os
  17. try:
  18. os.remove(hist_values_path)
  19. except:
  20. print("could not remove {}".format(hist_values_path))
  21. img_to_show_1 = puppy
  22. img_to_show_2 = hist_values_img
  23. img_to_show_3 = rainbow
  24. # ---> beginning of the "while True ... - if 0xFF == 27: break" structure
  25. cv2.namedWindow(winname='window_1')
  26. cv2.namedWindow(winname='window_2')
  27. cv2.namedWindow(winname='window_3')
  28. while True:
  29. cv2.imshow('window_1', img_to_show_1)
  30. cv2.imshow('window_2', img_to_show_2)
  31. cv2.imshow('window_3', img_to_show_3)
  32. plt.show()
  33. if cv2.waitKey(1) & 0xFF == 27: # se premo esc
  34. break
  35. cv2.destroyAllWindows()
  36. # ---> end of the "while True ... - if 0xFF == 27: break" structure

In this way I am not mixing GUIs and as I press 'esc', the histogram image closes together with the other images.

Credits to @ChristophRackwitz

Also, the snippet to convert a matplotlib-format element to a opencv-format element can be shrinked to this function:

  1. def convert_matplotlib_to_opencv_img(matplotlib_format_img, clear=False, transitory_img_path='my_transitory_img.jpg'):
  2. if clear:
  3. plt.clf()
  4. plt.plot(matplotlib_format_img)
  5. try:
  6. plt.savefig(transitory_img_path)
  7. except:
  8. raise OSError("could not save img to {}".format(transitory_img_path))
  9. cv2_imread_format_img = cv2.imread(transitory_img_path)
  10. import os
  11. try:
  12. os.remove(transitory_img_path)
  13. except:
  14. print("could not remove {}".format(transitory_img_path))
  15. return cv2_imread_format_img

huangapple
  • 本文由 发表于 2023年6月18日 20:12:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定