Error in K-means algorithm - c

I use the following call to openCV function to perform K-means algorithm:
cvKMeans2(points, count, &clusters, cvTermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0 ), 1, CV_KMEANS_USE_INITIAL_LABELS, centers);
where
image2 = cvLoadImage( "lab.jpg", 0);
points = cvCreateMat( image2->height, image2->width, CV_32FC1 );
cvConvert(image2, points);
//count= number of clusters.
CvMat* centers; // To store the center of each cluster. (output).
lab.jpg is an image in CIE L*a*b* format.
But the above line, while compiling, shows the following errors:
`CV_KMEANS_USE_INITIAL_LABELS' undeclared (first use in this function)
too many arguments to function `cvKMeans2'
It would be very helpful if someone can point out where am wrong, specially the first error which says KMEANS_USE_INITIAL_LABELS undeclared.
Thanks in advance !

From the opencv doc for cvKMeans2:
flags – Can be 0 or CV_KMEANS_USE_INITIAL_LABELS.
You left out CV_.
Edit: also note that there should be two arguments between termcrit and flags, so you are skipping either attempts or rng. Try
cvKMeans2(points, count, &clusters, cvTermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0 ), 1, 0, CV_KMEANS_USE_INITIAL_LABELS, centers);

Related

cvWatershed unsupported format or combination of formats

I'm working with OpenCV 2.4.11 in C on Code::Blocks, in particular through the O'Reilly book Learning OpenCV. The section on the watershed algorithm was a bit short, so I thought I'd play with it a bit to see how exactly it works. However, every time I call the function I get the following error:
OpenCV Error: Unsupported format or combination of formats (Only 32-bit, 1-chann
el output images are supported) in cvWatershed
My program so far is very simple:
int main(int arg, int arg2) {
//open windows
cvNamedWindow("Input", 1 );
cvNamedWindow("Markings", 1 );
//load images
IplImage* input = cvLoadImage("ActualDoorPhoto.jpg", CV_LOAD_IMAGE_COLOR);
assert(input != NULL);
IplImage* markingstemp = cvLoadImage("ActualMarkingTest.jpg", CV_LOAD_IMAGE_COLOR);
assert(markingstemp != NULL);
//prepare markings
IplImage* markings = cvCreateImage(cvGetSize(markingstemp), 32, 1);
CvMat* markmat = cvCreateMat(input->width, input->height, CV_32FC1);
cvWatershed(input, markmat);
cvShowImage("Input", input);
cvShowImage("Markings", markings);
cvWaitKey(0);
return 0;
}
I have tried putting both markings and markmat as the second argument for cvWatershed, as well as several other things (notably markings with the contours of markingstemp drawn onto it), but every time I get the same error. Can anyone tell me what I'm doing wrong?
You're inverting the dimensions of the output matrix. It should be:
CvMat* markmat = cvCreateMat(input->height, input->width, CV_32FC1);
The format should also probably be changed to CV_32SC1.

stitching manually with opencv

Hi I am trying to stitch some images without using the stitch class provided by opencv. But, the output is quit unexpected.
I will explain it with the input and output image.
input1
input2
expected output
real output
I think something is wrong with my ROI copying. anybody please help !!!
My code for stitching part is as follows-
std::vector< Point2f > points1,points2;
for( int i = 0; i < matches1.size(); i++ )
{
points1.push_back( keypoints_input1[matches1[i].queryIdx ].pt );
points2.push_back( keypoints_input2[matches1[i].trainIdx ].pt );
}
/* Find the Homography Matrix for current and next frame*/
Mat H1 = findHomography( points2, points1, CV_RANSAC );
/* Use the Homography Matrix to warp the images*/
cv::Mat result1;
warpPerspective(input2, result1, H1, Size(input2.cols+150, input2.rows+150), INTER_CUBIC);
Mat stitch_1(Size(input2.cols+150, input2.rows+150),CV_8UC3);
Mat roi1(stitch_1, Rect(0, 0, input1.cols, input1.rows));
Mat roi2(stitch_1, Rect(0, 0, result1.cols, result1.rows));
input2.copyTo(roi1);
result1.copyTo(roi2);
Can anybody tell me where I am going wrong ? thanks.
Edit: input1(640,360) and input2(790,510) are of different size.
I hope that this example helps you.
It's interesting to test on different images.
EDIT:
try this code:
Mat stitch_1(Size(input2.cols*2+ input1.rows,input2.rows*2),CV_8UC3);
Mat roi1(stitch_1, Rect(0, 0, input1.cols, input1.rows));
Mat roi2(stitch_1, Rect(0, 0, result1.cols, result1.rows));
result1.copyTo(roi2);
input1.copyTo(roi1);
imshow("final", stitch_1);

