活动介绍
file-type

HSV值检测程序:C++实现图片HSV分析

版权申诉

RAR文件

2KB | 更新于2025-08-07 | 131 浏览量 | 0 下载量 举报 收藏
download 限时特惠:#14.90
### 标题分析 从标题"Main_HSV检测_HSV_C++_"中,我们可以得知该程序是用C++编写的,主要用于HSV(Hue, Saturation, Value)色彩空间的图像检测。标题表明程序可能是一个主程序或者入口文件,且与HSV检测密切相关。 ### 描述分析 描述中提到的“该程序可以用来检测图片的HSV值”,说明该程序的主要功能是读取图像文件,分析图像并提取出HSV色彩空间中各像素的色彩信息,即色调(Hue)、饱和度(Saturation)和亮度(Value)三个分量的值。HSV色彩空间是一种常用的颜色模型,特别适合进行颜色分割和图像处理,因为它接近人类对颜色的感知方式。 描述还指出“检测到的HSV值可以用于阈值设定”,这说明提取出的HSV值可以进一步用于图像分割和特征提取等后续处理。在图像处理中,阈值设定是一个重要步骤,用于将图像中的特定颜色或者亮度范围的像素分离出来,以便进行特定的图像分析或处理任务。 ### 标签分析 标签"HSV检测"和"HSV C++"进一步强调了该程序的核心功能和使用的编程语言。标签"HSV检测"再次确认了程序的功能性,即专注于色彩分析,而"HSV C++"则指出该程序是使用C++语言开发的,C++作为一种高效的编程语言,在图像处理和计算机视觉领域有着广泛的应用。 ### 压缩包子文件列表分析 从提供的文件名称列表"Main.cpp"来看,这是整个程序的主文件,通常包含了程序的主要逻辑和入口点。在C++中,main函数是程序执行的起点,因此文件名中的"Main"很可能指向这个主函数。在该文件中,开发者应当实现了对图像文件的读取、HSV值的计算和输出等相关功能。 ### 知识点详述 1. **HSV色彩空间**:HSV色彩空间是一种描述颜色的方法,与RGB色彩空间相比,它更接近于人类的视觉感知。色调H代表颜色的种类,饱和度S代表颜色的纯度,而亮度V(也称值)代表颜色的明亮程度。在图像处理中,通过分析图像的HSV值,我们可以更好地处理图像颜色问题。 2. **图像处理中的HSV值提取**:C++程序通常使用图像处理库,如OpenCV,来处理图像数据。在OpenCV中,开发者可以使用相应的函数从图像中提取出HSV值。这通常涉及到将图像从RGB格式转换为HSV格式,并遍历图像的每一个像素点以提取其HSV值。 3. **阈值设定与应用**:在图像处理过程中,阈值设定是根据HSV值将图像分割成前景和背景的常用技术。通过设置不同的HSV阈值,可以提取出图像中的特定颜色范围,这对于目标检测、图像分割和特征提取等任务至关重要。 4. **C++在图像处理中的应用**:C++作为一种高效、性能优越的编程语言,在图像处理领域扮演着重要角色。利用C++编写图像处理程序可以实现复杂算法的快速执行,满足实时处理的要求。OpenCV库是C++进行图像处理和计算机视觉开发的事实标准库之一。 5. **OpenCV库的使用**:在C++中,使用OpenCV库可以大幅简化图像处理任务。开发者可以通过OpenCV提供的接口读取图像文件、转换图像格式、获取像素信息等。这些功能使得C++在处理图像、视频和其他视觉数据方面变得异常强大。 综上所述,这个名为"Main_HSV检测_HSV_C++_"的C++程序,很可能是一个用于图像处理的实用工具,它通过OpenCV等图像处理库来实现对图像HSV色彩值的检测,检测到的HSV值则可以作为阈值设定的依据,用于后续的图像分割和分析。开发者通过编写相应的C++代码,实现图像读取、颜色空间转换、像素遍历及HSV值提取等关键步骤,最终达到自动化图像处理的目的。

相关推荐

filetype

