OpenCV 在图像上运行 kmeans 算法

发布于 2024-11-29 07:44:41 字数 1382 浏览 2 评论 0原文

我试图在 3 通道彩色图像上运行 kmeans,但每次我尝试运行该函数时,它似乎都会崩溃并出现以下错误:

OpenCV Error: Assertion failed (data.dims <= 2 && type == CV_32F && K > 0) in unknown function, file ..\..\..\OpenCV-2.3.0\modules\core\src\matrix.cpp, line 2271

我在下面的代码中包含了一些注释,以帮助指定传入的内容。非常感谢任何帮助。

// Load in an image
// Depth: 8, Channels: 3
IplImage* iplImage = cvLoadImage("C:/TestImages/rainbox_box.jpg");

// Create a matrix to the image
cv::Mat mImage = cv::Mat(iplImage);

// Create a single channel image to create our labels needed
IplImage* iplLabels = cvCreateImage(cvGetSize(iplImage), iplImage->depth, 1);

// Convert the image to grayscale
cvCvtColor(iplImage, iplLabels, CV_RGB2GRAY);

// Create the matrix for the labels
cv::Mat mLabels = cv::Mat(iplLabels);

// Create the labels
int rows = mLabels.total();
int cols = 1;
cv::Mat list(rows, cols, mLabels .type());
uchar* src;
uchar* dest = list.ptr(0);
for(int i=0; i<mLabels.size().height; i++) 
{
    src = mLabels.ptr(i);
    memcpy(dest, src, mLabels.step);
    dest += mLabels.step;
}
list.convertTo(list, CV_32F);

// Run the algorithm
cv::Mat labellist(list.size(), CV_8UC1);
cv::Mat centers(6, 1, mImage.type());
cv::TermCriteria termcrit(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0);
kmeans(mImage, 6, labellist, termcrit, 3, cv::KMEANS_PP_CENTERS, centers);

I am trying to run kmeans on a 3 channel color image, but every time I try to run the function it seems to crash with the following error:

OpenCV Error: Assertion failed (data.dims <= 2 && type == CV_32F && K > 0) in unknown function, file ..\..\..\OpenCV-2.3.0\modules\core\src\matrix.cpp, line 2271

I've included the code below with some comments to help specify what is being passed in. Any help is greatly appreciated.

// Load in an image
// Depth: 8, Channels: 3
IplImage* iplImage = cvLoadImage("C:/TestImages/rainbox_box.jpg");

// Create a matrix to the image
cv::Mat mImage = cv::Mat(iplImage);

// Create a single channel image to create our labels needed
IplImage* iplLabels = cvCreateImage(cvGetSize(iplImage), iplImage->depth, 1);

// Convert the image to grayscale
cvCvtColor(iplImage, iplLabels, CV_RGB2GRAY);

// Create the matrix for the labels
cv::Mat mLabels = cv::Mat(iplLabels);

// Create the labels
int rows = mLabels.total();
int cols = 1;
cv::Mat list(rows, cols, mLabels .type());
uchar* src;
uchar* dest = list.ptr(0);
for(int i=0; i<mLabels.size().height; i++) 
{
    src = mLabels.ptr(i);
    memcpy(dest, src, mLabels.step);
    dest += mLabels.step;
}
list.convertTo(list, CV_32F);

// Run the algorithm
cv::Mat labellist(list.size(), CV_8UC1);
cv::Mat centers(6, 1, mImage.type());
cv::TermCriteria termcrit(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0);
kmeans(mImage, 6, labellist, termcrit, 3, cv::KMEANS_PP_CENTERS, centers);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弃爱 2024-12-06 07:44:41

错误说明了一切断言失败(data.dims <= 2 && type == CV_32F && K > 0)

这些非常简单要理解的规则,该函数仅在以下情况下才起作用:

  • mImage.depth()CV_32F

  • 如果 mImage.dims<= 2

  • 并且如果K > 0 。在本例中,您将 K 定义为 6

从您对问题的陈述来看,似乎:

IplImage* iplImage = cvLoadImage("C:/TestImages/rainbox_box.jpg");` 

默认情况下将图像加载为 IPL_DEPTH_8U 而不是 IPL_DEPTH_32F。这意味着 mImage 也是 IPL_DEPTH_8U,这就是您的代码无法正常工作的原因。

The error says all: Assertion failed (data.dims <= 2 && type == CV_32F && K > 0)

These are very simple rules to understand, the function will work only if:

  • mImage.depth() is CV_32F

  • if mImage.dims is <= 2

  • and if K > 0. In this case, you define K as 6.

From what you stated on the question, it seems that:

IplImage* iplImage = cvLoadImage("C:/TestImages/rainbox_box.jpg");` 

is loading the image as IPL_DEPTH_8U by default and not IPL_DEPTH_32F. This means that mImage is also IPL_DEPTH_8U, which is why your code is not working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文