pyqt5用label显示灰度图片的直方图代码
时间: 2023-04-02 08:00:48 浏览: 304
以下是pyqt5用label显示灰度图片的直方图代码:
```python
import cv2
import numpy as np
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QLabel, QApplication, QMainWindow
from PyQt5.QtCore import Qt
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 读取灰度图片
img = cv2.imread('gray_image.jpg', cv2.IMREAD_GRAYSCALE)
# 计算直方图
hist, bins = np.histogram(img.ravel(), 256, [, 256])
# 创建画布
canvas = np.zeros((100, 256, 3), dtype=np.uint8)
# 绘制直方图
for i in range(256):
cv2.line(canvas, (i, ), (i, hist[i]), (255, 255, 255))
# 转换为QImage
qimage = QImage(canvas.data, canvas.shape[1], canvas.shape[], QImage.Format_RGB888)
# 显示直方图
label = QLabel(self)
label.setPixmap(QPixmap.fromImage(qimage))
label.setAlignment(Qt.AlignCenter)
self.setCentralWidget(label)
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
```
希望对你有帮助!
阅读全文