def color_similarity(img1, img2): #0~1: 1表示完全相似 0表示完全不相似 similarity_list = [] # 读取图像并转换为HSV img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2HSV) img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2HSV) # 计算平均HSV值 avg_hsv1 = np.mean(img1, axis=(0,1)) avg_hsv2 = np.mean(img2, axis=(0,1)) # 归一化处理(H:0-180°→0-1, S/V:0-255→0-1) norm_hsv1 = [avg_hsv1[0]/180, avg_hsv1[1]/255, avg_hsv1[2]/255] norm_hsv2 = [avg_hsv2[0]/180, avg_hsv2[1]/255, avg_hsv2[2]/255] # 计算三维空间距离 delta = np.array(norm_hsv1) - np.array(norm_hsv2) distance = np.linalg.norm(delta) similarity = 1 - min(distance, 1.0) #相似度保存 similarity_list.append(similarity) return similarity_list # 关键点连接关系定义(COCO格式) skeleton = [ (0, 1), # 鼻子 → 左眼 (0, 2), # 鼻子 → 右眼 (1, 3), # 左眼 → 左耳 (2, 4), # 右眼 → 右耳 (0, 5), # 鼻子 → 左肩 (0, 6), # 鼻子 → 右肩 (5, 6), # 左肩 → 右肩(躯干上部) (5, 7), # 左肩 → 左肘 (7, 9), # 左肘 → 左手腕 (6, 8), # 右肩 → 右肘 (8, 10), # 右肘 → 右手腕 (5, 11), # 左肩 → 左髋 (6, 12), # 右肩 → 右髋 (11, 12), # 左髋 → 右髋(躯干下部) (11, 13), # 左髋 → 左膝盖 (13, 15), # 左膝盖 → 左脚踝 (12, 14), # 右髋 → 右膝盖 (14, 16) # 右膝盖 → 右脚踝 ] crop_ROI = [] for i, connection in enumerate(skeleton): start_idx, end_idx = connection if (confidences[start_idx] > 0.3 and confidences[end_idx] > 0.3): start_point = tuple(map(int, keypoints[start_idx])) end_point = tuple(map(int, keypoints[end_idx])) if start_point == (0, 0) or end_point == (0, 0): continue if (start_idx==5 and end_idx==6): crop_ROI.append(start_point) crop_ROI.append(end_point) if (start_idx==11 and end_idx==12): crop_ROI.append(end_point) crop_ROI.append(start_point) cv2.line(frame, start_point, end_point, (255, 0, 0), 2) # print('crop_ROI: ', crop_ROI) if len(crop_ROI) < 4: continue ROI = crop_perspective(frame, crop_ROI) # ROI = crop_mask(image, crop_ROI) ROI_resize = cv2.resize(ROI.copy(), (320, 320)) # print(ROI.shape, "ROI_resize.shape: ", ROI_resize.shape, temp_img.shape) si_list = color_similarity(ROI_resize, temp_img) # si_list = compare_color_histograms(ROI_resize, temp_img) # print(si_list) cv2.rectangle(frame, crop_ROI[0], crop_ROI[2], (0, 255, 0), 2) cv2.putText(frame, f"si:{round(float(si_list[0]), 3)}", (crop_ROI[1][0], crop_ROI[1][1]), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 0), 3) cv2.imshow('ROI', ROI_resize) # cv2.waitKey(0)将python代码严格转成c++代码,使用cmake编译

filetype

