opencv 提取彩色图像轮廓
本程序功能:提取彩色3通道图像的轮廓
#include <opencv.hpp>
#include <iostream>
#include <vector>
using namespace cv;
using namespace std;
int main()
{
Mat dstImage = imread("1.png");
namedWindow("原图", 2);
imshow("原图", dstImage);
uchar *data = dstImage.data;
int height = dstImage.rows;
int width = dstImage.cols;
vector<vector<int>> state(height, vector<int>(width, 0)); //动态二维数组,标记是否为轮廓点
int scale = dstImage.channels();
//for (int i = 0; i < height; i++)
//{
// for (int j = 0; j < width; j++)
// {
// uchar *p = dstImage.ptr<uchar>(i,j);
// int a = p[0]; //B通道灰度值
// int b = p[1]; //G通道灰度值
// int c = p[2]; //R通道灰度值
// }
//}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
uchar *p = dstImage.ptr<uchar>(i,j); //指向彩色图像的每个像素值
if (p[0] == 255 && i > 0 && i < height&&j>0 && j < width)
{
for (int m = i - 1; m <= i + 1; m++)
{
for (int n = j - 1; n <= j + 1; n++)
{
uchar *p1 = dstImage.ptr<uchar>(m, n);
if (p1[0] == 0)
{
state[i][j] = 1;
}
}
}
}
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
uchar *p = dstImage.ptr<uchar>(i,j);
if (state[i][j] == 1)
{
p[0] = 0;
p[1] = 0;
p[2] = 255;
}
}
}
namedWindow("轮廓图", 2);
imshow("轮廓图", dstImage);
waitKey(0);
return 0;
}
运行结果: