TypeError:不支持mat数据类型= 19

时间:2018-12-15 19:08:38

标签: python image numpy opencv imshow

我正在编写一个创建随机图像的代码。我这样做的方法是采用许多不同的随机值并创建一个文件。然后将通过OpenCV读取并显示文件。参见示例。

    def R_Write():
       if os.path.exists("draw_data.pgi"):
          os.remove("draw_data.pgi")
       d_data = open("draw_data.pgi", "w")
       d_data.write("[[[")
       d_data.write(d[1] + " " + d[2] + " " + d[3]+ "]" + "\n")
       d_data.write("  " + "[" + d[4] + " " + d[5] + " " + d[6] + "]" + "\n")
       d_data.write("  " + "[" + d[7] + " " + d[8] + " " + d[9] + "]" + "\n")
       d_data.write("  " + "..." + "\n")
       d_data.write("  " + "[" + d[10] + " " + d[11] + " " + d[12] + "]" + "\n")
       d_data.write("  " + "[" + d[13] + " " + d[14] + " " + d[15] + "]" + "\n")
       d_data.write("  " + "[" + d[16] + " " + d[17] + " " + d[18] + "]]" + "\n")
       d_data.write("\n")
       d_data.write(" " + "[[" + d[19] + " " + d[20] + " " + d[21] + "]" + "\n")
       d_data.write("  " + "[" + d[22] + " " + d[23] + " " + d[24] + "]" + "\n")
       d_data.write("  " + "[" + d[25] + " " + d[26] + " " + d[27] + "]" + "\n")
       d_data.write("  " + "..." + "\n")
       d_data.write("  " + "[" + d[28] + " " + d[29] + " " + d[30] + "]" + "\n")
       d_data.write("  " + "[" + d[31] + " " + d[32] + " " + d[33] + "]" + "\n")
       d_data.write("  " + "[" + d[34] + " " + d[35] + " " + d[36] + "]]" + "\n")
       d_data.write("\n")
       d_data.write(" " + "[[" + d[37] + " " + d[38] + " " + d[39] + "]" + "\n")
       d_data.write("  " + "[" + d[40] + " " + d[41] + " " + d[42] + "]" + "\n")
       d_data.write("  " + "[" + d[43] + " " + d[44] + " " + d[45] + "]" + "\n")
       d_data.write("  " + "..." + "\n")
       d_data.write("  " + "[" + d[46] + " " + d[47] + " " + d[48] + "]" + "\n")
       d_data.write("  " + "[" + d[49] + " " + d[50] + " " + d[51] + "]" + "\n")
       d_data.write("  " + "[" + d[52] + " " + d[53] + " " + d[54] + "]]" + "\n")
       d_data.write("\n")
       d_data.write(" " + "..." + "\n")
       d_data.write("\n")
       d_data.write(" " + "[[" + d[37] + " " + d[38] + " " + d[39] + "]" + "\n")
       d_data.write("  " + "[" + d[40] + " " + d[41] + " " + d[42] + "]" + "\n")
       d_data.write("  " + "[" + d[43] + " " + d[44] + " " + d[45] + "]" + "\n")
       d_data.write("  " + "..." + "\n")
       d_data.write("  " + "[" + d[46] + " " + d[47] + " " + d[48] + "]" + "\n")
       d_data.write("  " + "[" + d[49] + " " + d[50] + " " + d[51] + "]" + "\n")
       d_data.write("  " + "[" + d[52] + " " + d[53] + " " + d[54] + "]]" + "\n")
       d_data.write("\n")
       d_data.write(" " + "[[" + d[55] + " " + d[56] + " " + d[57] + "]" + "\n")
       d_data.write("  " + "[" + d[58] + " " + d[59] + " " + d[60] + "]" + "\n")
       d_data.write("  " + "[" + d[61] + " " + d[62] + " " + d[63] + "]" + "\n")
       d_data.write("  " + "..." + "\n")
       d_data.write("  " + "[" + d[64] + " " + d[65] + " " + d[66] + "]" + "\n")
       d_data.write("  " + "[" + d[67] + " " + d[68] + " " + d[69] + "]" + "\n")
       d_data.write("  " + "[" + d[70] + " " + d[71] + " " + d[72] + "]]" + "\n")
       d_data.write("\n")
       d_data.write(" " + "[[" + d[73] + " " + d[74] + " " + d[75] + "]" + "\n")
       d_data.write("  " + "[" + d[76] + " " + d[77] + " " + d[78] + "]" + "\n")
       d_data.write("  " + "[" + d[79] + " " + d[80] + " " + d[81] + "]" + "\n")
       d_data.write("  " + "..." + "\n")
       d_data.write("  " + "[" + d[82] + " " + d[83] + " " + d[84] + "]" + "\n")
       d_data.write("  " + "[" + d[85] + " " + d[86] + " " + d[87] + "]" + "\n")
       d_data.write("  " + "[" + d[88] + " " + d[89] + " " + d[90] + "]]]")
       d_data.close()
       d_data = open("draw_data.pgi", "r").read()
       print(d_data)
       Draw(d_data)

  def Draw(data):
          cv2.imshow("Random_Pixels", np.array(data))

但是随后发生错误TypeError:不支持mat数据类型= 19。如何将其转换为其他类型或如何解决问题?

我已经尝试了几件事,

    data_2 = np.int16(data)     # convert to signed 16 bit integer to allow overflow
    data_2 = scale_factor*data_2 # apply scale factor
    data_2 = clip(data_2, 0, 255) # force all values to be between 0 and 255

    # after clip img2 is effectively unsigned 8 bit, but make it explicit:
    data_2 = np.uint8(data_2)

请帮助我吗?我真的很想参加这个项目,我想继续下去,但是我不能没有你的帮助。

0 个答案:

没有答案