def color_similarity(img1, img2): #0~1: 1表示完全相似 0表示完全不相似 similarity_list = [] # 读取图像并转换为HSV img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2HSV) img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2HSV) # 计算平均HSV值 avg_hsv1 = np.mean(img1, axis=(0,1)) avg_hsv2 = np.mean(img2, axis=(0,1)) # 归一化处理(H:0-180°→0-1, S/V:0-255→0-1) norm_hsv1 = [avg_hsv1[0]/180, avg_hsv1[1]/255, avg_hsv1[2]/255] norm_hsv2 = [avg_hsv2[0]/180, avg_hsv2[1]/255, avg_hsv2[2]/255] # 计算三维空间距离 delta = np.array(norm_hsv1) - np.array(norm_hsv2) distance = np.linalg.norm(delta) similarity = 1 - min(distance, 1.0) #相似度保存 similarity_list.append(similarity) return similarity_list # 关键点连接关系定义(COCO格式) skeleton = [ (0, 1), # 鼻子 → 左眼 (0, 2), # 鼻子 → 右眼 (1, 3), # 左眼 → 左耳 (2, 4), # 右眼 → 右耳 (0, 5), # 鼻子 → 左肩 (0, 6), # 鼻子 → 右肩 (5, 6), # 左肩 → 右肩(躯干上部) (5, 7), # 左肩 → 左肘 (7, 9), # 左肘 → 左手腕 (6, 8), # 右肩 → 右肘 (8, 10), # 右肘 → 右手腕 (5, 11), # 左肩 → 左髋 (6, 12), # 右肩 → 右髋 (11, 12), # 左髋 → 右髋(躯干下部) (11, 13), # 左髋 → 左膝盖 (13, 15), # 左膝盖 → 左脚踝 (12, 14), # 右髋 → 右膝盖 (14, 16) # 右膝盖 → 右脚踝 ] crop_ROI = [] for i, connection in enumerate(skeleton): start_idx, end_idx = connection if (confidences[start_idx] > 0.3 and confidences[end_idx] > 0.3): start_point = tuple(map(int, keypoints[start_idx])) end_point = tuple(map(int, keypoints[end_idx])) if start_point == (0, 0) or end_point == (0, 0): continue if (start_idx==5 and end_idx==6): crop_ROI.append(start_point) crop_ROI.append(end_point) if (start_idx==11 and end_idx==12): crop_ROI.append(end_point) crop_ROI.append(start_point) cv2.line(frame, start_point, end_point, (255, 0, 0), 2) # print('crop_ROI: ', crop_ROI) if len(crop_ROI) < 4: continue ROI = crop_perspective(frame, crop_ROI) # ROI = crop_mask(image, crop_ROI) ROI_resize = cv2.resize(ROI.copy(), (320, 320)) # print(ROI.shape, "ROI_resize.shape: ", ROI_resize.shape, temp_img.shape) si_list = color_similarity(ROI_resize, temp_img) # si_list = compare_color_histograms(ROI_resize, temp_img) # print(si_list) cv2.rectangle(frame, crop_ROI[0], crop_ROI[2], (0, 255, 0), 2) cv2.putText(frame, f"si:{round(float(si_list[0]), 3)}", (crop_ROI[1][0], crop_ROI[1][1]), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 0), 3) cv2.imshow('ROI', ROI_resize) # cv2.waitKey(0)将python代码严格转成c++代码头文件.h和实现.cpp的方式,使用cmake编译

filetype

