Opencv4图像分割和识别第二课(实战1)将目标从图像中分割出来

前言

本文提供Opencv4图像分割和识别课程(https://edu.csdn.net/course/detail/24864)第二课(实战一)的两个实例代码和一个课后作业。

实例一

将图像上的马(背景是草原)分割出来,并和原图像上下合并成一幅新图像。代码及其运行结果如下:

int horse_segment(const char *file_name, Mat &result_img)
{
	Mat img = imread(file_name);
	if (img.empty())
	{
		printf("reading image fails \n");
		return FILE_READ_FAIL;
	}

	Mat gray, binary;
	//cvtColor(img, gray, CV_BGR2GRAY); //opencv3.x 代码
	cvtColor(img, gray, COLOR_BGR2GRAY);
	threshold(gray, binary, 25, 255, THRESH_BINARY_INV);

	rectangle(img, Rect(260, 248, 240, 80), Scalar(0, 0, 255), 4);
	String text = "ltshan139";
	putText(img, text, Point(270, 300), FONT_HERSHEY_SIMPLEX, 1.5, Scalar(255, 255, 255), 2);

	int final_width, final_height;
	final_width = img.cols;
	final_height = img.rows << 1;
	result_img = Mat(Size(final_width, final_height), CV_8UC3);
	img.copyTo(result_img(Rect(0, img.rows, img.cols, img.rows)));

	Mat roi_noise = binary(Rect(0, 200, img.cols, img.rows - 200));
	blur(roi_noise, roi_noise, Size(3, 3));
	erode(roi_noise, roi_noise, Mat(), Point(-1, -1), 2);
	//cvtColor(binary, binary, CV_GRAY2BGR);
	cvtColor(binary, binary, COLOR_GRAY2BGR);
	binary.copyTo(result_img(Rect(0, 0, img.cols, img.rows)));

	return SUCCESS;
}

实例二

将图像上的两只熊分割出来,并和原图像左右合并成一幅新图像。代码及结果如下所示

int bear_segment(const char *file_name, Mat &result_img)
{
	Mat img = imread(file_name);
	if (img.empty())
	{
		printf("reading image fails \n");
		return -1;
	}

	Mat img_gray;
	cvtColor(img, img_gray, COLOR_BGR2GRAY);

	Mat img_bin;
	threshold(img_gray, img_bin, 120, 255, THRESH_BINARY_INV);

	Mat segment_bin(Size(img.cols, img.rows), CV_8UC1, Scalar(0));

	vector<vector<Point>> contours;
	//findContours(img_bin, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); //opencv3.x
	findContours(img_bin, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
	int i, size;
	size = contours.size();
	for (i = 0; i < size; i++)
	{
		if (contourArea(contours[i]) > 20000)
			drawContours(segment_bin, contours, i, Scalar(255), FILLED);
	}

	int width, height;
	width = img.cols;
	height = img.rows;
	result_img = Mat(Size(2 * width, height), CV_8UC3);
	img.copyTo(result_img(Rect(0, 0, width, height)));

	Mat seg_bin_color;
	cvtColor(segment_bin, seg_bin_color, COLOR_GRAY2BGR);
	seg_bin_color.copyTo(result_img(Rect(width, 0, width, height)));

	return 0;
}

 课后作业一

将两只熊中较大者分割并显示出来。 代码修改部分及其结果图如下所示。#if 0下面的代码是原来代码,#else部分则是升级代码。

#if 0
	vector<vector<Point>> contours;
	//findContours(img_bin, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); //opencv3.x
	findContours(img_bin, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
	int i, size;
	size = contours.size();
	for (i = 1; i < size; i++)
	{
		if (contourArea(contours[i]) > 20000)
		{
			drawContours(segment_bin, contours, i, Scalar(255), FILLED);
		}
	}

#else
	erode(img_bin, img_bin, Mat());

	vector<vector<Point>> contours;
	//findContours(img_bin, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); //opencv3.x
	findContours(img_bin, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
	int i, size;
	size = contours.size();
	int max_index = 0;
	double max_area = contourArea(contours[0]);
	for (i = 1; i < size; i++)
	{
		double area = contourArea(contours[i]);
		if (area > max_area)
		{
			max_area = area;
			max_index = i;
		}
	}
	drawContours(segment_bin, contours, max_index, Scalar(255), FILLED);
#endif

 

 

 

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ltshan139

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

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

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

打赏作者

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

抵扣说明:

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

余额充值