我使用numpy来创建网格并填充cols,rows和center。如何使用QPixmap显示此网格?
我的代码:
class GridTest(QWidget):
def __init__(self, parent=None):
super(GridTest, self).__init__(parent)
self.makeGrid = QLabel("Ground Truth")
layout = QVBoxLayout()
layout.addWidget(self.makeGrid)
self.setLayout(layout)
def fill_square(self):
# Parameters
jpg_offset_row = 3
jpg_offset_col = 6
step = 10
out_size = 400
line_color = (0, 255, 255)
center_color = (0, 0, 255)
# Initialize white background
grid = np.ones((step*8+9, step*8+9, 3), np.uint8)*255
# Create the grid
for idx in range(9):
ctr = (step+1)*idx
grid[:, ctr] = 0
grid[ctr, :] = 0
for col in range(8):
left_x = (step + 1) * col + 1
right_x = (step + 1) * (col + 1) - 1
top_y = (step + 1) * jpg_offset_row + 1
bottom_y = (step + 1) * (jpg_offset_row + 1) - 1
corners = np.asarray(
[(left_x, top_y),
(right_x, top_y),
(right_x, bottom_y),
(left_x, bottom_y)]
)
cv2.fillConvexPoly(grid, corners, line_color)
for row in range(8):
left_x = (step + 1) * jpg_offset_col + 1
right_x = (step + 1) * (jpg_offset_col + 1) - 1
top_y = (step + 1) * row + 1
bottom_y = (step + 1) * (row + 1) - 1
corners = np.asarray(
[(left_x, top_y),
(right_x, top_y),
(right_x, bottom_y),
(left_x, bottom_y)]
)
cv2.fillConvexPoly(grid, corners, line_color)
left_x2 = (step + 1) * jpg_offset_col + 1
right_x2 = (step + 1) * (jpg_offset_col + 1) - 1
top_y2 = (step + 1) * jpg_offset_row + 1
bottom_y2 = (step + 1) * (jpg_offset_row + 1) - 1
corners2 = np.asarray(
[(left_x2, top_y2),
(right_x2, top_y2),
(right_x2, bottom_y2),
(left_x2, bottom_y2)]
)
cv2.fillConvexPoly(grid, corners2, center_color)
grid = cv2.resize(grid, (out_size, out_size), interpolation=cv2.INTER_NEAREST)
new_image = Image.fromarray(grid.astype(np.uint8))
qimage = ImageQt(new_image)
self.makeGrid.setPixmap(QPixmap.fromImage(qimage))
self.makeGrid.show()
def main():
app = QApplication(sys.argv)
ex = GridTest()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
和libs是 导入系统 来自PyQt4.QtCore import * 来自PyQt4.QtGui import * 来自PIL导入图片 导入numpy为np 从PIL.ImageQt导入ImageQt import cv2