首页 > 其他分享 >OpenCV3.2图像分割 实例3:KMeans图像分割

OpenCV3.2图像分割 实例3:KMeans图像分割

时间:2023-08-18 09:00:55浏览次数:33  
标签:src 分割 index int KMeans Scalar 图像 col row

 1 #include <opencv2/opencv.hpp>
 2 #include <iostream>
 3  
 4 using namespace cv;
 5 using namespace std;
 6  
 7 int main(int argc, char** argv) {
 8     Mat src = imread("toux.jpg");
 9     if (src.empty()) {
10         printf("could not load image...\n");
11         return -1;
12     }
13     namedWindow("input image", CV_WINDOW_AUTOSIZE);
14     imshow("input image", src);
15  
16     Scalar colorTab[] = {
17         Scalar(0, 0, 255),
18         Scalar(0, 255, 0),
19         Scalar(255, 0, 0),
20         Scalar(0, 255, 255),
21         Scalar(255, 0, 255)
22     };
23  
24     int width = src.cols;//图像的列
25     int height = src.rows;//图像的行
26     int dims = src.channels();//图像的通道数
27  
28     // 初始化定义
29     int sampleCount = width*height;//获取图像的像素点数
30     int clusterCount = 4;//要分类块数
31     Mat points(sampleCount, dims, CV_32F, Scalar(10));//定义样本数据
32     Mat labels;
33     Mat centers(clusterCount, 1, points.type());//定义中心点(浮点数)
34  
35     // RGB 数据转换到样本数据
36     int index = 0;
37     for (int row = 0; row < height; row++) {
38         for (int col = 0; col < width; col++) {
39             index = row*width + col;
40             Vec3b bgr = src.at<Vec3b>(row, col);
41             points.at<float>(index, 0) = static_cast<int>(bgr[0]);//通道0
42             points.at<float>(index, 1) = static_cast<int>(bgr[1]);//通道1
43             points.at<float>(index, 2) = static_cast<int>(bgr[2]);//通道2
44         }
45     }
46  
47     // 运行K-Means
48     TermCriteria criteria = TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 10, 0.1);
49     kmeans(points, clusterCount, labels, criteria, 3, KMEANS_PP_CENTERS, centers);//尝试3次
50  
51     // 显示图像分割结果
52     Mat result = Mat::zeros(src.size(), src.type());
53     for (int row = 0; row < height; row++) {
54         for (int col = 0; col < width; col++) {
55             index = row*width + col;
56             int label = labels.at<int>(index, 0);
57             result.at<Vec3b>(row, col)[0] = colorTab[label][0];
58             result.at<Vec3b>(row, col)[1] = colorTab[label][1];
59             result.at<Vec3b>(row, col)[2] = colorTab[label][2];
60         }
61     }
62  
63     for (int i = 0; i < centers.rows; i++) {
64         int x = centers.at<float>(i, 0);
65         int y = centers.at<float>(i, 1);
66         printf("center %d = c.x : %d, c.y : %d\n", i, x, y);
67     }
68     
69     imshow("KMeans Image Segmentation Demo", result);
70     waitKey(0);
71     return 0;
72 }

注:中心点是相对于图像RGB像素值而求定的位置。

标签:src,分割,index,int,KMeans,Scalar,图像,col,row
From: https://www.cnblogs.com/ybqjymy/p/17639440.html

相关文章

  • OpenCV3.2图像分割 实例4:GMM(高斯混合模型)样本数据训练与预言
    1#include<opencv2/opencv.hpp>2#include<iostream>34usingnamespacecv;5usingnamespacecv::ml;6usingnamespacestd;78intmain(intargc,char**argv){9Matimg=Mat::zeros(500,500,CV_8UC3);10RNGrng(123......
  • OpenCV3.2图像分割 实例5:GMM(高斯混合模型)图像分割
    1#include<opencv2/opencv.hpp>2#include<iostream>34usingnamespacecv;5usingnamespacecv::ml;6usingnamespacestd;78intmain(intargc,char**argv){9Matsrc=imread("toux.jpg");10if(src.empt......
  • C++ 字符串分割函数
    #include<iostream>#include<string>#include<vector>#include<algorithm>#include<chrono>usingnamespacestd;voidMyprint(strings){cout<<s<<endl;}vector<string>Split(strings,stringc){......
  • 分割等和子集(没理解透彻)
    给定一个非空的正整数数组nums,请判断能否将这些数字分成元素和相等的两部分。示例1:输入:nums=[1,5,11,5]输出:true解释:nums可以分割成[1,5,5]和[11]。示例2:输入:nums=[1,2,3,5]输出:false解释:nums不可以分为和相等的两部分背包问题解决classSolution{......
  • 计算机视觉(Computer Vision),计算机图形学(Computer Graphics)和数字图像(Image Proce
    计算机视觉(ComputerVision),计算机图形学(ComputerGraphics)和数字图像(ImageProcessing)从学科分类:ComputerScience/ArtificialIntelligence/ComputerVisionComputerScience/ComputerGraphicsandVisualizationElectricalEngineering/SignalProcessing/Digit......
  • 基于FPGA实现的图像加密与解密
    双非硕士研一下学期视觉转FPGA长路漫漫,但希望前途光明基于图像加密的方法网上有特别多种由于我自己本身也是初学者,所以就复现了最简单最直接的加密和解密的方法也就是明文和密钥的异或操作 显示的图片如下:原图/解密 加密图片思路:1)定义两个ROM,分别存储明文数据和密钥数据,明文数据......
  • Python小项目:利用 U-net 完成细胞图像分割
    完整数据集下载:下载链接1前言在当今数字化时代,图像处理和分析已经成为了科学研究和技术应用领域的关键部分。在生物医学领域,图像分析对于诊断、治疗和疾病研究具有重要意义。本项目将带您深入了解U-net细胞分割技术,这是一种在生物医学图像领域广泛应用的语义分割方法,旨在精......
  • 【黑产攻防道02】如何对抗黑产的图像识别模型
    全文8799字,预计阅读时间约25分钟,建议收藏后阅读验证码本质上自带一层答案的语义,这是天然的区分人和自动程序的地方。无论使用哪一种方式破解验证码,都必须识别出答案。在多年与黑产破解者的博弈对抗的过程中,我们发现,从限制黑产获取验证码图片答案这个方向来进行防御,不仅可以减少运营......
  • CutLER:一种用于无监督目标检测和实例分割的方法
    本文分享自华为云社区《CutLER:一种用于无监督目标检测和实例分割的方法》,作者:Hint。目标检测是计算机视觉中的一种重要任务,使AI系统感知、推理、理解目标。训练定位模型需要特别的标注,比如目标的框、掩膜、定位点等。本文的工作研究了无监督的目标检测和实例分割,不使用人工标注。首......
  • 在Java中将 Word 转换为图像
    您可能需要将Word文档转换为图像的原因有很多。例如,很多设备不需要任何特殊软件就可以直接打开并显示图像,并且图像在传输时其内容很难被篡改。在本文中,您将学习如何使用Spire.DocforJava将Word转换为流行的图像格式,例如JPG、PNG和SVG。 安装适用于Java的Spire.Doc首先,您......