int imageSeg_kmeans()
{
string image_path = "D:/vs2019Proj/ConsoleApplication1/timg.jpg";
Mat src_image = imread(image_path);
if (!src_image.data)
{
cout << "could not load image.." << endl;
return -1;
}
//颜色板
vector<Scalar> colorTab = {
{0,0,255},
{0,255,0},
{255,0,0},
{255,255,0},
{0,255,255},
{255,0,255}
};
int width = src_image.cols;
int height = src_image.rows;
int chnes = src_image.channels();
//把彩色图像中的数据点提取出来放入points里面
//points的类型是CV_32F
Mat points(width * height, chnes, CV_32F,Scalar(10));
for (int row = 0; row < height; row++)
{
uchar* ptr = src_image.ptr<uchar>(row);
for (int col = 0; col < width* chnes; col+= chnes)
{
int index = row * width + col/chnes;
float* ptr_points = points.ptr<float>(index);
ptr_points[0] = static_cast<int>(ptr[col]);//B
ptr_points[1] = static_cast<int>(ptr[col+1]);//G
ptr_points[2] = static_cast<int>(ptr[col+2]);
Opencv之kmeans图像分割
最新推荐文章于 2025-06-11 14:03:40 发布