白色坏点选区

#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;
}
void Draw_rectangle(Point pt1, Point pt2);
void on_mouse(int event, int x, int y, int flags, void* ustc);

Mat srcImage;
Point pre_pt(0, 0);
Point pre_pt_first;
//pre_pt_first1;//鼠标点
Point cur_pt(0, 0);
Point cur_pt_end;
//cur_pt_end1;
int box1_width = 0, box1_height = 0;//画框的宽和高
Rect box1;
bool flag1 = false;
bool flag2 = false;
bool flag3 = false;

int main()
{
	
	VideoCapture capture("34.avi");
	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);

	int i = 15;
	float last = 0;
	while (true)
	{
		capture >> srcImage;
		if (srcImage.empty())
		{
			cout << "视频加载失败 !" << endl;
			return -1;
		}
		setMouseCallback("【采集视频】", on_mouse, 0);
		if (flag3 == true)
		{
			Draw_rectangle(pre_pt_first, cur_pt_end);
			// 【2】定义一个Mat类型并给其设定ROI区域
			Mat imageROI = srcImage(Rect(pre_pt_first.x, pre_pt_first.y, box1_width, box1_height));
			Mat s3 = imageROI;
			Mat s2;
			cvtColor(s3, s2, COLOR_BGR2GRAY);
			threshold(s2, s2, 253, 255, THRESH_BINARY);

			namedWindow("阈值化", WINDOW_NORMAL);
			imshow("阈值化", s2);
			double a = sum(s2);
			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(900);
			
		}
		namedWindow("【采集视频】", WINDOW_NORMAL);
		imshow("【采集视频】", srcImage);
		waitKey(900);
	}
	return 0;
}

//【画框】
void Draw_rectangle(Point pt1, Point pt2)
{
	rectangle(srcImage, pt1, pt2, Scalar(0, 255, 0), 2, 8, 0);
}

//鼠标操作
void on_mouse(int event, int x, int y, int flags, void* ustc)
{
	
	if (event == CV_EVENT_LBUTTONDOWN)//左键按下
	{
		flag1 = true;
		pre_pt = Point(x, y);
		pre_pt_first = pre_pt;
		//sprintf(temp_1,"x:%d,y:%d",x,y);    
		//putText(src,temp_1,Point(x,y),FONT_HERSHEY_SIMPLEX,0.5,Scalar(255,255,255));  
		//imshow("【采集视频】", srcImage);
	}
	else if (event == EVENT_MOUSEMOVE && (flags & EVENT_FLAG_LBUTTON))//左键按下并且鼠标移动
	{
		if (flag1 == true)
		{
			flag2 = true;
		}

		cur_pt = Point(x, y);
		//sprintf_s(temp_1, "x:%d,y:%d", x, y);
		//putText(srcImage, temp_1, Point(x, y), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 255, 255));
		Draw_rectangle(pre_pt, cur_pt);
		//imshow("【采集视频】", srcImage);
	}
	else if (event == CV_EVENT_LBUTTONUP)//左键弹起
	{
		if (flag2 == true)
		{
			flag3 = true;
		}

		cur_pt = Point(x, y);
		cur_pt_end = cur_pt;
		//sprintf(temp_1, "x:%d,y:%d", x, y);
		//putText(srcImage1, temp_1, Point(x, y), FONT_HERSHEY_SIMPLEX, 0.4, Scalar(0, 255, 255));
		//circle(srcImage1, cur_pt, 3, cvScalar(255, 0, 0), CV_FILLED, CV_AA, 0);
		Draw_rectangle(pre_pt, cur_pt);
		imshow("【采集视频】", srcImage);
	}
	box1_width = cur_pt.x - pre_pt.x;
	box1_height = cur_pt.y - pre_pt.y;
	//Rect box1(pre_pt_first.x, pre_pt_first.y, box1_width, box1_height);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值