代码
#include <stdio.h>
#include <string>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include<opencv2/highgui/highgui_c.h>
using namespace std;
using namespace cv;
int main()
{
// 图像读取及判断
Mat srcImage = imread("F:\\轮胎网图.jpg");
if (!srcImage.data)
return 1;
// 灰度转换
Mat srcGray;
cvtColor(srcImage, srcGray, CV_RGB2GRAY);
namedWindow("srcGray", CV_NORMAL);
imshow("srcGray", srcGray);
GaussianBlur(srcGray, srcGray, Size(11,11 ), 0);
threshold(srcGray, srcGray, 0, 255, CV_THRESH_OTSU);
namedWindow("otsuResultImage", CV_NORMAL);
imshow("otsuResultImage", srcGray);
//五个颜色,聚类之后的颜色随机从这里面选择
Scalar colorTab[] = {
Scalar(255,255,255),
Scalar(0,0,0),
};
int width = srcGray.cols;//图像的宽
int height = srcGray.rows;//图像的高
int channels = srcGray.channels();//图像的通道数
cout << width << height << "tong dao"<<channels << endl;
//初始化一些定义
int sampleCount = width * height;//所有的像素
int clusterCount = 2;//分类数
Mat points(sampleCount, channels, CV_8U, Scalar(0));//points用来保存所有的数据
Mat labels;//聚类后的标签
Mat center(clusterCount, 1, points.type());//聚类后的类别的中心
//将灰度图图像的像素转到到样本数据
int index = 0;
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
index = row * width + col; //为每个像素点赋索引值,一行一行的进行
int bgr = srcGray.at<uchar>(row, col);// 8U 类型的 RGB 彩色图像可以使用 <Vec3b>
points.at<float>(index) = static_cast<int>(bgr); //通道的分量
//cv::mat的成员函数: .at(int y, int x)可以用来存取图像中对应坐标为(x,y)的元素坐标。
}
}
//运行K-means算法
//MAX_ITER也可以称为COUNT最大迭代次数,EPS最高精度,10表示最大的迭代次数,0.1表示结果的精确度
TermCriteria criteria = TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 10, 0.1);
cv::kmeans(points, clusterCount, labels, criteria, 3, KMEANS_PP_CENTERS, center);
//显示图像分割结果
Mat result = Mat::zeros(srcGray.size(), srcGray.type());//创建一张结果图
for (int i = 0; i < srcGray.rows; i++)
{
for (int j = 0; j < srcGray.cols; j++)
{
index = i * width + j;
int label = labels.at<int>(index,0);//每一个像素属于哪个标签
result.at<uchar>(i, j) = colorTab[label][0];//对结果图中的每一个通道进行赋值
}
};
imshow("Kmeans", result);
cv::waitKey(0);
return 0;
}
报错是这个
提示的有900多行是啥意思,我程序没有900行为啥提示这么多行,还有如何知道这个900行错误是如何转换到现有的代码是多少行错误?