Пример #1
0
def read_show_img(image_name, show_img=False):
    # raster = gdal.Open('C:\\test.jpg')
    # image_name = 'C:\hyperspectral\AVIRISReflectanceSubset.dat'
    raster = gdal.Open(image_name)
    # raster = gdal.Open('e:\qwer.img')
    print('Reading the image named:', image_name)
    # print('geotrans is ', raster.GetGeoTransform())
    # print('projection is ',raster.GetProjection())
    xsize = raster.RasterXSize  # RasterXSize是图像宽
    ysize = raster.RasterYSize  # RasterYSize是图像的高
    band_num = raster.RasterCount  # RasterCount是图像的波段数
    im_GeoTrans = raster.GetGeoTransform() #仿射矩阵,左上角像素的大地坐标和像素分辨率
                        # (左上角x, x分辨率,仿射变换,左上角y坐标,y分辨率,仿射变换)
    im_proj = raster.GetProjection() #地图投影信息,字符串表示
    print('in read and show x,y', xsize, ysize)

    # 获取rgb对应波段数值,传递给拉伸显示模块

    raster_array = raster.ReadAsArray()  # raster_array.shape=(191,128,155)对应(band_num,Ysize,Xsize),
    # Ysize代表行数,Xsize代表列数
    raster_array_np = np.array(raster_array)

    #
    # print('rnp',np.shape(rnp))
    print('raster_array shape in read_show img', raster_array_np.shape)
    if band_num >= 3:
        r = raster_array[0]
        g = raster_array[1]
        b = raster_array[2]
        print('type of r in read_show_img:', type(r))

        if show_img:  # 判断是否需要显示图像
            strechimg(xsize, ysize, r, g, b)  # 调用图像拉伸显示模块
            # strechimg(92, 92, r[0:92,0:92], g[0:92,0:92], b[0:92,0:92])  # 调用图像拉伸显示模块
    else:
        img_data = raster.GetRasterBand(1)
        img_data = img_data.ReadAsArray()
        # print(np.shape(img_data))
        if show_img:
            strechimg_pan(xsize, ysize, img_data)
            # print()
    return raster, raster_array, xsize, ysize, band_num, im_GeoTrans, im_proj
Пример #2
0
def read_show_tiff(tiff_path):
    raster = gdal.Open(tiff_path)
    xsize = raster.RasterXSize
    ysize = raster.RasterYSize
    band_num = raster.RasterCount
    raster_array = raster.ReadAsArray()

    if band_num >= 3:
        # plt.imshow(raster_array[0,:,:])
        # plt.show()
        r = raster_array[40]
        g = raster_array[20]
        b = raster_array[10]
        stretch.strechimg(xsize, ysize, r, g, b)
    elif band_num == 1:
        # plt.imshow(raster_array)
        # plt.show()
        im = raster_array
        print('max in show tiff', np.max(im))
        stretch.strechimg_pan(xsize, ysize, im, False)
Пример #3
0
def read_from_img(image_name, show_img=False):
    # 用于将遥感影像读入到数组,整体影像。
    # 输入值为影像完整路径,输出为raster,raster_array,xsize, ysize,band_num
    raster = gdal.Open(image_name)
    #raster = gdal.Open('e:\qwer.img')
    print('Read from img function,Reading the image named:', image_name)
    xsize = raster.RasterXSize  # RasterXSize是图像的行数,也就是图的高,即Y
    ysize = raster.RasterYSize  # RasterYSize是图像的列数,图像的宽度
    band_num = raster.RasterCount  #RasterCount是图像的波段数

    # 获取rgb对应波段数值,传递给拉伸显示模块
    raster_array = raster.ReadAsArray(
    )  #raster_array.shape=(191,128,155)对应(band_num,Ysize,Xsize),
    # Ysize代表行数,Xsize代表列数
    #raster_array_np = np.array(raster_array)
    #print('raster_array shape in read_show img',raster_array_np.shape)
    #
    if band_num >= 3:
        # r = raster.GetRasterBand(29)#raster_array[0]
        # g = raster.GetRasterBand(20)#raster_array[1]
        # b = raster.GetRasterBand(12)#raster_array[2]

        # r = r.ReadAsArray()
        # g = g.ReadAsArray()
        # b = b.ReadAsArray()
        r = raster_array[0]
        g = raster_array[1]
        b = raster_array[2]
        print('type of r in read_show_img:', type(r))

        if show_img:  #判断是否需要显示图像
            strechimg(xsize, ysize, r, g, b)  #调用图像拉伸显示模块
            #strechimg(92, 92, r[0:92,0:92], g[0:92,0:92], b[0:92,0:92])  # 调用图像拉伸显示模块
    else:
        img_data = raster.GetRasterBand(1)
        img_data = img_data.ReadAsArray()
        print(np.shape(img_data))
        if show_img:
            strechimg_pan(xsize, ysize, img_data)
