|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码:
from tifffile import imread, imwrite
from skimage import filters, feature, color, morphology
import matplotlib.pyplot as plt
import numpy as np
from scipy import ndimage
import cv2
from skimage.filters import threshold_otsu
# 1. 分割遥感影像为多块图幅
def split_image(image, num_rows, num_cols):
height, width = image.shape[:2]
row_height = height // num_rows
col_width = width // num_cols
images = []
for r in range(num_rows):
for c in range(num_cols):
start_row = r * row_height
end_row = start_row + row_height
start_col = c * col_width
end_col = start_col + col_width
sub_image = image[start_row:end_row, start_col:end_col]
images.append(sub_image)
return images
#2. 分别对每块图幅转为灰度图像,并应用直方图均衡化
def convert_to_grayscale(images):
grayscale_images = []
for image in images:
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
enhanced_image = cv2.equalizeHist(gray_image) # 直方图均衡化增强对比度
grayscale_images.append(enhanced_image)
return grayscale_images
#3.检测目标地物
def edge_detection(images):
edges = []
for image in images:
# 使用新的边缘检测方法
edges_image = filters.roberts(image)
# 计算全局阈值
threshold = threshold_otsu(edges_image)
# 进行二值化处理
binary = edges_image > threshold
# 将二值化的图像转换为灰度图像
gray_image = color.rgb2gray(binary)
edges.append(gray_image)
return edges
# 4. 消除噪声干扰
def denoise(images):
denoised_images = []
for image in images:
# 使用其他滤波方法,这里以高斯滤波为例
denoised_image = cv2.GaussianBlur(image, (5, 5), 0)
denoised_images.append(denoised_image)
return denoised_images
# 5. 将各个图幅合并为一个图幅
def merge_images(images, num_rows, num_cols):
row_height, col_width = images[0].shape[:2]
merged_image = np.zeros((row_height * num_rows, col_width * num_cols), dtype=np.uint8)
i = 0
for r in range(num_rows):
for c in range(num_cols):
start_row = r * row_height
end_row = start_row + row_height
start_col = c * col_width
end_col = start_col + col_width
merged_image[start_row:end_row, start_col:end_col] = images[i]
i += 1
return merged_image
# 6. 导出结果
def export_result(image, filename):
cv2.imwrite(filename, image)
# 加载遥感影像
image = imread(r"F:\duneline\dune\dune.tif")
# 1. 分割遥感影像为多块图幅
num_rows = 7
num_cols = 11
sub_images = split_image(image, num_rows, num_cols)
# 2. 分别对每块图幅转为灰度图像
gray_images = convert_to_grayscale(sub_images)
# 3. 使用其他边缘检测方法代替Canny算法
edges = edge_detection(gray_images)
# 4. 消除噪声干扰
denoised_edges = denoise(edges)
# 5. 将各个图幅合并为一个图幅
merged_image = merge_images(denoised_edges, num_rows, num_cols)
# 6. 导出结果
export_result(merged_image, 'path_to_output2.jpg')
报错信息:
Traceback (most recent call last):
File "F:/duneline/sobel.py", line 145, in <module>
edges = edge_detection(gray_images)
File "F:/duneline/sobel.py", line 106, in edge_detection
gray_image = color.rgb2gray(binary)
File "C:\安装软件\Python\lib\site-packages\skimage\_shared\utils.py", line 394, in fixed_func
return func(*args, **kwargs)
File "C:\安装软件\Python\lib\site-packages\skimage\color\colorconv.py", line 875, in rgb2gray
rgb = _prepare_colorarray(rgb)
File "C:\安装软件\Python\lib\site-packages\skimage\color\colorconv.py", line 140, in _prepare_colorarray
raise ValueError(msg)
ValueError: the input array must have size 3 along `channel_axis`, got (459, 230) |
|