OpenCV Code Sample Error - c

I have written the following Code Sample that is supposed to create a peculiar-looking gradient and save it to a file specified by the user:
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
static IplImage *image = 0;
void main() {
char path[1024];
int x, y;
CvScalar scalar;
scanf("%s", path);
image = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
for (y = 0; y < 480; y++) {
for (x = 0; x < 640; x++) {
scalar = cvGet2D(image, x, y);
scalar.val[0]=(unsigned char)(x + y);
cvSet2D(image, x, y, scalar);
}
}
cvSaveImage(path, image, 0);
}
I compile it using: gcc opencv.c -o opencv `pkg-config --libs --cflags opencv` -lm and everything seems to be OK. However, during runtime (input: "sample.png"), I get the following error:
OpenCV Error: One of arguments' values is out of range (index is out of range) in cvPtr2D, file /builddir/build/BUILD/OpenCV-2.3.1/modules/core/src/array.cpp, line 1797
terminate called after throwing an instance of 'cv::Exception'
what(): /builddir/build/BUILD/OpenCV-2.3.1/modules/core/src/array.cpp:1797: error: (-211) index is out of range in function cvPtr2D
Aborted (core dumped)
Any help, please? Thanks in Advance! :)

cvGet2D and cvSet2D uses [row,column] convention like many other functions in opencv.
For further readings:
http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/

The cvGet2D and cvSet2D function take the row as the first argument, and the column as the second argument: you have the x and y arguments the wrong way round. Hence you are going outside the image. The call should be:
cvGet2D(image, y, x);

Related

Having link error and not sure how to solve it?

#include <stdio.h>
#include <stdbool.h>
int SmallerOf(int x, int y);
void TestCase(void)
{
int x, y, actual;
x = 3;
y = 4;
actual = 3;
int expected = SmallerOf(x, y);
if (actual == expected)
{
printf("PASS");
}
else
{
printf("FAIL");
}
}
Having an error with linker having a linker error message 2019. I tried to see if my linker under properties looking at something different or a certain file. It seems like everything is good and I a just look. What could be the cause of this when I haven't touched the linker directories? thank you
I have looked at properties and it seems to be fine and not sure what this error message means.
This is a (forward) declaration of a function:
int SmallerOf(int x, int y);
It makes a promise to the compiler that there will be a definition of the function later, so the compiler will not complain when you call it in your code - even if the compiler have even not seen the actual function definition yet.
When linking your final program, it will however not find this definition, hence you get a linker error. In order to fix the problem, provide a definition of the function too. Example:
int SmallerOf(int x, int y) {
return x < y ? x : y;
}
Your final program also needs a definition of the int main(void) or int main(int, char**) function in order to know where to start execution of the program.

C: undefined reference to 'WinMain#16'

I'm very new to coding and I have been trying to write code to adds two integers. But whenever I try to run it using 'gcc addition.c' in the terminal I always reports an error. I tried reinstalling the compiler i.e Mingw several times but the problem does not gets fixed.
(I m currently doing C language on VS CODE software, when you answer to my issue please use layman language)
#include <stdio.h>
int main() {
int x=1;
int y=2;
int z=0;
z=x+y;
printf("%d", z);
return 0;
}
Windows PowerShell
PS D:\C tutorials> gcc addition.c
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to "WinMain#16' collect.exe: error: ld returned 1 exit status
I added a \n to clean up the printf().
#include <stdio.h>
int main(void) {
int x=1;
int y=2;
int z=0;
z=x+y;
printf("%d\n", z);
return 0;
}
% gcc -o addition addition.c -lc ; ./addition
3
You needed to include the C library, represented by the -lc in the gcc line.

problems compiling xmlrpc-c program