int GetConvDetectionPoseResultInt8(int8_t **pBlob, std::vector<int> &qnt_zp, std::vector<float> &qnt_scale, std::vector<float> &DetectiontRects,std::vector<std::map<int, KeyPoint>> &keypoints) { static auto meshgrid = GenerateMeshgrid(); int ret = 0; int gridIndex = -2; float xmin = 0, ymin = 0, xmax = 0, ymax = 0; float cls_val = 0; float cls_max = 0; int cls_index = 0; int quant_zp_cls = 0, quant_zp_reg = 0, quant_zp_msk, quant_zp_seg = 0; float quant_scale_cls = 0, quant_scale_reg = 0, quant_scale_msk = 0, quant_scale_seg = 0; int quant_zp_pose = 0; float quant_scale_pose = 0; int model_class_num = class_num; std::vector<DetectRect> detectRects; for (int index = 0; index < headNum; index++) { int8_t *reg = (int8_t *)pBlob[index * 2 + 0]; int8_t *cls = (int8_t *)pBlob[index * 2 + 1]; int8_t *pose = (int8_t *)pBlob[index + headNum * 2]; quant_zp_reg = qnt_zp[index * 2 + 0]; quant_zp_cls = qnt_zp[index * 2 + 1]; quant_scale_reg = qnt_scale[index * 2 + 0]; quant_scale_cls = qnt_scale[index * 2 + 1]; quant_zp_pose = qnt_zp[index + headNum * 2]; quant_scale_pose = qnt_scale[index + headNum * 2]; for (int h = 0; h < mapSize[index][0]; h++) { for (int w = 0; w < mapSize[index][1]; w++) { gridIndex += 2; for (int cl = 0; cl < model_class_num; cl++) { cls_val = sigmoid( DeQnt2F32(cls[cl * mapSize[index][0] * mapSize[index][1] + h * mapSize[index][1] + w], quant_zp_cls, quant_scale_cls)); if (0 == cl) { cls_max = cls_val; cls_index = cl; } else { if (cls_val > cls_max) { cls_max = cls_val; cls_index = cl; } } } if (cls_max > objectThreshold) { DetectRect temp; xmin = (meshgrid[gridIndex + 0] - DeQnt2F32(reg[0 * mapSize[index][0] * mapSize[index][1] + h * mapSize[index][1] + w], quant_zp_reg, quant_scale_reg)) * strides[index]; ymin = (meshgrid[gridIndex + 1] - DeQnt2F32(reg[1 * mapSize[index][0] * mapSize[index][1] + h * mapSize[index][1] + w], quant_zp_reg, quant_scale_reg)) * strides[index]; xmax = (meshgrid[gridIndex + 0] + DeQnt2F32(reg[2 * mapSize[index][0] * mapSize[index][1] + h * mapSize[index][1] + w], quant_zp_reg, quant_scale_reg)) * strides[index]; ymax = (meshgrid[gridIndex + 1] + DeQnt2F32(reg[3 * mapSize[index][0] * mapSize[index][1] + h * mapSize[index][1] + w], quant_zp_reg, quant_scale_reg)) * strides[index]; xmin = xmin > 0 ? xmin : 0; ymin = ymin > 0 ? ymin : 0; xmax = xmax < input_w ? xmax : input_w; ymax = ymax < input_h ? ymax : input_h; if (xmin >= 0 && ymin >= 0 && xmax <= input_w && ymax <= input_h) { temp.xmin = xmin / input_w; temp.ymin = ymin / input_h; temp.xmax = xmax / input_w; temp.ymax = ymax / input_h; temp.classId = cls_index; temp.score = cls_max; for (int kc = 0; kc < keypoint_num; kc++) { KeyPoint kp; kp.x = (DeQnt2F32(pose[(kc * 3 + 0) * mapSize[index][0] * mapSize[index][1] + h * mapSize[index][1] + w], quant_zp_pose, quant_scale_pose) * 2 + (meshgrid[gridIndex + 0] - 0.5)) * strides[index] / input_w; kp.y = (DeQnt2F32(pose[(kc * 3 + 1) * mapSize[index][0] * mapSize[index][1] + h * mapSize[index][1] + w], quant_zp_pose, quant_scale_pose) * 2 + (meshgrid[gridIndex + 1] - 0.5)) * strides[index] / input_h; kp.score = sigmoid(DeQnt2F32(pose[(kc * 3 + 2) * mapSize[index][0] * mapSize[index][1] + h * mapSize[index][1] + w], quant_zp_pose, quant_scale_pose)); kp.id = kc; temp.keyPoints.push_back(kp); } detectRects.push_back(temp); } } } } } std::sort(detectRects.begin(), detectRects.end(), [](DetectRect &Rect1, DetectRect &Rect2) -> bool { return (Rect1.score > Rect2.score); }); NN_LOG_DEBUG("NMS Before num :%ld", detectRects.size()); for (int i = 0; i < detectRects.size(); ++i) { float xmin1 = detectRects[i].xmin; float ymin1 = detectRects[i].ymin; float xmax1 = detectRects[i].xmax; float ymax1 = detectRects[i].ymax; int classId = detectRects[i].classId; float score = detectRects[i].score; if (classId != -1) { // 将检测结果按照classId、score、xmin1、ymin1、xmax1、ymax1 的格式存放在vector<float>中 DetectiontRects.push_back(float(classId)); DetectiontRects.push_back(float(score)); DetectiontRects.push_back(float(xmin1)); DetectiontRects.push_back(float(ymin1)); DetectiontRects.push_back(float(xmax1)); DetectiontRects.push_back(float(ymax1)); std::map<int, KeyPoint> kps; for (int kn = 0; kn < keypoint_num; kn++) { kps.insert({kn, detectRects[i].keyPoints[kn]}); } keypoints.push_back(kps); for (int j = i + 1; j < detectRects.size(); ++j) { float xmin2 = detectRects[j].xmin; float ymin2 = detectRects[j].ymin; float xmax2 = detectRects[j].xmax; float ymax2 = detectRects[j].ymax; float iou = IOU(xmin1, ymin1, xmax1, ymax1, xmin2, ymin2, xmax2, ymax2); if (iou > nmsThreshold) { detectRects[j].classId = -1; } } } } return ret; } 将下面python代码严格转成c++代码,放在上面的c++代码中def color_similarity(img1, img2): #0~1: 1表示完全相似 0表示完全不相似 similarity_list = [] # 读取图像并转换为HSV img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2HSV) img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2HSV) # 计算平均HSV值 avg_hsv1 = np.mean(img1, axis=(0,1)) avg_hsv2 = np.mean(img2, axis=(0,1)) # 归一化处理(H:0-180°→0-1, S/V:0-255→0-1) norm_hsv1 = [avg_hsv1[0]/180, avg_hsv1[1]/255, avg_hsv1[2]/255] norm_hsv2 = [avg_hsv2[0]/180, avg_hsv2[1]/255, avg_hsv2[2]/255] # 计算三维空间距离 delta = np.array(norm_hsv1) - np.array(norm_hsv2) distance = np.linalg.norm(delta) similarity = 1 - min(distance, 1.0) #相似度保存 similarity_list.append(similarity) return similarity_list # 关键点连接关系定义(COCO格式) skeleton = [ (0, 1), # 鼻子 → 左眼 (0, 2), # 鼻子 → 右眼 (1, 3), # 左眼 → 左耳 (2, 4), # 右眼 → 右耳 (0, 5), # 鼻子 → 左肩 (0, 6), # 鼻子 → 右肩 (5, 6), # 左肩 → 右肩(躯干上部) (5, 7), # 左肩 → 左肘 (7, 9), # 左肘 → 左手腕 (6, 8), # 右肩 → 右肘 (8, 10), # 右肘 → 右手腕 (5, 11), # 左肩 → 左髋 (6, 12), # 右肩 → 右髋 (11, 12), # 左髋 → 右髋(躯干下部) (11, 13), # 左髋 → 左膝盖 (13, 15), # 左膝盖 → 左脚踝 (12, 14), # 右髋 → 右膝盖 (14, 16) # 右膝盖 → 右脚踝 ] crop_ROI = [] for i, connection in enumerate(skeleton): start_idx, end_idx = connection if (confidences[start_idx] > 0.3 and confidences[end_idx] > 0.3): start_point = tuple(map(int, keypoints[start_idx])) end_point = tuple(map(int, keypoints[end_idx])) if start_point == (0, 0) or end_point == (0, 0): continue if (start_idx==5 and end_idx==6): crop_ROI.append(start_point) crop_ROI.append(end_point) if (start_idx==11 and end_idx==12): crop_ROI.append(end_point) crop_ROI.append(start_point) cv2.line(frame, start_point, end_point, (255, 0, 0), 2) # print('crop_ROI: ', crop_ROI) if len(crop_ROI) < 4: continue ROI = crop_perspective(frame, crop_ROI) # ROI = crop_mask(image, crop_ROI) ROI_resize = cv2.resize(ROI.copy(), (320, 320)) # print(ROI.shape, "ROI_resize.shape: ", ROI_resize.shape, temp_img.shape) si_list = color_similarity(ROI_resize, temp_img) # si_list = compare_color_histograms(ROI_resize, temp_img) # print(si_list) cv2.rectangle(frame, crop_ROI[0], crop_ROI[2], (0, 255, 0), 2) cv2.putText(frame, f"si:{round(float(si_list[0]), 3)}", (crop_ROI[1][0], crop_ROI[1][1]), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 0), 3) cv2.imshow('ROI', ROI_resize) # cv2.waitKey(0)使用cmake编译

filetype
filetype
内容概要:本文探讨了在MATLAB/SimuLink环境中进行三相STATCOM(静态同步补偿器)无功补偿的技术方法及其仿真过程。首先介绍了STATCOM作为无功功率补偿装置的工作原理,即通过调节交流电压的幅值和相位来实现对无功功率的有效管理。接着详细描述了在MATLAB/SimuLink平台下构建三相STATCOM仿真模型的具体步骤,包括创建新模型、添加电源和负载、搭建主电路、加入控制模块以及完成整个电路的连接。然后阐述了如何通过对STATCOM输出电压和电流的精确调控达到无功补偿的目的,并展示了具体的仿真结果分析方法,如读取仿真数据、提取关键参数、绘制无功功率变化曲线等。最后指出,这种技术可以显著提升电力系统的稳定性与电能质量,展望了STATCOM在未来的发展潜力。 适合人群:电气工程专业学生、从事电力系统相关工作的技术人员、希望深入了解无功补偿技术的研究人员。 使用场景及目标:适用于想要掌握MATLAB/SimuLink软件操作技能的人群,特别是那些专注于电力电子领域的从业者;旨在帮助他们学会建立复杂的电力系统仿真模型,以便更好地理解STATCOM的工作机制,进而优化实际项目中的无功补偿方案。 其他说明:文中提供的实例代码可以帮助读者直观地了解如何从零开始构建一个完整的三相STATCOM仿真环境,并通过图形化的方式展示无功补偿的效果,便于进一步的学习与研究。
食肉库玛
  • 粉丝: 79
上传资源 快速赚钱