一、将手机当做摄像头和opencv连接
1.首先在手机上下载【ip摄像头】
2.
#include <iostream>
#include<opencv2/highgui.hpp>;
#include<opencv2/imgcodecs.hpp>;
#include<opencv2/imgproc.hpp>;
#include <opencv2/objdetect.hpp>;
using namespace cv;
using namespace std;
int main() {
VideoCapture capture;
Mat frame;
//注意下面的连接部分,admin:admin(账号密码打开软件后会提示,也可以设置),
//@符号之后的是局域网ip地址(打开app后,点击下方“打开IP摄像头服务器”,会有显示局域网ip)
//即:http://<USERNAME>:<PASSWORD>@<IP_ADDRESS>/<the value of src>
capture.open("http://123:[email protected]:8081/");
while (1)
{
capture >> frame; //读取当前每一帧画面
imshow("读取视频", frame); //显示当前图像帧
waitKey(10); //延时30ms
}
return 0;
}
代码中用到了登陆名,密码,ip,端口号,手机端软件都会告诉你,而且还可以设置,图像的质量、格式也可以设置,挺不错的一款软件。
二、摄像头人脸检测
#include <iostream>
#include<opencv2/highgui.hpp>;
#include<opencv2/imgcodecs.hpp>;
#include<opencv2/imgproc.hpp>;
#include <opencv2/objdetect.hpp>;
using namespace cv;
using namespace std;
int main() {
VideoCapture capture;
Mat img;
capture.open("http://123:[email protected]:8081/");
//创建级联,人脸检测的实际代码
CascadeClassifier faceCascade;
faceCascade.load("D:/Resources/haarcascade_frontalface_default.xml");
vector<Rect> faces;
while (true)
{
capture >> img;
//人脸检测
faceCascade.detectMultiScale(img, faces, 1.1, 10);
for (int i = 0; i < faces.size(); i++)
{
rectangle(img, faces[i].tl(), faces[i].br(), Scalar(255, 0, 255), 3);
}
imshow("Image", img);
waitKey(5);
}
}
三、虚拟画家
大体思路:找到颜色,获取轮廓,画圆
1.通过摄像头找到自己想要提取的颜色
#include <iostream>
#include<opencv2/highgui.hpp>;
#include<opencv2/imgcodecs.hpp>;
#include<opencv2/imgproc.hpp>;
#include <opencv2/objdetect.hpp>;
using namespace cv;
using namespace std;
int hmin = 0, smin = 0, vmin = 0;
int hmax = 179, smax = 255, vmax = 255;
VideoCapture capture;
Mat frame, imgHSV, mask;
int main() {
namedWindow("Trackbars", (600, 200));
createTrackbar("hue min", "Trackbars", &hmin, 179);
createTrackbar("hue max", "Trackbars", &hmax, 179);
createTrackbar("sat min", "Trackbars", &smin, 255);
createTrackbar("sat max", "Trackbars", &smax, 255);
createTrackbar("val min", "Trackbars", &vmin, 255);
createTrackbar("val max", "Trackbars", &vmax, 255);
capture.open("http://123:[email protected]:8081/");
while (1)
{
capture >> frame;
//转换为色相空间
cvtColor(frame, imgHSV, COLOR_BGR2HSV);
Scalar lower(hmin, smin, vmin);
Scalar upper(hmax, smax, vmax);
inRange(imgHSV, lower, upper, mask);
imshow("提色", frame);
imshow("正常", mask);
waitKey(30);
}
return 0;
}
(大体搞了下但我的环境很杂 所以效果不是特别的好)
2.获取提色的蒙版
#include <iostream>
#include<opencv2/highgui.hpp>;
#include<opencv2/imgcodecs.hpp>;
#include<opencv2/imgproc.hpp>;
#include <opencv2/objdetect.hpp>;
using namespace cv;
using namespace std;
VideoCapture capture;
Mat frame;
//这里是按照从min到max
vector<vector<int>> myColors{ {90,186,0,179,255,255},//粉色
{124,48,117,143,170,255} };//紫色
//现在还要定义显示什么颜色,当检测到上面这些的时候 我们就显示这些颜色
vector<vector<int>> myColorValues{ {0,255,0},//绿色
{255,0,255} };//紫色
void findColor(Mat img) {
Mat imgHSV;
cvtColor(img, imgHSV, COLOR_BGR2HSV);
for (int i = 0; i < myColors.size(); i++)
{
Scalar lower(myColors[i][0], myColors[i][1], myColors[i][2]);
Scalar upper(myColors[i][3], myColors[i][4], myColors[i][5]);
Mat mask;
inRange(imgHSV, lower, upper, mask);
imshow(to_string(i), mask);
}
}
int main() {
capture.open("http://123:[email protected]:8081/");
while (1)
{
capture >> frame;
findColor(frame);
imshow("读取视频", frame);
waitKey(10);
}
return 0;
}
当视频里出现相应颜色的时候 才会显示相应的蒙版
3.现在可以从这个蒙版获取精确的点(画出蒙版轮廓)
#include <iostream>
#include<opencv2/highgui.hpp>;
#include<opencv2/imgcodecs.hpp>;
#include<opencv2/imgproc.hpp>;
#include <opencv2/objdetect.hpp>;
using namespace cv;
using namespace std;
VideoCapture capture;
Mat frame;
//这里是按照从min到max
vector<vector<int>> myColors{ {90,186,0,179,255,255},//粉色
{124,48,117,143,170,255} };//紫色
//现在还要定义显示什么颜色,当检测到上面这些的时候 我们就显示这些颜色
vector<vector<int>> myColorValues{ {0,255,0},//绿色
{255,0,255} };//紫色
void getContours(Mat imgDil) {
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(imgDil, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
for (int i = 0; i < contours.size(); i++)
{
int area = contourArea(contours[i]);
if (area > 1000) {
//打印轮廓
drawContours(frame, contours, i, Scalar(255, 0, 255), 2);
}
}
}
void findColor(Mat img) {
Mat imgHSV;
cvtColor(img, imgHSV, COLOR_BGR2HSV);
for (int i = 0; i < myColors.size(); i++)
{
Scalar lower(myColors[i][0], myColors[i][1], myColors[i][2]);
Scalar upper(myColors[i][3], myColors[i][4], myColors[i][5]);
Mat mask;
inRange(imgHSV, lower, upper, mask);
/*imshow(to_string(i), mask);*/
getContours(mask);
}
}
int main() {
capture.open("http://123:[email protected]:8081/");
while (1)
{
capture >> frame;
findColor(frame)