Using initial labels causes exception in OpenCV kmeans











up vote
0
down vote

favorite












I'm using OpenCV kmeans alghoritm to compute some kind of histogram.



In the first stage, I'm taking some feautres extracted from my train data, and I'm putting it into one of cv::kmeans functions params.



The output of cv::kmeans gives me two different outputs.




  • labels computed for my data

  • centers, calcualted during executing cv::kmeans


In the next stage, I would like to compute labels for my test data, but using some data computed in the previous stage, but if I set KMEANS_USE_INITIAL_LABELS flag and pass labels computed in previous stage it produces an exception with message:





error: (-215:Assertion failed) (unsigned)_labels.at(i) < (unsigned)K in function 'kmeans'





Here is my wrapper method for cv::kmeans



void Kmeans::fit_predict(cv::InputArray in, cv::InputOutputArray out)
{
// in is and input, my sample/s descriptors
// out -> computed labels; in first stage this array is empty

int flag = cv::KMEANS_PP_CENTERS;

if (!out.empty()) {
flag = cv::KMEANS_USE_INITIAL_LABELS;

cv::Mat m;

out.copyTo(m);


std::cout << m; // M has appropriate values, each value is < K(clusters amount)

}

cv::kmeans(in, _clusters_count, out, _term_criteria, _attempts, flag, _centers);

}


How I'm calling kmeans method(which is a part of my Kmeans class)



auto kmeans = std::make_unique<Kmeans>();

cv::Mat my_train_data .......initializing etc...
cv::Mat labels;

std::vector<Sample> test_samples ........initializng etc....


kmeans->fit_predict(my_train_data, labels);


for (auto& s : test_samples) {
cv::Mat test_labels = labels.clone();

kmeans->fit_predict(s->getDescriptors(), test_labels);

/// Process outputs etc...
}


Here are parameters with I'm calling cv::kmeans




  • in -> defined above


  • _clusters_count -> it is property of my class, it is 100


  • out -> defined above


  • _term_criteria -> stored as property of my class, it has value:
    cv::TermCriteria(cv::TermCriteria::MAX_ITER + cv::TermCriteria::EPS, 300, 0.0001)


  • flag -> _attempts -> property of my class, it is 20


  • _centers -> property of my class, it has default value: cv::noArray()



Why I'm getting this error?










share|improve this question






















  • Please, provide a Minimal, Complete, and Verifiable example (i.e. something that we can just compile without having to add anything) that will let us reproduce this problem. What version of OpenCV is this happening with?
    – Dan Mašek
    Nov 10 at 22:38















up vote
0
down vote

favorite












I'm using OpenCV kmeans alghoritm to compute some kind of histogram.



In the first stage, I'm taking some feautres extracted from my train data, and I'm putting it into one of cv::kmeans functions params.



The output of cv::kmeans gives me two different outputs.




  • labels computed for my data

  • centers, calcualted during executing cv::kmeans


In the next stage, I would like to compute labels for my test data, but using some data computed in the previous stage, but if I set KMEANS_USE_INITIAL_LABELS flag and pass labels computed in previous stage it produces an exception with message:





error: (-215:Assertion failed) (unsigned)_labels.at(i) < (unsigned)K in function 'kmeans'





Here is my wrapper method for cv::kmeans



void Kmeans::fit_predict(cv::InputArray in, cv::InputOutputArray out)
{
// in is and input, my sample/s descriptors
// out -> computed labels; in first stage this array is empty

int flag = cv::KMEANS_PP_CENTERS;

if (!out.empty()) {
flag = cv::KMEANS_USE_INITIAL_LABELS;

cv::Mat m;

out.copyTo(m);


std::cout << m; // M has appropriate values, each value is < K(clusters amount)

}

cv::kmeans(in, _clusters_count, out, _term_criteria, _attempts, flag, _centers);

}


How I'm calling kmeans method(which is a part of my Kmeans class)



auto kmeans = std::make_unique<Kmeans>();

cv::Mat my_train_data .......initializing etc...
cv::Mat labels;

std::vector<Sample> test_samples ........initializng etc....


kmeans->fit_predict(my_train_data, labels);


for (auto& s : test_samples) {
cv::Mat test_labels = labels.clone();

kmeans->fit_predict(s->getDescriptors(), test_labels);

/// Process outputs etc...
}


Here are parameters with I'm calling cv::kmeans




  • in -> defined above


  • _clusters_count -> it is property of my class, it is 100


  • out -> defined above


  • _term_criteria -> stored as property of my class, it has value:
    cv::TermCriteria(cv::TermCriteria::MAX_ITER + cv::TermCriteria::EPS, 300, 0.0001)


  • flag -> _attempts -> property of my class, it is 20


  • _centers -> property of my class, it has default value: cv::noArray()



Why I'm getting this error?










share|improve this question






















  • Please, provide a Minimal, Complete, and Verifiable example (i.e. something that we can just compile without having to add anything) that will let us reproduce this problem. What version of OpenCV is this happening with?
    – Dan Mašek
    Nov 10 at 22:38













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm using OpenCV kmeans alghoritm to compute some kind of histogram.



In the first stage, I'm taking some feautres extracted from my train data, and I'm putting it into one of cv::kmeans functions params.



The output of cv::kmeans gives me two different outputs.




  • labels computed for my data

  • centers, calcualted during executing cv::kmeans


