C++ OpenCV:kmeans分割C++;
我正在写一个程序来识别图像上的数字。 我没有看到代码中有错误。您将收到一个运行时异常。表示垫。 我使用Visual Studio 2015社区和OpenCV 3.1。 在stackoverflow上找到Kmeans分段代码 例外情况: test_opencv_nuget.exe!cv::Mat::at>(内部i0,内部i1)Сааааааа++ mat.inl.hpp文件 行: CV_DbgAssert((无符号)(i1*数据类型::通道)<(无符号)(size.p[1]*通道())C++ OpenCV:kmeans分割C++;,c++,opencv,C++,Opencv,我正在写一个程序来识别图像上的数字。 我没有看到代码中有错误。您将收到一个运行时异常。表示垫。 我使用Visual Studio 2015社区和OpenCV 3.1。 在stackoverflow上找到Kmeans分段代码 例外情况: test_opencv_nuget.exe!cv::Mat::at>(内部i0,内部i1)Сааааааа++ mat.inl.hpp文件 行: CV_DbgAssert((无符号)(i1*数据类型::通道)>摄像机图像; cvtColor(摄像机图像、灰度图像、
#包括“opencv2/opencv.hpp”
#包括“opencv2/imgproc/imgproc.hpp”
#包括“opencv2/imgcodecs.hpp”
#包括“opencv2/highgui/highgui.hpp”
#包括“opencv2/core.hpp”
#包括
使用名称空间cv;
使用名称空间std;
int main(int argc,字符**argv)
{
视频捕捉帽;
如果(!cap.open(0))
返回0;
Mat摄像机;
Mat灰度图像;
Mat二值图像;
Mat otsu_二值图像;
Mat-laplas_电路_图像;
对于(;;)
{
cap>>摄像机图像;
cvtColor(摄像机图像、灰度图像、cv::COLOR\U RGB2GRAY);
阈值(灰度图像、大津二值图像、0、255、CV阈值大津);
Mat-laplas_kernel=(Mat_uu3,3)您正在访问某个超出边界的Mat。使用调试器找出位置,并检查用于访问矩阵的索引是否有效。在创建“样本”矩阵时,似乎x和y是反向的:(y*otsu.rows+x,z)
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
VideoCapture cap;
if (!cap.open(0))
return 0;
Mat camera_image;
Mat gray_scale_image;
Mat binary_image;
Mat otsu_binary_image;
Mat laplas_circuit_image;
for (;;)
{
cap >> camera_image;
cvtColor(camera_image, gray_scale_image, cv::COLOR_RGB2GRAY);
threshold(gray_scale_image, otsu_binary_image, 0, 255, CV_THRESH_OTSU);
Mat laplas_kernel = (Mat_<float>(3, 3) << 0, 1, 0,
1, -4, 1,
0, 1, 0);
filter2D(otsu_binary_image, laplas_circuit_image, -1, laplas_kernel);
Mat samples(otsu_binary_image.rows * otsu_binary_image.cols, 3, CV_32F);
for (int y = 0; y < otsu_binary_image.rows; y++)
for (int x = 0; x < otsu_binary_image.cols; x++)
for (int z = 0; z < 3; z++)
samples.at<float>(y + x*otsu_binary_image.rows, z) = otsu_binary_image.at<Vec3b>(y, x)[z];
int clusterCount = 5;
Mat labels;
int attempts = 5;
Mat centers;
kmeans(samples, clusterCount, labels, TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 10000, 0.0001), attempts, KMEANS_PP_CENTERS, centers);
Mat kmeans_image(otsu_binary_image.size(), otsu_binary_image.type());
for (int y = 0; y < otsu_binary_image.rows; y++)
for (int x = 0; x < otsu_binary_image.cols; x++)
{
int cluster_idx = labels.at<int>(y + x*otsu_binary_image.rows, 0);
kmeans_image.at<Vec3b>(y, x)[0] = centers.at<float>(cluster_idx, 0);
kmeans_image.at<Vec3b>(y, x)[1] = centers.at<float>(cluster_idx, 1);
kmeans_image.at<Vec3b>(y, x)[2] = centers.at<float>(cluster_idx, 2);
}
imshow("original", camera_image);
imshow("gray_scale", gray_scale_image);
imshow("otsu", otsu_binary_image);
imshow("laplas_circuit", laplas_circuit_image);
imshow("clusters", kmeans_image);
char c = cvWaitKey(33);
if (c == 27) break; // press ESC
}
return 0;
}