mu_tou_man

导航

 
#  -*-coding:utf8 -*-
# from compiler.ast import flatten
import os
import traceback

from PIL import Image
import numpy as np
from numpy import *

from image.SearchIndexOutBoundException import SearchIndexOutBoundException

# 图片搜索范围,数值越大,搜索越准确,但是耗时也越长
SEARCH_RANGE = 5
# 当前图片中低于该灰度值的像素点要通过搜索附近图片的值进行覆盖,要根据实际温度要求调整该参数
PIXEL_VALUE_THRESHOLD = 14407
# 要根据图像高度调整该参数,lst文件为82*49(Height*Width)
IMAGE_HEIGHT = 82

# 获取input目录下LST中所有原始图像的绝对路径
def get_lst_src_images(input_lst_dir):
    image_abs_paths = []
    for one_image_name in os.listdir(input_lst_dir):
        image_abs_paths.append(input_lst_dir + os.sep + one_image_name)
    return image_abs_paths

def read_one_image(image_path):
    return Image.open(image_path)

    # 将一幅图像的灰度值转化为一行的数组
def get_array_data_from_image(image_path):
    image = read_one_image(image_path)
    return np.array(image).flatten()

    # 将数组的数据保存到图像中
def save_array_data_to_image(image_name, array_data, row_number):
    # 将数组转化为一维矩阵
    one_row_matrix = np.mat(array_data)
    # 一维矩阵矩阵转化row_number行的矩阵
    result_matrix = one_row_matrix.reshape(row_number, -1)
    # 将矩阵数据数据类型转化成16位,并且将数据转化成适合图像的数据
    image = Image.fromarray(result_matrix.astype(np.uint16))
    image.save(image_name)

def save_array_data_to_csv(csv_name, array_data, row_number, data_format):
    # 将数组转化为一维矩阵
    one_row_matrix = np.mat(array_data)
    # 一维矩阵矩阵转化row_number行的矩阵
    result_matrix = one_row_matrix.reshape(row_number, -1)
    # 将矩阵数据保存
    np.savetxt(csv_name, result_matrix, fmt=data_format, delimiter=',')

def correct_lst_images(image_paths, search_range):
    lst_output_dir = 'output' + os.sep + 'LST' + os.sep
    lst_corrected_image_dir = lst_output_dir + 'LST Corrected Image' + os.sep
    lst_corrected_data_dir = lst_output_dir + 'LST Corrected Data' + os.sep
    image_number = len(image_paths)
    for current_image_index in range(image_number):
        print 'processing......please wait,the processing image index=' + str(current_image_index)
        current_image = image_paths[current_image_index]
        current_array_data = get_array_data_from_image(current_image)
        image_name = get_file_name_from_abs_path(current_image)
        result_array_data = []
        for current_array_index in range(len(current_array_data)):
            # 如果当前像素点灰度值满足要求,就不用搜索周围图像了
            if current_array_data[current_array_index] >= PIXEL_VALUE_THRESHOLD:
                result_array_data.append(current_array_data[current_array_index])
            # 如果当前图像的像素点灰度值不满足,先向右搜索,再向左搜索
            else:
                for range_index in range(1, search_range + 1):
                    try:
                        override_image_index = get_override_image_index(current_image_index, range_index, image_number,
                                                'right')
                        override_array_data = get_array_data_from_image(image_paths[override_image_index])
                        if override_array_data[current_array_index] >= PIXEL_VALUE_THRESHOLD:
                            result_array_data.append(override_array_data[current_array_index])
                            break
                        else:
                            override_image_index = get_override_image_index(current_image_index, range_index,
                                                                            image_number, 'left')
                            override_array_data = get_array_data_from_image(image_paths[override_image_index])
                            if override_array_data[current_array_index] >= PIXEL_VALUE_THRESHOLD:
                                result_array_data.append(override_array_data[current_array_index])
                                break
                            else:
                                # 如果找到左边最后一张还没有找到满足条件的灰度值,则该像素值保持不变,对应非关注区域像素值为0的大片黑色区域
                                if range_index == search_range:
                                    # print 'left not find ideal data,current_array_index' + str(current_array_index)
                                    result_array_data.append(current_array_data[current_array_index])
                                    break
                                else:
                                    continue
                    except SearchIndexOutBoundException as e1:
                        if range_index == search_range:
                            # print 'exception not find ideal data,current_array_index=' + str(current_array_index)
                            result_array_data.append(current_array_data[current_array_index])
                            break
                        else:
                            continue
                    except Exception as e2:
                        msg = traceback.format_exc()
                        print (msg)
                        continue
        save_array_data_to_image(lst_corrected_image_dir + image_name, result_array_data, IMAGE_HEIGHT)
        save_array_data_to_csv(lst_corrected_data_dir + image_name + '.csv', result_array_data, IMAGE_HEIGHT, '%d')

