灰度图像,Canny边缘检测,同一个窗口中同时显示三个阶段的图像

创建一个程序实现以下功能:

a.从视频读取图像帧;将结果转化为灰度图像;对该图像完成Canny边缘检测。用三个窗口分别显示上述三个步骤的结果,并给窗口适当命名。
b.在同一个窗口中同时显示三个阶段的图像。(提示:创建一个图像,高度与原始图像相同,高度是原始视频帧的三倍。将三个图像分别拷贝到该图像中,或者使用指针(更加机智的方法)创建三个图像头部,分别指向该图像数据的开头处、三分之一处和三分之二处,接下来使copyTo()函数。
c.在图像的三个不同部分使用文本标签分别描述三个过程。

代码环境

vs2017 + opencv 2.4.8

效果如下

在这里插入图片描述

代码 注释详细

#include <stdio.h>
#include <Windows.h>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <stdio.h>
#include <iostream> // std

using namespace cv;
using namespace std;

int main()
{
	// 原图像 灰度图像 Canny边缘检测图像
	Mat greatImg, cannyImg, sourceImg;
	// 读取视频
	VideoCapture cap("D:/Deskoppe/is/2.avi");
	// 读取异常处理
	if (!cap.isOpened()) return 0;
	Mat fram;
	while (true)
	{
		cap >> fram; // 读取一帧
		if (fram.empty()) break;

		// 缩放未二分之一
		resize(fram, sourceImg, Size(fram.cols * 0.5, fram.rows *0.5), 0, 0, INTER_LINEAR);
		int  heigth = sourceImg.rows; // 读取高度
		int  width = sourceImg.cols;// 读取宽度
		 // 二值化处理
		cvtColor(sourceImg, greatImg, COLOR_RGB2GRAY);
		// 边缘检测处理
		Canny(greatImg, cannyImg, 30, 100, 3);
		// 创建一个三合一的图像容器
		Mat newImg;
		newImg.create(heigth, width * 3, CV_8UC3);

		imshow("sourceImg", sourceImg);
		imshow("greatImg", greatImg);
		imshow("cannyImg", cannyImg);
		// 将处理好的图像合并到相应位置
		putText(sourceImg, "sourceImg", Point(120, 30), FONT_HERSHEY_COMPLEX_SMALL, 1, Scalar(0, 0, 255), 1, 8);
		sourceImg.copyTo(newImg(Rect(0, 0, width, heigth)));

		Mat tem3;
		// 将单通道图像转换为三通道,bong合并
		tem3.create(heigth, width, CV_8UC3);
		cvtColor(greatImg, tem3, COLOR_GRAY2BGR);
		putText(tem3, "er zhi hua", Point(120, 30), FONT_HERSHEY_COMPLEX_SMALL, 1, Scalar(0, 0, 255), 1, 8);
		tem3.copyTo(newImg(Rect(width, 0, width, heigth)));

		Mat tem31;
		tem31.create(heigth, width, CV_8UC3);
		cvtColor(cannyImg, tem31, COLOR_GRAY2BGR);
		putText(tem31, "bian yuan chu li", Point(120, 30), FONT_HERSHEY_COMPLEX_SMALL, 1, Scalar(0, 0, 255), 3, 8);
		tem31.copyTo(newImg(Rect(width * 2, 0, width, heigth)));

		imshow("newImg", newImg);

		waitKey(1000);

	}

	waitKey(0);
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值