OpenCV视频中分割圆形区域,利用判断图中的点是否在画的圆内,如果在圆内,则不变色;如果不在圆内,则改变它的的颜色。从而只能看到圆内的视频,并且可以把圆形部分截取,利用的是ROI(感兴趣区域),但是因为图片必须是矩形的,所以ROI也只能是矩形,截取的圆外接矩形。
使用的开发工具
OPENCV版本:OpenCV2.4.13
操作系统:windows10
VS版本:VS2015专业版
加载视频
加载视频有两种方法:一种是采用摄像头,另外一种是从视频文件中读入。但是需要注意的是从摄像头加载视频,最好先设定摄像头的显示窗口大小,width和height。视频文件就不需要了,因为视频文件有固定的宽和高,把每帧图片赋给新创建的图片之后,可以直接使用cols和rows属性,摄像头的话就需要使用到设定的width和height。
1、摄像头设置窗口大小
capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
2、AVI视频文件注意路径不要弄错,或者直接和.cpp源文件放在一块,是双斜杠\\
鼠标操作
这个链接有鼠标操作的一些解释,有需要的话,可以看看
画圆和框
这里画圆是先根据画线来确定的,画的线的长度就是圆的半径,半径利用勾股定理求得,圆心就是线段的最后一个点。
1、画圆函数
//【画圆】
void Draw_circle(Point pt_center, int pt_radius)
{
circle(srcImage, pt_center, pt_radius, Scalar(0, 255, 0), 1, 8, 0);
}
2、画框函数
//【画框】
void Draw_rectangle(Point pt1, Point pt2)
{
rectangle(srcImage, pt1, pt2, Scalar(0, 255, 0), 2, 8, 0);
}
3、勾股定理求半径
把圆形周围的颜色改变
画完圆之后,需要把圆周围颜色改变,而圆内不变。遍历图片,然后进行变色。
1、蓝色通道B
srcImage.at<Vec3b>(Point(x, y))[0] = 255;
2、绿色通道G
srcImage.at<Vec3b>(Point(x, y))[1] = 0;
3、红色通道R
srcImage.at<Vec3b>(Point(x, y))[2] = 0;
4、循环遍历,然后改变颜色
for (int x = 0; x < srcImage.cols; x++)
{
for (int y = 0; y < srcImage.rows; y++)
{
int temp = ((x - center.x) * (x - center.x) + (y - center.y) *(y - center.y));
if (temp < (radius * radius))
{
//srcImage.at<Vec3b>(Point(x, y))[0] = 0;
//srcImage.at<Vec3b>(Point(x, y))[1] = 255;
//srcImage.at<Vec3b>(Point(x, y))[2] = 0;
}
else
{
srcImage.at<Vec3b>(Point(x, y))[0] = 255;
srcImage.at<Vec3b>(Point(x, y))[1] = 0;
srcImage.at<Vec3b>(Point(x, y))[2] = 0;
}
}
}
截取圆形区域
1、把圆的外接矩形当做ROI
Mat imageROI = srcImage(Rect(center.x - radius, center.y - radius, radius * 2, radius * 2));
2、截取并保存
imshow("【logo】", logoImage);
imwrite("logo.jpeg", logoImage);
效果图
源代码
#include<opencv2/opencv.hpp>
#include<iostream>
#include <math.h>
using namespace std;
using namespace cv;
void Draw_rectangle(Point pt1, Point pt2);
void on_mouse(int event, int x, int y, int flags, void* ustc);
void Draw_circle(Point pt_center, int pt_radius);
void Draw_line(Point pt1, Point pt2, Scalar color);
Mat srcImage;
Point pre_pt = (0, 0), pre_pt_first, pre_pt_first1;//鼠标点
Point cur_pt = (0, 0), cur_pt_end, cur_pt_end1, center, pt_center;
float radius, A, B;
bool flag1 = false;
bool flag2 = false;
bool flag3 = false;
#define WINDOW_NAME "【drawing】"
int main()
{
VideoCapture capture("C:\\Users\\Administrator\\Desktop\\1.avi");
capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
while (true)
{
capture >> srcImage;
/*if (!capture.isOpened())//判断摄像头是否打开
{
cout << "视频加载失败 !" << endl;
}*/
if(srcImage.empty())
{
cout << "视频加载失败 !" << endl;
return -1;
}
setMouseCallback("【采集视频】", on_mouse, 0);
if (flag3 == true)
{
Draw_circle(center, radius);
for (int x = 0; x < srcImage.cols; x++)
{
for (int y = 0; y < srcImage.rows; y++)
{
int temp = ((x - center.x) * (x - center.x) + (y - center.y) *(y - center.y));
if (temp < (radius * radius))
{
//srcImage.at<Vec3b>(Point(x, y))[0] = 0;
//srcImage.at<Vec3b>(Point(x, y))[1] = 255;
//srcImage.at<Vec3b>(Point(x, y))[2] = 0;
}
else
{
srcImage.at<Vec3b>(Point(x, y))[0] = 255;
srcImage.at<Vec3b>(Point(x, y))[1] = 0;
srcImage.at<Vec3b>(Point(x, y))[2] = 0;
}
}
}
Mat imageROI = srcImage(Rect(center.x - radius, center.y - radius, radius * 2, radius * 2));
Mat logoImage = imageROI;
imshow("【logo】", logoImage);
imwrite("logo.jpeg", logoImage);
}
imshow("【采集视频】", srcImage);
waitKey(1);
}
return 0;
}
//【画线】
void Draw_line(Point pt1, Point pt2, Scalar color)
{
line(srcImage, pt1, pt2, color, 2, CV_AA);
}
//【画框】
void Draw_rectangle(Point pt1, Point pt2)
{
rectangle(srcImage, pt1, pt2, Scalar(0, 255, 0), 2, 8, 0);
}
//【画圆】
void Draw_circle(Point pt_center, int pt_radius)
{
circle(srcImage, pt_center, pt_radius, Scalar(0, 255, 0), 1, 8, 0);
}
//鼠标操作
void on_mouse(int event, int x, int y, int flags, void* ustc)
{
char temp_1[20];
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(temp_1, "x:%d,y:%d", x, y);
putText(srcImage, temp_1, Point(x, y), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 255, 255));
A = cur_pt.x - pre_pt_first.x;
B = cur_pt.y - pre_pt_first.y;
radius = sqrt(A*A + B*B);
center = Point(cur_pt.x, cur_pt.y);
Draw_line(Point(pre_pt_first.x, pre_pt_first.y), Point(cur_pt.x, cur_pt.y), Scalar(0, 0, 255));
Draw_circle(center, radius);
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);
A = cur_pt_end.x - pre_pt_first.x;
B = cur_pt_end.y - pre_pt_first.y;
radius = sqrt(A*A + B*B);
center = Point(cur_pt_end.x, cur_pt_end.y);
Draw_line(Point(pre_pt_first.x, pre_pt_first.y), Point(cur_pt.x, cur_pt.y), Scalar(0, 0, 255));
Draw_circle(center, radius);
imshow("【采集视频】", srcImage);
}
}