In the next stage, I would like to compute labels for my test data, but using some data computed in the previous stage, but if I set KMEANS_USE_INITIAL_LABELS flag and pass labels computed in previous stage it produces an exception with message:





error: (-215:Assertion failed) (unsigned)_labels.at(i) < (unsigned)K in function 'kmeans'





Here is my wrapper method for cv::kmeans



void Kmeans::fit_predict(cv::InputArray in, cv::InputOutputArray out)
{
// in is and input, my sample/s descriptors
// out -> computed labels; in first stage this array is empty

int flag = cv::KMEANS_PP_CENTERS;

if (!out.empty()) {
flag = cv::KMEANS_USE_INITIAL_LABELS;

cv::Mat m;

out.copyTo(m);


std::cout << m; // M has appropriate values, each value is < K(clusters amount)

}

cv::kmeans(in, _clusters_count, out, _term_criteria, _attempts, flag, _centers);

}


How I'm calling kmeans method(which is a part of my Kmeans class)



auto kmeans = std::make_unique<Kmeans>();

cv::Mat my_train_data .......initializing etc...
cv::Mat labels;

std::vector<Sample> test_samples ........initializng etc....


kmeans->fit_predict(my_train_data, labels);


for (auto& s : test_samples) {
cv::Mat test_labels = labels.clone();

kmeans->fit_predict(s->getDescriptors(), test_labels);

/// Process outputs etc...
}


Here are parameters with I'm calling cv::kmeans




  • in -> defined above


  • _clusters_count -> it is property of my class, it is 100


  • out -> defined above


  • _term_criteria -> stored as property of my class, it has value:
    cv::TermCriteria(cv::TermCriteria::MAX_ITER + cv::TermCriteria::EPS, 300, 0.0001)


  • flag -> _attempts -> property of my class, it is 20


  • _centers -> property of my class, it has default value: cv::noArray()



Why I'm getting this error?










share|improve this question













I'm using OpenCV kmeans alghoritm to compute some kind of histogram.



In the first stage, I'm taking some feautres extracted from my train data, and I'm putting it into one of cv::kmeans functions params.



The output of cv::kmeans gives me two different outputs.




  • labels computed for my data

  • centers, calcualted during executing cv::kmeans


In the next stage, I would like to compute labels for my test data, but using some data computed in the previous stage, but if I set KMEANS_USE_INITIAL_LABELS flag and pass labels computed in previous stage it produces an exception with message:





error: (-215:Assertion failed) (unsigned)_labels.at(i) < (unsigned)K in function 'kmeans'





Here is my wrapper method for cv::kmeans



void Kmeans::fit_predict(cv::InputArray in, cv::InputOutputArray out)
{
// in is and input, my sample/s descriptors
// out -> computed labels; in first stage this array is empty

int flag = cv::KMEANS_PP_CENTERS;

if (!out.empty()) {
flag = cv::KMEANS_USE_INITIAL_LABELS;

cv::Mat m;

out.copyTo(m);


std::cout << m; // M has appropriate values, each value is < K(clusters amount)

}

cv::kmeans(in, _clusters_count, out, _term_criteria, _attempts, flag, _centers);

}


How I'm calling kmeans method(which is a part of my Kmeans class)



auto kmeans = std::make_unique<Kmeans>();

cv::Mat my_train_data .......initializing etc...
cv::Mat labels;

std::vector<Sample> test_samples ........initializng etc....


kmeans->fit_predict(my_train_data, labels);


for (auto& s : test_samples) {
cv::Mat test_labels = labels.clone();

kmeans->fit_predict(s->getDescriptors(), test_labels);

/// Process outputs etc...
}


Here are parameters with I'm calling cv::kmeans




  • in -> defined above


  • _clusters_count -> it is property of my class, it is 100


  • out -> defined above


  • _term_criteria -> stored as property of my class, it has value:
    cv::TermCriteria(cv::TermCriteria::MAX_ITER + cv::TermCriteria::EPS, 300, 0.0001)


  • flag -> _attempts -> property of my class, it is 20


  • _centers -> property of my class, it has default value: cv::noArray()



Why I'm getting this error?







c++ opencv machine-learning






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 22:05









bielu000

19415




19415












  • Please, provide a Minimal, Complete, and Verifiable example (i.e. something that we can just compile without having to add anything) that will let us reproduce this problem. What version of OpenCV is this happening with?
    – Dan Mašek
    Nov 10 at 22:38


















  • Please, provide a Minimal, Complete, and Verifiable example (i.e. something that we can just compile without having to add anything) that will let us reproduce this problem. What version of OpenCV is this happening with?
    – Dan Mašek
    Nov 10 at 22:38
















Please, provide a Minimal, Complete, and Verifiable example (i.e. something that we can just compile without having to add anything) that will let us reproduce this problem. What version of OpenCV is this happening with?
– Dan Mašek
Nov 10 at 22:38




Please, provide a Minimal, Complete, and Verifiable example (i.e. something that we can just compile without having to add anything) that will let us reproduce this problem. What version of OpenCV is this happening with?
– Dan Mašek
Nov 10 at 22:38

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243878%2fusing-initial-labels-causes-exception-in-opencv-kmeans%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243878%2fusing-initial-labels-causes-exception-in-opencv-kmeans%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python