白色像素占比

#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
double sum(Mat src)
{
	double counterw = 0;
	double counterz = 0;
	Mat_<uchar>::iterator it = src.begin<uchar>();
	Mat_<uchar>::iterator itend = src.end<uchar>();
	for (; it != itend; it++)
	{
		if ((*it) > 0)
			counterw += 1;
		if ((*it) == 0)
			counterz += 1;
	}
	double a = counterw * 1.0 / (counterz + counterw) * 100;
	return a;
}
int main()
{
	Mat huabu(1000, 1000, CV_8UC3, Scalar(0, 0, 0));
	line(huabu, Point(5, 10), Point(5, 950), Scalar(255, 255, 255), 2, LINE_AA);
	line(huabu, Point(5, 10), Point(2,15), Scalar(255, 255, 255), 2, LINE_AA);
	line(huabu, Point(5, 10), Point(8, 15), Scalar(255, 255, 255), 2, LINE_AA);
	line(huabu, Point(5, 950), Point(950, 950), Scalar(255, 255, 255), 2, LINE_AA);
	line(huabu, Point(950, 950), Point(945, 945), Scalar(255, 255, 255), 2, LINE_AA);
	line(huabu, Point(950, 950), Point(945, 955), Scalar(255, 255, 255), 2, LINE_AA);
	VideoCapture capture("34.avi");
	int i = 15;
	float last = 0;
	while (1)
	{
		Mat s1;
		capture >> s1;
		namedWindow("原视频", WINDOW_NORMAL);
		imshow("原视频", s1);
		Mat s3 = s1(Rect(Point(0, 80), Point(180,270)));
		if (s3.empty())
		{
			cout << "n=vedio is over";
			return 0;
		}
		Mat s2;
		cvtColor(s3, s2, COLOR_BGR2GRAY);


		threshold(s2, s2, 253, 255, THRESH_BINARY);

		namedWindow("阈值1", WINDOW_NORMAL);
		imshow("阈值1", s2);
		double a = sum(s2);
		//double *p = &a;
		//vector<Point>g;
		cout << "图片s1白色占比:" << a << endl;
		//---------
		int h = 100;
		int w = 100;
		line(huabu, Point(i - 10, 950 - last * 10), Point(i, 950 - a * 10), Scalar(0, 0, 255), 2, LINE_AA);
		i += 10;
		last = a;
		namedWindow("画布", WINDOW_NORMAL);
		imshow("画布", huabu);
		waitKey(500);
	}
	return 0;
}
import cv2 import numpy as np def compare_white_areas(image_path, output_scale=0.5): """处理图片:区分蓝白色,分割并比较左右区域白色比,按比例缩小输出 参数: image_path: 图片路径 output_scale: 输出图片缩放比例(0-1) """ # 读取图片 img = cv2.imread(image_path) if img is None: print("错误:无法读取图片") return # 转换到HSV颜色空间 hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # 定义白色和蓝色的HSV范围[^1] lower_white = np.array([0, 0, 200]) upper_white = np.array([180, 30, 255]) lower_blue = np.array([90, 50, 50]) upper_blue = np.array([130, 255, 255]) # 创建掩膜 white_mask = cv2.inRange(hsv, lower_white, upper_white) blue_mask = cv2.inRange(hsv, lower_blue, upper_blue) # 组合掩膜:白色=255,蓝色=128 combined_mask = np.zeros_like(white_mask) combined_mask[white_mask == 255] = 255 combined_mask[blue_mask == 255] = 128 # 转换为BGR用于显示 color_mask = cv2.cvtColor(combined_mask, cv2.COLOR_GRAY2BGR) # 从中间分割图片 height, width = img.shape[:2] mid = width // 2 # 左右两部分 left_img = img[:, :mid] right_img = img[:, mid:] left_mask = combined_mask[:, :mid] right_mask = combined_mask[:, mid:] # 计算白色像素比 def calc_white_percent(mask): total = mask.size white_pixels = np.sum(mask == 255) return (white_pixels / total) * 100 if total > 0 else 0 left_percent = calc_white_percent(left_mask) right_percent = calc_white_percent(right_mask) # 按比例缩小图片 def resize_image(image, scale): if scale == 1: return image new_width = int(image.shape[1] * scale) new_height = int(image.shape[0] * scale) return cv2.resize(image, (new_width, new_height), interpolation=cv2.INTER_AREA) # 创建结果展示图 result_img = np.hstack(( resize_image(left_img, output_scale), resize_image(right_img, output_scale), resize_image(color_mask, output_scale) )) # 添加文字说明 font = cv2.FONT_HERSHEY_SIMPLEX text_left = f"Left: {left_percent:.1f}% white" text_right = f"Right: {right_percent:.1f}% white" cv2.putText(result_img, text_left, (10, 30), font, 0.7, (0, 255, 0), 2) cv2.putText(result_img, text_right, (result_img.shape[1]//3 + 10, 30), font, 0.7, (0, 255, 0), 2) # 显示比较结果 if abs(left_percent - right_percent) < 5: comparison = "左右白色比相近" elif left_percent > right_percent: comparison = "左侧白色更多" else: comparison = "右侧白色更多" cv2.putText(result_img, comparison, (10, 70), font, 0.9, (0, 0, 255), 2) # 显示结果 cv2.imshow("Result: Left | Right | Mask", result_img) cv2.waitKey(0) cv2.destroyAllWindows() # 保存结果图片 output_path = "result.jpg" cv2.imwrite(output_path, result_img) print(f"结果图片已保存至: {output_path}") return { "left_white_percent": left_percent, "right_white_percent": right_percent, "output_scale": output_scale } # 使用示例 - 缩放比例为原始大小的60% result = compare_white_areas(r"C:\Users\lenovo\Desktop\conser.jpg", output_scale=0.6) 修改一下,不用def
11-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值