def get_file_name_from_abs_path(image_abs_path):
    image_name = os.path.basename(image_abs_path)
    return image_name

def get_override_image_index(index, range_index, image_number, direction):
    if direction == 'right':
        # 超出右边边界,报异常;否则返回搜索图像索引值
        if index + range_index >= image_number:
            raise SearchIndexOutBoundException(
                "can not find image in right direction ! Please check,index=" + str(index) + ",rang_index=" + str(
                    range_index))
        return index + range_index
    elif direction == 'left':
        # 超出左边边界,报异常;否则返回搜索图像索引值
        if index < range_index:
            raise SearchIndexOutBoundException(
                "can not find image in left direction ! Please check,index=" + str(index) + ",rang_index=" + str(
                    range_index))
        return index - range_index

    else:
        raise Exception("direction error! Please check")

def write_src_images_pixel_value_to_csv(images_abs_paths):
    i = 0
    lst_output_dir = 'output' + os.sep + 'LST' + os.sep
    lst_src_data_dir = lst_output_dir + 'LST Src Data' + os.sep
    # lst_temperature_data_dir = lst_dir + 'LST Src Temperature Data' + os.sep
    for oneImage in images_abs_paths:
        image = read_one_image(oneImage)
        src = array(image)
        # temperature_array = src * 0.02 - 273.15
        np.savetxt(lst_src_data_dir + get_file_name_from_abs_path(oneImage) + '.csv', src, fmt='%d', delimiter=',')
        # np.savetxt(lst_temperature_data_dir + images_names[i] + '_temperature.csv', temperature_array, fmt='%d',
        #    delimiter=',')
        # satisfied_array = np.where(temperature_array >= 10)
        # avg = np.average(temperature_array[satisfied_array])
        # print avg
        print "i=" + str(i)
        i = i + 1

def write_src_images_sorted_pixel_value_to_one_csv(images_abs_paths):
    lst_output_dir = 'output' + os.sep + 'LST' + os.sep
    lst_src_data_dir = lst_output_dir + 'LST Src One Row Data' + os.sep
    image_to_data = []
    for oneImage in images_abs_paths:
        filename = get_file_name_from_abs_path(oneImage)
        file_id = filename[13:16]
        image = read_one_image(oneImage)
        src = array(image)
        one_row = src.flatten()
        one_row.sort()
        filter_zero_area_data = one_row[1664:]
        add_file_index_data = list(filter_zero_area_data)
        add_file_index_data.insert(0, int(file_id))
        image_to_data.append(add_file_index_data)
    one_row_matrix = np.mat(image_to_data)
    # 一维矩阵矩阵转化row_number行的矩阵
    result_matrix = one_row_matrix.reshape(150, -1)
    # image_to_data.setdefault(filename, one_row)
    temperature_array = result_matrix * 0.02 - 273.15
    np.savetxt(lst_src_data_dir + 'source pixel value data.csv', result_matrix, fmt='%d', delimiter=',')
    np.savetxt(lst_src_data_dir + 'source temperature data.csv', temperature_array, fmt='%.2f', delimiter=',')
    # np.savetxt(lst_temperature_data_dir + images_names[i] + '_temperature.csv', temperature_array, fmt='%d',
    #    delimiter=',')
    # satisfied_array = np.where(temperature_array >= 10)
    # avg = np.average(temperature_array[satisfied_array])
    # print avg

if __name__ == '__main__':
    current_path = os.getcwd()
    input_lst_dir = current_path + os.sep + 'input' + os.sep + 'LST'
    images_abs_paths = get_lst_src_images(input_lst_dir)
    print images_abs_paths
    #write_src_images_sorted_pixel_value_to_one_csv(images_abs_paths)
    correct_lst_images(images_abs_paths, SEARCH_RANGE)

 

posted on 2022-01-17 19:15  mu_tou_man  阅读(43)  评论(0编辑  收藏  举报