Why I get a 1282 error after glLoadIdentity() is invoked?

CODE:
int err = glGetError(); // err = 0
glMatrixMode(GL_TEXTURE);
err = glGetError(); // err = 0
glLoadIdentity();
err = glGetError(); // err = 1282
GL_INVALID_OPERATION
The specified operation is not allowed in the current state. The offending command is ignored and has no other side effect than to set the error flag.
I use gDEBugger to check openGL's state when I get the error.
GL_CURRENT_RASTER_POSITION_VALID=TRUE
GL_CURRENT_RASTER_TEXTURE_COORDS=N/A
GL_CURRENT_TEXTURE_COORDS=N/A
GL_PROJECTION_STACK_DEPTH=1
GL_TEXTURE_1D=FALSE
GL_TEXTURE_2D=FALSE
GL_TEXTURE_GEN_Q=N/A
GL_TEXTURE_GEN_R=N/A
GL_TEXTURE_GEN_S=N/A
GL_TEXTURE_GEN_T=N/A
GL_TEXTURE_MATRIX=N/A
GL_TEXTURE_STACK_DEPTH=N/A
GL_TEXTURE_BINDING_1D=0
GL_TEXTURE_BINDING_2D=0
GL_TEXTURE_BINDING_3D=0
GL_TEXTURE_ENV_MODE=GL_MODULATE
GL_TEXTURE_ENV_COLOR={0, 0, 0, 0}
GL_ACTIVE_TEXTURE=GL_TEXTURE11
GL_CLIENT_ACTIVE_TEXTURE=GL_TEXTURE0
GL_MAX_TEXTURE_UNITS=4
GL_ACTIVE_TEXTURE_ARB=GL_TEXTURE11
GL_CLIENT_ACTIVE_TEXTURE_ARB=GL_TEXTURE0
GL_MAX_TEXTURE_UNITS_ARB=4
I can not figure out where is wrong. Why not the simply operation can not be allowed?
From the documentation:
GL_INVALID_OPERATION is generated if glLoadIdentity is executed
between the execution of glBegin and the corresponding execution of
glEnd.
I guess that it is why.
Immediate mode, along with matrix handling on the OpenGL side, is deprecated. You should be getting the same error even if you use glMatrixMode. Switching to OpenGL version 2.1 with wglCreateContextAttribsARB worked for me, although I've read comments saying it's not guaranteed that it will work in the future. You have to do your own matrix calculus and VBOs right from the start now.

SIFT Assertion Failed error