Пример #4
0
def read_show_img(image_name,show_img = False):
    #raster = gdal.Open('C:\\test.jpg')
    #image_name = 'C:\hyperspectral\AVIRISReflectanceSubset.dat'
    raster = gdal.Open(image_name)
    #raster = gdal.Open('e:\qwer.img')
    print('Reading the image named:',image_name)
    xsize = raster.RasterXSize  # RasterXSize是图像的行数,也就是图的高,即Y
    ysize = raster.RasterYSize  # RasterYSize是图像的列数,图像的宽度
    band_num = raster.RasterCount #RasterCount是图像的波段数


    # 获取rgb对应波段数值,传递给拉伸显示模块

    raster_array = raster.ReadAsArray() #raster_array.shape=(191,128,155)对应(band_num,Ysize,Xsize),
                                        # Ysize代表行数,Xsize代表列数
    raster_array_np = np.array(raster_array)

#
    #print('rnp',np.shape(rnp))
    print('raster_array shape in read_show img',raster_array_np.shape)
    if band_num >= 3:
        r = raster_array[0]
        g = raster_array[1]
        b = raster_array[2]
        print('type of r in read_show_img:', type(r))

        if show_img:    #判断是否需要显示图像
            strechimg(xsize,ysize,r,g,b) #调用图像拉伸显示模块
            #strechimg(92, 92, r[0:92,0:92], g[0:92,0:92], b[0:92,0:92])  # 调用图像拉伸显示模块
    else:
        img_data = raster.GetRasterBand(1)
        img_data = img_data.ReadAsArray()
        print(np.shape(img_data))
        if show_img:
            strechimg_pan(xsize,ysize,img_data)
            #print()
    return raster,raster_array,xsize, ysize,band_num
    #tensorflow中的数据shape是[batch,height/Ysize,width/Xsize,band_num]
    y_data = y_data.reshape(sample_num, class_num)  #[batch,class_num]
    #print('after y_data')

    xs = tf.placeholder(tf.float32,
                        [None, sample_size_Y, sample_size_X, band_num])  #
    ys = tf.placeholder(tf.float32, [None, class_num])
    keep_prob = tf.placeholder(tf.float32)

    train = False
    if train:
        train_cnn()
    else:
        test_xs, test_ys = get_next_batch(x_data, y_data, num_per_class,
                                          sample_num, 60)
        print(test_xs.shape)
        x_img = np.reshape(test_xs[0, :, :, :],
                           [band_num, sample_size_Y, sample_size_X])
        strechimg(sample_size_X, sample_size_Y, x_img[0, :, :], x_img[1, :, :],
                  x_img[2, :, :])
        predicted_label = test_cnn(test_xs)

        #predicted_label = np.array(predicted_label)
        print('predicted_label:', predicted_label)
        print('real label:', test_ys.argmax(1))
        real_label = test_ys.argmax(1)
        # real_label =[2,3]
        result = (predicted_label == real_label)  #判断两个数组的对应元素是否相同
        #result =np.array(result)
        print(result, np.sum(result == True))  #打印对应元素相等的数量