首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >A cpp implementation the weighted average threshold as the criterion for opt

A cpp implementation the weighted average threshold as the criterion for opt

原创
作者头像
Swing Dunn
发布2025-10-22 11:19:08
发布2025-10-22 11:19:08
870
举报
文章被收录于专栏:Some studies in imgsSome studies in imgs

By using OpenCV 3 to implement the process described earlier, the results obtained in the process are similar.

Source code:

代码语言:txt
复制
				//原图二值图
				cv::Mat binary;
				//target_rect  通过搜索得到的填涂区域
				binary = ori_binary(target_rect);

				vectr<vector<Point>> contours;
				vector<Vec4i> hierarchy;
				findContours(binary, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
				for (size_t i = 0; i < contours.size(); i++) {
					double area = contourArea(contours[i]);
					if (area < 4) {
						drawContours(binary, contours, i, Scalar(0), -1); 
					}
				}

				//目标区域内 统计黑白像素各自的数量
				int blackPixelCount = 0;
				for (int i = 0; i < binary.rows; i++) {
					for (int j = 0; j < binary.cols; j++) {
						if (binary.at<uchar>(i, j) == 0) {
							blackPixelCount++;
						}
					}
				}

				int whitePixelCount = 0;
				for (int i = 0; i < binary.rows; i++) {
					for (int j = 0; j < binary.cols; j++) {
						if (binary.at<uchar>(i, j) == 255) {
							whitePixelCount++;
						}
					}
				}

          //计算黑白像素占比
				int a_target_area = binary.rows * binary.cols;
				double rf= blackPixelCount / (double)a_target_area; 
				double rb = whitePixelCount / (double)a_target_area;   
				int T= rf_bg_rate * 255;   
				int A= rb_bg_rate * 255;   

				Mat dst1(target_area.size(), target_area.type(), gray_f_value);
				dst1.copyTo(gray(Rect(target_rect.x, target_rect.y, dst1.cols, dst1.rows)));
				show_img(dst1);

				Mat dst2(target_area.size(), target_area.type(), gray_b_value);
				dst2.copyTo(gray(Rect(target_rect.x, target_rect.y, dst2.cols, dst2.rows)));
				show_img(dst2);

				Mat binary_img;
				cv::cvtColor(binary, binary_img, COLOR_GRAY2BGR);
				binary_img.copyTo(orignal_img(Rect(target_rect.x, target_rect.y, binary_img.cols, binary_img.rows)));

				std::string str_rate = std::to_string(rb);
				if (str_rate.length() > 5)
				str_rate = str_rate.substr(0, 5);
				cv::putText(orignal_img, str_rate, cv::Point(target_rect.x, target_rect.y), cv::FONT_HERSHEY_SIMPLEX, 0.4, Scalar(0, 0, 255), 1);
				show_img(orignal_img);

				double sum_gwm_value = 0.0;
				double sum_gwm_mask = 0.0;
				for (int i = 0; i < target_area.rows; i++) {  
					for (int j = 0; j < target_area.cols; j++) {
						int pixelValue = target_area.at<uchar>(i, j);
						double mask_rate = 0;
						if (pixelValue >= T)
							mask_rate = 0;
						else if (pixelValue >= A)
							mask_rate = 1 - (pixelValue - A) / (double)(T- A);
						else
							mask_rate = 1;
						sum_gwm_value += pixelValue * mask_rate;
						sum_gwm_mask += mask_rate;
					}
				}        

				int gmw_pixel_value = sum_gwm_value / sum_gwm_mask;

				std::string a_pixel_value = std::to_string(gmw_pixel_value);
				cv::putText(orignal_img, a_pixel_value, cv::Point(target_rect.x, target_rect.y), cv::FONT_HERSHEY_SIMPLEX, 0.4, Scalar(0, 0, 255), 1);
				
				int gwmPixelCount = 0;
				for (int i = 0; i < target_area.rows; i++) {
					for (int j = 0; j < target_area.cols; j++) {
						if (target_area.at<uchar>(i, j) < gmw_pixel_value) {
							gwmPixelCount++;
						}
					}
				}  
				double gmw_rate = gwmPixelCount / (double)a_target_area;
				int gmw_value = gmw_rate * 255;

				std::string a_gmw_value = std::to_string(gmw_value);
				if (a_gmw_value.length() > 4)
						a_gmw_value = a_gmw_value.substr(0, 4);
				cv::putText(orignal_img, a_gmw_value, cv::Point(target_rect.x, target_rect.y), cv::FONT_HERSHEY_SIMPLEX, 0.4, Scalar(0, 0, 255), 1);

				Mat dst3(target_area.size(), target_area.type(), gmw_pixel_value);
				show_img(dst3);

				Mat dst4(target_area.size(), target_area.type(), gmw_value);
				show_img(dst4);

				dst4.copyTo(gray(Rect(target_rect.x, target_rect.y, dst4.cols, dst4.rows)));
  

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Source code:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档