SAR图像变换域处理+HOG特征提取

 写这篇文章的目的是记录自己学习的过程,同时帮助那些一样需要对合成孔径雷达图像进行变换处理的伙伴们。

1.直接HOG特征提取

import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage.feature import hog
from skimage import exposure
from skimage.color import rgb2gray

# Define input folder paths
input_folder_paths = [
    r'D:\dataset\OpenSARship1\Test\Tanker',
    # r'D:\dataset\OpenSARship1\Test\Cargo2',
    # r'D:\dataset\OpenSARship1\Test\Cargo3'
]

# Define target image size
target_size = (256, 256)  # For example, set as 256x256

# Define output folder paths
output_folder_paths = [
    r'D:\dataset\HOG_OpenSARship\Test\Tanker',
    # r'D:\dataset\HOG_OpenSARship\Test\Cargo2',
    # r'D:\dataset\HOG_OpenSARship\Test\Cargo3'
]

# Loop over each input folder path
for input_folder_path, output_folder_path in zip(input_folder_paths, output_folder_paths):
    # Get all image files in the input folder
    image_files = [f for f in os.listdir(input_folder_path) if f.endswith(('.jpg','.png'))]

    # Loop over each image file
    for image_file in image_files:
        # Check if the output image already exists
        result_file = os.path.join(output_folder_path, 'hog_' + image_file)
        if os.path.exists(result_file):
            continue

        # 1. Read the SAR image and resize it
        image = cv2.imread(os.path.join(input_folder_path, image_file))
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        image_resized = cv2.resize(image, target_size)

        # 2. Convert the image to grayscale
        image_gray = rgb2gray(image_resized)

        # 3. Extract HOG features from the grayscale image
        fd, hog_image = hog(image_gray, orientations=6, pixels_per_cell=(18, 18),
                            cells_per_block=(2, 2), visualize=True)

        # 4. Rescale the HOG image intensity for better visualization
        hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 1))

        # 5. Display and save only the HOG feature
        fig, axes = plt.subplots(1, 1, figsize=(6, 6))  # Create only one subplot

        hog_plot = axes.imshow(hog_image_rescaled, cmap='jet')
        axes.axis('off')  # Turn off axis display

        plt.tight_layout()

        # Save the processed image to the output folder path
        plt.savefig(result_file)

        # Close the figure window
        plt.close()

2.STFT+HOG特征提取

  1. 导入库:首先导入必要的Python库,包括os用于文件操作,numpy用于数值操作,matplotlib用于图像可视化,scipy.signal中的stft函数用于短时傅里叶变换,skimage.feature用于HOG特征提取,以及skimage中的其他函数用于图像处理。

  2. 定义输入和输出目录:定义了输入图像目录和输出图像目录。是为了处理不同种类的船只图像。

  3. 定义通用图像大小common_size 变量定义了希望将图像调整为的通用大小(256x256像素)。

  4. 循环处理不同种类的船只图像:通过循环处理不同的输入目录,每个目录对应一种船只类型。

  5. 获取图像文件列表:从输入目录中获取所有的JPEG和PNG文件列表。

  6. 遍历图像文件:循环处理每个图像文件。

  7. 读取SAR图像:使用plt.imread函数读取图像,然后检查图像是否为灰度图像。如果是灰度图像,将其转换为RGB格式。

  8. 调整图像大小:将图像调整为通用大小。

  9. 提取HOG特征:使用skimage.feature.hog函数从图像中提取HOG特征。这些特征用于描述图像的纹理和结构。

  10. 重新缩放HOG图像:通过 skimage.exposure.rescale_intensity 函数重新缩放HOG图像的强度,以获得更好的可视化效果。

  11. 显示和保存HOG特征:创建一个Matplotlib图形,显示HOG特征,并将结果保存到输出目录中。图形的参数已经配置为不显示坐标轴。

  12. 创建输出目录:如果输出目录不存在,代码会自动创建它。

  13. 保存结果:保存处理后的图像到输出目录,并关闭Matplotlib图形。

import os
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import stft
from skimage.feature import hog
from skimage import exposure, transform
from skimage.color import rgb2gray, gray2rgb

# Define the input and output directories
# Define the input and output directories
input_image_dirs = [
    r'D:\dataset\OpenSARship1\Train\Tanker',
    # r'D:\dataset\OpenSARship1\Train\Fishing',
    # r'D:\dataset\OpenSARship1\Train\Tanker'
]

