opencv实践程序3——打开摄像头视频及轮廓检测的简单程序

经过不断的测试终于试验好了
#include <iostream>
#include <cv.h>
#include <highgui.h>
using namespace std;
using namespace cv;


int main( int argc, char** argv )
{
VideoCapture cap(0); //打开默认的摄像头号  
    Mat edges;  
    namedWindow("edges",1); //边缘的窗口
namedWindow("video",1); //正常摄像头视频
int num = 0;   
    cap.open(num); 
for(;;)  //也可换为while(1)
    {  
        Mat frame;  
        cap >> frame; // 从摄像头中获取新的一帧  
if ( !frame.empty() )  imshow("Video", frame);//摄像头部分到这就结束了,下面全是关于轮廓的
        cvtColor(frame, edges, CV_BGR2GRAY);  
        GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);  
        Canny(edges, edges, 0, 30, 3);  
        imshow("edges", edges);  
        if(waitKey(30) >= 0) break;  
}
return 0;
}

最简单的打开摄像头视频程序
#include <iostream>
#include <cv.h>
#include <highgui.h>
using namespace std;
using namespace cv;


int main( int argc, char** argv )
{
VideoCapture cap(0);  
cap.open(0);  
   // namedWindow("edges",1); 
while(1) 
    {  
        Mat frame;  
        cap >> frame; 
if ( !frame.empty() )  imshow("Video", frame);
        if(waitKey(30) >= 0) break;  
}
cap.release();
  return 0;
}

简单视频

#include "cv.h" 
#include "highgui.h"

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <assert.h> 
#include <math.h> 
#include <float.h> 
#include <limits.h> 
#include <time.h> 
#include <ctype.h>

int main()
{

	cv::VideoCapture capture("1.mp4");//dssssssssssss
	if(!capture.isOpened())
	return 1;
	bool stop(false);

	cv::Mat frame;
	cv::namedWindow("extracted");
	while(!stop)
	{
		if(!capture.read(frame))
	break;
		cv::imshow("extracted frame",frame);
		if(cv::waitKey(10)>0)
			stop=true;
	}


	capture.release();
}




感谢这位朋友给出了起步代码:http://blog.youkuaiyun.com/BrikOff/article/details/21243491

### 实现轮廓检测的基础 为了在 Raspberry Pi 5 上使用 OpenCV 进行轮廓检测,需要先安装必要的软件包并编写相应的 Python 代码。这不仅涉及到了解基本的编程概念,还涉及到图像处理的具体方法[^1]。 #### 安装依赖项 确保已经安装了最新版本的 Raspbian 操作系统之后,可以通过命令行工具来更新现有的软件包列表,并安装所需的库: ```bash sudo apt-get update && sudo apt-get upgrade -y pip install opencv-python numpy ``` 上述命令会下载并安装 OpenCV 及其依赖项 `numpy`,后者对于高效地执行数组运算至关重要[^2]。 #### 编写轮廓检测程序 下面是一个简单的例子,展示了如何读取摄像头输入、转换色彩空间到灰度模式、应用二值化阈值处理以及查找和绘制轮廓: ```python import cv2 import numpy as np cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) _, threshed_frame = cv2.threshold(gray_frame, 127, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU) contours, _ = cv2.findContours(threshed_frame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contour_image = frame.copy() for cnt in contours: area = cv2.contourArea(cnt) if area > 100: # Filter out small noise-like objects. x,y,w,h = cv2.boundingRect(cnt) cv2.rectangle(contour_image,(x,y),(x+w,y+h),(0,255,0),2) cv2.imshow(&#39;Contour Detection&#39;, contour_image) key = cv2.waitKey(1) & 0xFF if key == ord("q"): break cv2.destroyAllWindows() cap.release() ``` 这段代码实现了从视频流中捕捉帧,将其转化为适合于轮廓提取的形式——即黑白两色表示法;接着通过调用函数找到所有的外部边界,并过滤掉面积过小的对象以减少误报率;最后将矩形框画出来以便直观展示所发现的目标位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值