I'm trying to use SIFT to match two images and i'm using the code below:
cv::initModule_nonfree();
cv::Mat matFrame(frame);
cv::Mat matFrameAnt(frameAnterior);
cv::SiftFeatureDetector detector(400); //I've tried different values here
cv::SiftDescriptorExtractor extractor(400); //but i get always the same error
std::vector<cv::KeyPoint> keypoints1;
std::vector<cv::KeyPoint> keypoints2;
detector.detect( matFrame, keypoints1 );
detector.detect( matFrameAnt, keypoints2 );
cv::Mat feat1;
cv::Mat feat2;
cv::Mat descriptor1;
cv::Mat descriptor2;
extractor.compute( matFrame, keypoints1, descriptor1 );
extractor.compute( matFrameAnt, keypoints2, descriptor2 );
std::vector<cv::DMatch> matches;
cv::BFMatcher matcher(cv::NORM_L2, false);
matcher.match(descriptor1,descriptor2, matches);
cv::Mat result;
cv::drawMatches( matFrame, keypoints1, matFrameAnt, keypoints2, matches, result );
cv::namedWindow("SIFT", CV_WINDOW_AUTOSIZE );
cv::imshow("SIFT", result);
I get this error when i run the code (it compiles perfectly).
"OpenCV Error: Assertion failed (firstOctave >= -1 && actualNlayers <= nOctaveLayers) in unknown function, file ......\src\opencv\modules\nonfree\src\sift.cpp, line 755".
I understand that that function is getting a non positive value, so i printed all the possible values from my code and i found out that the size of my two keypoints vectors are -616431 and -616422.
The two images i'm using are black&white images, with a black blackground and my hand (white) in the middle of it.
What's happening? Am i using not valid images? Am i using the functions cv::SiftFeatureDetector and cv::SiftDescriptorExtractor wrong?
It seems you have no clue what you are doing. This feature is fairly undocumented, therefore try digging into the source, or let me tell you what you did.
cv::SiftFeatureDetector detector(50)
This means you will get at most 50 matches.
cv::SiftDescriptorExtractor extractor(400);
This means your magnification for extration is 400x. This parameter should be in the order of "1" for normal results.
The rest of the documentation is here: http://docs.opencv.org/2.3/modules/features2d/doc/common_interfaces_of_feature_detectors.html#SiftFeatureDetector

MagickWand for C: Lack of documentation means more errors for me

I am writing an improved Perlin noise (I don't really understand simplex noise) terrain generator for C, and I am practically finished with the alpha build. However, there is one thing holding me back: actually saving the stupid image. I recruited MagickWand to help me solve the problem of PNG creation, and it looks like a nice implementation on the whole, with tons of useful features etc., but there is very little documentation on the whole thing. No tutorials, really, just a bunch of lists of functions and some example programs. Here is my code so far, based on this:
EDIT: Cut out a bunch of irrelevant code.
#include <stdio.h>
#include <stdlib.h>
#include "mt.h"
#include "diamondsquare.h"
#include "/Library/Frameworks/libWand.framework/Versions/6.3.0/Headers/wand/MagickWand.h"
int main () {
unsigned long seed = 0, x = 0, y = 0, initial = 0, range = 0;
int smooth = 0, fail = 1, index1 = 0, index2 = 0, exception = 0;
char flagchar1 = 'n';
// Some imperative code. Not relevant.
image *ConstituteImage(x, y, "I", IntegerPixel, grid, &exception);
write("image.png", image);
}
At the very least, I know that this is linked wrong (compiling returns an error inside wand.h that it can't find one of the headers). What's the proper way to go about creating an image from an array within a program using MagickWand for C?
Too much code, it could be summarized with:
image *ConstituteImage(x, y, "I", IntegerPixel, grid, &exception);
write("image.png", image);
But reading the MagickWand link you provided:
MagickWriteImageFile
MagickWriteImageFile() writes an image to an open file descriptor. The
format of the MagickWriteImageFile method is:
MagickBooleanType MagickWriteImageFile ( MagickWand *wand, FILE *file
); A description of each parameter follows:
wand: The magick wand. file: The file descriptor.
So it is clear you have to call:
MagickBooleanType MagickWriteImageFile ( MagickWand *wand, FILE *file );
that header almost definitely tries to include other headers so you need something like:
gcc -I"/Library/Frameworks/libWand.framework/Versions/6.3.0/Headers"
or
gcc -I"/Library/Frameworks/libWand.framework/Versions/6.3.0/Headers/wand"

Resources