OpenCV3.0 + VS2013出现“ACCESS_MASK不明确”错误

VS2013+OpenCV3.0编译问题
本文介绍了在使用VS2013+OpenCV3.0进行编译时遇到的一个常见错误及其解决方案。错误原因是由于头文件中的命名空间冲突导致,特别是与Windows.h中的ACCESS_MASK定义冲突。文章给出了具体的解决步骤。

平台环境

我使用的是VS2013 + OpenCV3.0,在编译程序时,提示出现如下错误:

什么原因导致的?

最后经过查找资料发现,是因为我项目中的其中一个头文件(.h)中使用了using namspace cv空间命名,但是我在源文件中又引用了系统头文件#include<windows.h>,这样就会导致了冲突。原因是他们俩中都有ACCESS_MASK定义,所以就会导致该变量不明确,就会报错。

解决办法

最方便的解决办法可能就是,将所有的头文件(.h)中的using namespace cv都去掉,然后将需要用到的地方用cv::代替。然后将using namespace cv放入到cpp文件中,在cpp文件中不会导致冲突。所以就只会在.h文件中麻烦一点。

1、在main函数中调用d455深度相机,拍摄RGB图片和深度图片,打开窗口,窗框里面有3个小窗口,依次显示RGB图片、深度图片、对深度图处理后的深度图像,当用户点击空格则保存图片,点击q键则关闭程序; 2、在强光照环境下,为深度相机添加红外滤光片 3、对窗框线段进行聚类和延长处理,形成完整框架 4、使用RANSAC算法拟合更精确的直线方程 请按照上面要求修改下面程序代码,并提供一份完整的程序代码 import cv2 import numpy as np import matplotlib.pyplot as plt import os import sys def safe_imread(file_path, flags=cv2.IMREAD_COLOR): """ 安全读取图像文件,解决文件路径错误和中文路径问题 参数: file_path: 图像文件路径 flags: OpenCV读取标志 (cv2.IMREAD_COLOR, cv2.IMREAD_GRAYSCALE, cv2.IMREAD_UNCHANGED) 返回: 图像数据 (numpy数组) 或 None """ # 检查文件是否存在 if not os.path.exists(file_path): print(f"[错误] 文件存在: {file_path}") return None # 检查文件是否可读 if not os.access(file_path, os.R_OK): print(f"[错误] 文件可读: {file_path}") return None # 使用cv2.imdecode解决中文路径问题 [^1] try: with open(file_path, &#39;rb&#39;) as f: img_bytes = np.frombuffer(f.read(), dtype=np.uint8) img = cv2.imdecode(img_bytes, flags) if img is None: print(f"[错误] 无法解码图像: {file_path}") return img except Exception as e: print(f"[异常] 读取图像时出错: {str(e)}") return None def validate_image(img, name): """ 验证图像是否有效 参数: img: 图像数据 name: 图像名称 (用于错误信息) 返回: bool: 图像是否有效 """ if img is None: print(f"[错误] {name} 图像为空") return False if img.size == 0: print(f"[错误] {name} 图像尺寸为0") return False if len(img.shape) < 2: print(f"[错误] {name} 图像维度足") return False return True def safe_cvtColor(img, conversion_code): """ 安全的颜色空间转换函数 参数: img: 输入图像 conversion_code: OpenCV颜色转换代码 返回: 转换后的图像 """ # 验证输入图像 if not validate_image(img, "颜色转换输入"): return None # 处理特殊情况:灰度图转RGB if conversion_code in [cv2.COLOR_BGR2GRAY, cv2.COLOR_RGB2GRAY]: if len(img.shape) == 2: # 已经是灰度图 return img.copy() elif img.shape[2] == 1: # 单通道彩色图 return img[:, :, 0].copy() # 处理特殊情况:RGB转灰度 if conversion_code in [cv2.COLOR_GRAY2BGR, cv2.COLOR_GRAY2RGB]: if len(img.shape) == 3: # 已经是彩色图 return img.copy() elif len(img.shape) == 2: # 灰度图 return cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) # 尝试标准转换 try: return cv2.cvtColor(img, conversion_code) except cv2.error as e: print(f"[错误] 颜色转换失败: {str(e)}") return None def detect_glass_and_frame(rgb_img, depth_map): """ 检测玻璃及窗框的核心函数(包含错误处理) 参数: rgb_img: RGB彩色图像 depth_map: 深度图 返回: glass_mask: 玻璃区域掩膜 frame_lines: 检测到的窗框线段 result_img: 可视化结果图像 """ # 验证输入图像 if not validate_image(rgb_img, "RGB图像"): return None, None, None if not validate_image(depth_map, "深度图"): return None, None, None # 确保深度图是单通道 if len(depth_map.shape) > 2: depth_map = depth_map[:, :, 0] # 1. 深度图预处理 try: # 创建玻璃候选区域掩膜(深度值为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) except Exception as e: print(f"[错误] 深度图预处理失败: {str(e)}") return None, None, None # 2. 玻璃区域分割 try: # 查找轮廓并过滤小区域 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) except Exception as e: print(f"[错误] 玻璃区域分割失败: {str(e)}") return None, None, None # 3. 窗框检测 try: # RGB图像预处理 - 使用安全转换 gray = safe_cvtColor(rgb_img, cv2.COLOR_BGR2GRAY) if gray is None: print("[警告] 使用备选灰度转换方法") gray = cv2.cvtColor(rgb_img, cv2.COLOR_BGR2GRAY) if len(rgb_img.shape) == 3 else rgb_img # 增强对比度 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] if abs(x1 - x2) < 10 or abs(y1 - y2) < 10: # 水平/垂直线 frame_lines.append(line[0]) except Exception as e: print(f"[错误] 窗框检测失败: {str(e)}") frame_lines = [] # 4. 结果可视化 try: result_img = rgb_img.copy() # 确保结果图像是三通道 if len(result_img.shape) == 2: result_img = cv2.cvtColor(result_img, cv2.COLOR_GRAY2BGR) # 绘制玻璃区域(半透明红色) glass_color = np.zeros_like(result_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) except Exception as e: print(f"[错误] 结果可视化失败: {str(e)}") result_img = rgb_img.copy() # 返回原始图像作为备选 return glass_roi, frame_lines, result_img def main(): """主函数,包含完整的错误处理""" print("玻璃窗检测程序启动") # 获取当前脚本所在目录 script_dir = os.path.dirname(os.path.abspath(__file__)) if &#39;__file__&#39; in globals() else os.getcwd() print(f"脚本目录: {script_dir}") # 构建图像路径 rgb_path = os.path.join(script_dir, &#39;window_scene.jpg&#39;) depth_path = os.path.join(script_dir, &#39;depth_map.png&#39;) print(f"RGB图像路径: {rgb_path}") print(f"深度图路径: {depth_path}") # 安全读取图像 rgb_img = safe_imread(rgb_path, cv2.IMREAD_COLOR) depth_map = safe_imread(depth_path, cv2.IMREAD_ANYDEPTH) # 检查图像读取结果 if not validate_image(rgb_img, "RGB图像") or not validate_image(depth_map, "深度图"): print("程序因图像读取失败而终止") return # 执行检测 try: glass_mask, frame_lines, result_img = detect_glass_and_frame(rgb_img, depth_map) except Exception as e: print(f"[严重错误] 检测过程中发生异常: {str(e)}") return # 显示结果 plt.figure(figsize=(15, 10)) # RGB图像 plt.subplot(221) plt.title("RGB Image") if len(rgb_img.shape) == 2: plt.imshow(rgb_img, cmap=&#39;gray&#39;) else: plt.imshow(cv2.cvtColor(rgb_img, cv2.COLOR_BGR2RGB)) # 深度图 plt.subplot(222) plt.title("Depth Map") plt.imshow(depth_map, cmap=&#39;jet&#39;) plt.colorbar() # 玻璃检测结果 plt.subplot(223) plt.title("Glass Detection") if glass_mask is not None: plt.imshow(glass_mask, cmap=&#39;gray&#39;) else: plt.text(0.5, 0.5, "玻璃检测失败", ha=&#39;center&#39;, va=&#39;center&#39;) plt.axis(&#39;off&#39;) # 最终结果 plt.subplot(224) plt.title("Detection Result") if result_img is not None: if len(result_img.shape) == 2: plt.imshow(result_img, cmap=&#39;gray&#39;) else: plt.imshow(cv2.cvtColor(result_img, cv2.COLOR_BGR2RGB)) else: plt.text(0.5, 0.5, "结果可视化失败", ha=&#39;center&#39;, va=&#39;center&#39;) plt.axis(&#39;off&#39;) plt.tight_layout() plt.savefig(&#39;detection_results.png&#39;) print("结果已保存为 detection_results.png") plt.show() if __name__ == "__main__": main() print("程序执行完成")
07-31
python3 glasswindow.py [ WARN:0@1.737] global loadsave.cpp:275 findDecoder imread_(&#39;window_scene.jpg&#39;): can&#39;t open/read file: check file path/integrity [ WARN:0@1.737] global loadsave.cpp:275 findDecoder imread_(&#39;depth_map.png&#39;): can&#39;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 &#39;cvtColor&#39; 请根据上面报错提示修改下面的代码,并提供一份完整的代码 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(&#39;window_scene.jpg&#39;) # 替换为实际RGB图像路径 depth_map = cv2.imread(&#39;depth_map.png&#39;, 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=&#39;jet&#39;) plt.colorbar() plt.subplot(223) plt.title("Glass Detection") plt.imshow(glass_mask, cmap=&#39;gray&#39;) plt.subplot(224) plt.title("Detection Result") plt.imshow(cv2.cvtColor(result_img, cv2.COLOR_BGR2RGB)) plt.tight_layout() plt.show()
07-31
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值