python3 glasswindow.py
[ WARN:0@1.737] global loadsave.cpp:275 findDecoder imread_('window_scene.jpg'): can't open/read file: check file path/integrity
[ WARN:0@1.737] global loadsave.cpp:275 findDecoder imread_('depth_map.png'): can't open/read file: check file path/integrity
Traceback (most recent call last):
File "/home/realsense-d455-test/glasswindow.py", line 84, in <module>
glass_mask, frame_lines, result_img = detect_glass_and_frame(rgb_img, depth_map)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/realsense-d455-test/glasswindow.py", line 41, in detect_glass_and_frame
gray = cv2.cvtColor(rgb_img, cv2.COLOR_BGR2GRAY)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cv2.error: OpenCV(4.12.0) /home/conda/feedstock_root/build_artifacts/libopencv_1753287227905/work/modules/imgproc/src/color.cpp:199: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'
请根据上面报错提示修改下面的代码,并提供一份完整的代码
import cv2
import numpy as np
import matplotlib.pyplot as plt
def detect_glass_and_frame(rgb_img, depth_map):
"""
检测玻璃及窗框的核心函数
参数:
rgb_img: RGB彩色图像 (H, W, 3)
depth_map: 深度图 (H, W),无效值通常为0
返回:
glass_mask: 玻璃区域掩膜
frame_lines: 检测到的窗框线段
result_img: 可视化结果图像
建议:
在强光照环境下,为深度相机添加红外滤光片
对窗框线段进行聚类和延长处理,形成完整框架
使用RANSAC算法拟合更精确的直线方程
"""
# ===== 1. 深度图预处理 =====
# 创建玻璃候选区域掩膜(深度值为0的区域)
glass_mask = np.where(depth_map == 0, 255, 0).astype(np.uint8)
# 形态学操作优化掩膜
kernel = np.ones((5, 5), np.uint8)
glass_mask = cv2.morphologyEx(glass_mask, cv2.MORPH_CLOSE, kernel) # 闭运算填充空洞
glass_mask = cv2.dilate(glass_mask, kernel, iterations=2) # 扩大边缘区域
# 中值滤波降噪
glass_mask = cv2.medianBlur(glass_mask, 5)
# ===== 2. 玻璃区域分割 =====
# 查找轮廓并过滤小区域
contours, _ = cv2.findContours(glass_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
min_area = 1000 # 最小面积阈值
glass_roi = np.zeros_like(glass_mask)
for cnt in contours:
area = cv2.contourArea(cnt)
if area > min_area:
cv2.drawContours(glass_roi, [cnt], -1, 255, -1)
# ===== 3. 窗框检测 =====
# RGB图像预处理
gray = cv2.cvtColor(rgb_img, cv2.COLOR_BGR2GRAY)
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))
enhanced = clahe.apply(gray)
# 边缘检测(在玻璃区域边界)
edges = cv2.Canny(enhanced, 50, 150)
# 霍夫变换检测直线
lines = cv2.HoughLinesP(edges, rho=1, theta=np.pi/180, threshold=50,
minLineLength=100, maxLineGap=10)
# 几何约束筛选窗框线
frame_lines = []
if lines is not None:
for line in lines:
x1, y1, x2, y2 = line[0]
# 筛选水平/垂直线(角度容差±10度)
if abs(x1 - x2) < 10 or abs(y1 - y2) < 10:
frame_lines.append(line[0])
# ===== 4. 结果可视化 =====
result_img = rgb_img.copy()
# 绘制玻璃区域(半透明红色)
glass_color = np.zeros_like(rgb_img)
glass_color[glass_roi == 255] = [0, 0, 255] # 红色
cv2.addWeighted(glass_color, 0.3, result_img, 1, 0, result_img)
# 绘制窗框线
for line in frame_lines:
x1, y1, x2, y2 = line
cv2.line(result_img, (x1, y1), (x2, y2), (0, 255, 0), 2)
return glass_roi, frame_lines, result_img
# ===== 主程序 =====
if __name__ == "__main__":
# 模拟数据加载(实际使用时应替换为真实相机数据)
# 注意:实际应用中需使用pyrealsense2等库获取深度相机数据
rgb_img = cv2.imread('window_scene.jpg') # 替换为实际RGB图像路径
depth_map = cv2.imread('depth_map.png', cv2.IMREAD_ANYDEPTH) # 替换为实际深度图路径
# 执行检测
glass_mask, frame_lines, result_img = detect_glass_and_frame(rgb_img, depth_map)
# 显示结果
plt.figure(figsize=(15, 10))
plt.subplot(221)
plt.title("RGB Image")
plt.imshow(cv2.cvtColor(rgb_img, cv2.COLOR_BGR2RGB))
plt.subplot(222)
plt.title("Depth Map")
plt.imshow(depth_map, cmap='jet')
plt.colorbar()
plt.subplot(223)
plt.title("Glass Detection")
plt.imshow(glass_mask, cmap='gray')
plt.subplot(224)
plt.title("Detection Result")
plt.imshow(cv2.cvtColor(result_img, cv2.COLOR_BGR2RGB))
plt.tight_layout()
plt.show()
最新发布