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

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章