opencv之调用摄像头简单轮廓提取

程序

#include "stdafx.h"

//本节讲述 画线、点、矩形、圆形和椭圆.多边形,文本。 等函数;
#include <opencv2/opencv.hpp>
#include <iostream>


using namespace std;
using namespace cv;

int threshold_value = 50;
int threshold_max = 255;

//Mat bgImage;
//const char *drawdemo_win = "draw shapes and text demo";
Mat addSaltNoise(const Mat srcImage, int n);
VideoCapture capture(0);
Mat vidio, gray_src, dst4, dst5, frame;
void Canny_Demo(int, void*);

int main(int argc, char**argv)
{
	Mat src, dst;

	src = imread("C:/Users/Rubison.DELL/Desktop\\杂物/壁纸/小白2.jpg");  //存放自己图像的路径 
	if (!src.data)
	{
		printf("could not load image...\r\n");
		return -1;
	}
	//char input_title[] = "原图";
	//char output_title[] = "模糊处理";

	while (1)
	{
		capture >> frame;
		imshow("原图",frame);

		cvtColor(frame, vidio, CV_BGR2GRAY);
		Canny_Demo(0, 0);


		//imshow("相机", vidio);
		waitKey(30);

	}
	waitKey(0);
	return 0;

}

void Canny_Demo(int, void*)
{
	blur(vidio, vidio, Size(3, 3), Point(-1, -1), BORDER_DEFAULT);
	Canny(vidio, dst4, 50, 100, 3, false);
	imshow("canny轮廓检测", dst4);
	dst5.create(frame.size(), frame.type());   //这两行代码是将原像素颜色拷贝到二值图中
	imshow("检测dst5", dst5);
	frame.copyTo(dst5, dst4);                //从而生成彩色边缘图

	imshow("结果", dst5);

}

运行结果
在这里插入图片描述
在这里插入图片描述
头一晃就炸了!!!!!
在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
当然可以!下面是一个使用Python和OpenCV调用摄像头并进行数字识别的示例代码: ```python import cv2 import numpy as np from keras.models import load_model # 加载数字识别模型 model = load_model('digit_recognition_model.h5') # 设置摄像头 cap = cv2.VideoCapture(0) while True: # 读取摄像头的帧 ret, frame = cap.read() # 将帧转换为灰度图像 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 对图像进行二值化处理 _, thresholded = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY_INV) # 查找图像中的轮廓 contours, _ = cv2.findContours(thresholded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for contour in contours: # 获取轮廓的边界框 x, y, w, h = cv2.boundingRect(contour) # 提取数字区域 digit_region = thresholded[y:y+h, x:x+w] # 调整数字区域的大小 resized_digit = cv2.resize(digit_region, (28, 28)) # 将图像转换为模型输入所需的格式 input_data = np.reshape(resized_digit, (1, 28, 28, 1)).astype('float32') / 255 # 使用模型进行预测 prediction = model.predict_classes(input_data)[0] # 在图像上绘制识别结果 cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.putText(frame, str(prediction), (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2) # 显示结果图像 cv2.imshow('Digit Recognition', frame) # 按下 'q' 键退出循环 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放摄像头并关闭窗口 cap.release() cv2.destroyAllWindows() ``` 在运行代码之前,你需要确保已经安装了OpenCV和Keras库,并且已经训练了一个数字识别模型(保存为'digit_recognition_model.h5')。这个模型可以使用MNIST数据集或者其他适用于数字识别的数据集进行训练。 这段代码会打开计算机的摄像头,实时读取摄像头的帧,并对每一帧进行数字识别。识别结果会在图像上用矩形框和数字进行标注。按下 'q' 键可以退出程序。 希望对你有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w5875895

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值