I'm trying out the examples in the xmlrpc-c documentation:
#include <stdio.h>
#include <xmlrpc.h>
#include <xmlrpc_server.h>
//#include <xmlrpc_server_abyss.h>
#include <xmlrpc_abyss.h>
#include <xmlrpc-c/base.h>
#include <xmlrpc-c/util.h>
static xmlrpc_value *
sample_add(xmlrpc_env * const envP,
xmlrpc_value * const paramArrayP,
void * const serverContext) {
xmlrpc_int32 x, y, z;
/* Parse our argument array. */
xmlrpc_decompose_value(envP, paramArrayP, "(ii)", &x, &y);
if (envP->fault_occurred)
return NULL;
/* Add our two numbers. */
z = x + y;
/* Return our result. */
return xmlrpc_build_value(envP, "i", z);
}
int
main (int const argc,
const char ** const argv) {
xmlrpc_server_abyss_parms serverparm;
xmlrpc_registry * registryP;
xmlrpc_env env;
xmlrpc_env_init(&env);
registryP = xmlrpc_registry_new(&env);
xmlrpc_registry_add_method(
&env, registryP, NULL, "sample.add", &sample_add, NULL);
serverparm.config_file_name = argv[1];
serverparm.registryP = registryP;
printf("Starting XML-RPC server...\n");
xmlrpc_server_abyss(&env, &serverparm, XMLRPC_APSIZE(registryP));
return 0;
}
I try to compile using gcc:
gcc source.c
nohting fancy and I get:
/tmp/ccfGuc6A.o: In function sample_add':
source.c:(.text+0x38): undefined reference toxmlrpc_decompose_value'
source.c:(.text+0x6d): undefined reference to xmlrpc_build_value'
/tmp/ccfGuc6A.o: In functionmain':
source.c:(.text+0x96): undefined reference to xmlrpc_env_init'
source.c:(.text+0xa5): undefined reference toxmlrpc_registry_new'
source.c:(.text+0xd8): undefined reference to xmlrpc_registry_add_method'
source.c:(.text+0x117): undefined reference toxmlrpc_server_abyss'
collect2: error: ld returned 1 exit status
these functions exist in the:
/usr/include/xmlrpc-c/base.h
whihc I have referenced:
include
I think I'm not passing the right options to link, I don't know how it's done though.
thanks
You definitely don't pass the correct argument for the linker. Just including a header file doesn't actually make the linker link with the library, you need to use the -l (lower-case L) option to tell the linker which libraries you need to link with, like
gcc source.c -lxmlrpc
I believe that xml-rpc-c comes with a helper program, intended to help you get the linking right. Its documented here
http://xmlrpc-c.sourceforge.net/doc/xmlrpc-c-config.html

C programming - "Undefined symbol referenced in file"

I am trying to write a program to approximate pi. It basically takes random points between 0.00 and 1.00 and compares them to the bound of a circle, and the ratio of points inside the circle to total points should approach pi (A very quick explanation, the specification goes in depth much more).
However, I am getting the following error when compiling with gcc:
Undefined first referenced
symbol in file
pow /var/tmp//cc6gSbfE.o
ld: fatal: symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
What is happening with this? I've never seen this error before, and I don't know why it's coming up. Here is my code (though I haven't fully tested it since I can't get past the error):
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void) {
float x, y;
float coordSquared;
float coordRoot;
float ratio;
int n;
int count;
int i;
printf("Enter number of points: ");
scanf("%d", &n);
srand(time(0));
for (i = 0; i < n; i++) {
x = rand();
y = rand();
coordSquared = pow(x, 2) + pow(y, 2);
coordRoot = pow(coordSquared, 0.5);
if ((x < coordRoot) && (y < coordRoot)) {
count++;
}
}
ratio = count / n;
ratio = ratio * 4;
printf("Pi is approximately %f", ratio);
return 0;
}
use -lm during compilation(or linking) to include math library.
Like this: gcc yourFile.c -o yourfile -lm
need to Link with -lm.
gcc test.c -o test -lm
The error is produced by the linker, ld. It is telling you that the symbol pow cannot be found (is undefined in all the object files handled by the linker). The solution is to include the library which includes the implementation of the pow() function, libm (m for math). [1] Add the -lm switch to your compiler command line invocation (after all the source file specifications) to do so, e.g.
gcc -o a.out source.c -lm
[1] Alternatively, you could have your own implementation of pow() in a separate translation unit or a library, but you would still have to tell the compiler/linker where to find it.

Why won't this small function(drawing a circle in opengl) compile in c?

I'm doing some experiments with opengl in c for linux. I've got the following function that would draw a circle given those parameters. I've included
#include <stdlib.h>
#include <math.h>
#include <GL/gl.h>
#include <GL/glut.h>
However when I compile:
gcc fiver.c -o fiver -lglut
I get:
/usr/bin/ld: /tmp/ccGdx4hW.o: undefined reference to symbol 'sin##GLIBC_2.2.5'
/usr/bin/ld: note: 'sin##GLIBC_2.2.5' is defined in DSO /lib64/libm.so.6 so try
adding it to the linker command line
/lib64/libm.so.6: could not read symbols: Invalid operation
collect2: ld returned 1 exit status
The function is the following:
void drawCircle (int xc, int yc, int rad) {
//
// draw a circle centered at (xc,yc) with radius rad
//
glBegin(GL_LINE_LOOP);
//
int angle;
for(angle = 0; angle < 365; angle = angle+5) {
double angle_radians = angle * (float)3.14159 / (float)180;
float x = xc + rad * (float)cos(angle_radians);
float y = yc + rad * (float)sin(angle_radians);
glVertex3f(x,0,y);
}
glEnd();
}
Does anyone know what's wrong?
The linker cannot find the definition of sin() function. You need to link your application against the math library. Compile with:
gcc fiver.c -o fiver -lglut -lm

Resources