V
V
vvafree2017-02-01 16:46:53
C++ / C#
vvafree, 2017-02-01 16:46:53

Image segmentation k-means OpenCV C++, why does an exception occur?

Constantly execution is interrupted by an exception at such coordinates.
File: mat.inl.hpp
Line: CV_DbgAssert((unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels()));
I tried not to pass the flow in grayscale, it did not give an effect.
The error specifically occurs when executing the kmeans () function, revealed empirically.
I can't understand. Everything is like documentation, all formats are observed, it should work.
Visual Studio 2015 Comm, OpenCV 3.1, Windows 10.

#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;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Stepanov, 2017-02-01
@koronabora

I usually take the library sources in such cases, add them to the project instead of .h + .lib and look with the debugger where the problem is inside.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question