opencv 提取单通道图像轮廓

程序功能:提取单通道图像轮廓

#include     
#include     
#include     
using namespace cv;
using namespace std;
int main()
{
	Mat SrcImage = imread("1.png");
	Mat grayImage, dstImage;
	//namedWindow("原图", 2);
	imshow("原图", SrcImage);
	cvtColor(SrcImage, grayImage, COLOR_RGB2GRAY);//灰度图
	threshold(grayImage, dstImage, 100, 255, THRESH_BINARY);//二值化图
	uchar *data = dstImage.data;
	int height = dstImage.rows;
	int width = dstImage.cols*dstImage.channels();
	cout << height << " " << width << endl;
	vector> state(height, vector(width, 0));     //动态二维数组,存储状态

	for (int i = 0; i < height; i++)
	{
		uchar *p = dstImage.ptr(i);             //行指针
		for (int j = 0; j < width; j++)
		{
			if (data[i*dstImage.step + j] == 0)
			{
				for (int m = i - 1; m <= i + 1; m++)
				{
					for (int n = j - 1; n <= j + 1; n++)
					{
						if (data[m*dstImage.step + n] == 255)
						{
							state[i][j] = 1;
						}
					}
				}
			}

		}
	}
	for (int i = 0; i < height; i++)
	{
		uchar *p = dstImage.ptr(i);
		for (int j = 0; j < width; j++)
		{
			if (state[i][j] == 1)
			{
				p[j] = 255;
			}
			else
			{
				p[j] = 0;
			}
		}
	}
	//namedWindow("轮廓图", 2);
	imshow("轮廓图", dstImage);
	waitKey(0);
	return 0;
}

程序结果:

opencv 提取单通道图像轮廓_第1张图片

你可能感兴趣的:(c++,图像处理,opencv,opencv基础,C++基础,图像处理)