output_image_dirs = [
    # r'D:\dataset\STFT_HOG_OSARship\Train\Cargo',
    # r'D:\dataset\STFT_HOG_OSARship\Train\Fishing',
     r'D:\dataset\HOG_OpenSARship\Train\Tanker'
]

# Define the common size to which you want to resize the images
common_size = (256, 256)  # Adjust the size as needed

# Loop through the input directories
for i, input_image_dir in enumerate(input_image_dirs):
    output_image_dir = output_image_dirs[i]

    # Get a list of all JPEG files in the input directory
    image_files = [f for f in os.listdir(input_image_dir) if f.endswith(('.jpg','.png'))]

    # Loop through the image files
    for image_file in image_files:
        # Check if the output image already exists
        output_file = os.path.join(output_image_dir, 'hog_' + image_file)
        if os.path.exists(output_file):
            continue

        # 1. Read the SAR image
        image = plt.imread(os.path.join(input_image_dir, image_file))

        # Check if the image is grayscale (single-channel)
        if len(image.shape) < 3 or image.shape[2] == 1:
            # Convert grayscale image to RGB
            image = gray2rgb(image)

        # 2. Resize the image to the common size
        output_shape = (common_size[0], common_size[1], image.shape[2])
        image = transform.resize(image, output_shape)

        # # 3. Perform Short-time Fourier Transform on the SAR image
        # f, t, Zxx = stft(image, nperseg=3, noverlap=1)
        #
        # # 4. Select a specific time plane from the STFT result
        # time_plane = np.abs(Zxx[:, :, -1])  # Replace <index> with the desired time plane index

        # 5. Extract HOG features from the selected time plane
        fd, hog_image = hog(image, orientations=6, pixels_per_cell=(18, 18),
                            cells_per_block=(2, 2), visualize=True, channel_axis=-1)

        # 6. Rescale HOG image intensities for better visualization
        hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 1))

        # 7. Display and save the HOG features
        fig, axes = plt.subplots(1, 1, figsize=(6, 6))  # Create a single subplot

        hog_plot = axes.imshow(hog_image_rescaled, cmap='jet')
        axes.axis('off')  # Turn off axis display

        plt.tight_layout()

        # Create the output directory if it doesn't exist
        os.makedirs(output_image_dir, exist_ok=True)

        # Save the result image to the output directory
        result_file = os.path.join(output_image_dir, 'hog_' + image_file)
        plt.savefig(result_file)

        # Close the figure window
        plt.close()




3.CWT+HOG特征提取

import os
import numpy as np
import matplotlib.pyplot as plt
import pywt
from skimage.feature import hog
from skimage import exposure, transform
from skimage.color import gray2rgb
import cv2  # OpenCV is used for CWT visualization

# Define the directories for input and output
input_image_dir = r'D:\HuaweiMoveData\Users\Ldr13\Desktop\SAR\OpenSARship\High speed craft'

# Define the common size to which you want to resize the images
common_size = (256, 256)  # Adjust the size as needed

# Get a list of all JPEG files in the input directory
image_files = [f for f in os.listdir(input_image_dir) if f.endswith('.jpg')]

# Loop through the image files
for image_file in image_files:
    # 1. Read the SAR image
    image = plt.imread(os.path.join(input_image_dir, image_file))  # gray image
    image = gray2rgb(image)

    # 2. Resize the image to the common size
    image = transform.resize(image, common_size)

    # 3. Perform Continuous Wavelet Transform (CWT) on the SAR image
    cwt_result, frequencies = pywt.cwt(image, scales=np.arange(1, 10), wavelet='morl')

    # 4. Select a specific scale from the CWT result (equivalent to time plane in STFT)
    scale_index = 3  # Replace with the desired scale index
    selected_scale = cwt_result[scale_index]

    # 5. Extract HOG features from the selected scale
    fd, hog_image = hog(selected_scale, orientations=6, pixels_per_cell=(18, 18),
                        cells_per_block=(2, 2), visualize=True, channel_axis=-1)

    # 6. Rescale HOG image intensities for better visualization
    hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 1))

    # 7. Display HOG features and save the result
    fig, axes = plt.subplots(1, 1, figsize=(6, 6))

    hog_plot = axes.imshow(hog_image_rescaled, cmap='jet')
    axes.axis('off')
    plt.tight_layout()

    result_file = os.path.join(os.path.join('D:\\HuaweiMoveData\\Users\\Ldr13\\Desktop\\SAR\\feature ext\\result2', 'hog_' + image_file))
    plt.savefig(result_file)
    plt.close()

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Imrea

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值