开发者

Confusing errors with opencv cvLoadImage and netbeans

开发者 https://www.devze.com 2022-12-17 16:24 出处:网络
I found that handy opencv library and tried to program a simple imagemodifier with it using C. When I started to compile my code I got some errors, that are a bit confusing as they doesn\'t seem to ma

I found that handy opencv library and tried to program a simple imagemodifier with it using C. When I started to compile my code I got some errors, that are a bit confusing as they doesn't seem to make sense. This is my first time when programming with netbeans and first time I'm trying to make something else than those basic calculator etc. programs. Below is the code and the errors the compiler gives:

int iscolor = -1; /* Used to load the image as it is*/
IplImage* image = 0;

/* Load Image */
image = cvLoadImage(const char* 'test.jpg', iscolor); /* line 34 */

/*Modify image*/
...

/* Save Image */
int cvSaveImage(const char* 'modified.jpg', const CvArr* image); /* line 43 */

/*End*/

Compiler errors

main.c: In function ‘main’:

main.c:34: error: expected expression before ‘const’

main.c:34:37: warning: character 开发者_如何学运维constant too long for its type

main.c:34: error: too few arguments to function ‘cvLoadImage’

main.c:43:33: warning: character constant too long for its type

main.c:43: error: expected ‘;’, ‘,’ or ‘)’ before '\x2e6a7067'

These errors confuses me a bit, because

  1. referring to opencv c reference cvLoadImage takes just two arguments.

  2. how come character constant is too long?

  3. expected expression before 'const'? hummm... I don't get this at all

  4. expected ‘;’, ‘,’ or ‘)’ before '\x2e6a7067'... well I don't get this either

    So can somebody tell me what I'm doing wrong? Any help would be nice :)


In C, single quotes are used to denote single (literal) character constants, and the resulting type of such constants is int. So, 'a' is OK, 'a1' is not, because that's two characters. C defines some escape sequences, for example '\n', which is treated as one character signifying a newline, or '\xff', which is the character with hexadecimal value 0xff. Since 'test.jpg' contains more than one character, the compiler is telling you that your literal character constant is too long. You want a string, which needs double quotes.

You need to put parentheses around a cast: (const char *)x is okay, const char * x is not (well it is okay if you are declaring x as a const char *). I think you added that cast because the compiler was warning you about "converting int to pointer without a cast". In general, unless you really know what you're doing, casting to silence the compiler is a very bad idea.

Based upon the above, Line 34 should be:

image = cvLoadImage("test.jpg", iscolor);

If you really want to cast "test.jpg" as const char *, do:

image = cvLoadImage((const char *)"test.jpg", iscolor);

Also, now you know how to fix this line:

int cvSaveImage(const char* 'modified.jpg', const CvArr* image);


You should learn some c programming language, grab a book or something...

proper lines would be :

34: image = cvLoadImage('test.jpg', iscolor);

43: int result = cvSaveImage('modified.jpg', image);

0

精彩评论

暂无评论...
验证码 换一张
取 消