‘CvLoadImage’ was not declared in this scope - c++

I try to use OpenCV C Api in my code. I have opencv and opencv2 folder under usr/include. I can use OpenCV C++ Api. C++ code and compilation&linking commands are below :
#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/opencv.hpp"
using namespace cv;
int main()
{
Mat im = imread("Sobel.jpg");
return 0;
}
Compile : g++ -c main.cpp
Linking : g++ -o exe main.opkg-config --libs opencv`
Now I want to use OpenCV C-Api. My code is here :
#include <iostream>
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/opencv.hpp"
using namespace cv;
int main()
{
IplImage* pImg = CvLoadImage("Sobel.jpg");
if(pImg == NULL)
return -1;
// ... big bloat to do the same operations with IplImage
CvShowImage("Image", pImg);
cvWaitKey();
CvReleaseImage(&pImg); // Do not forget to release memory.
// end code here
return 0;
}
When I compile g++ -c main.cpp, the compiler says that ‘CvLoadImage’ was not declared in this scope

A simple spelling mistake, the function prototype is
IplImage* cvLoadImage( const char* filename, int iscolor=CV_LOAD_IMAGE_COLOR );

Related

cannot find permission denied opencv/lib

I wrote a very simple program with opencv
and I get this problem ( I'm using TDM-GCC x64 compiler)
cannot find c:/opencv/opencv2/build/x64/lib permission denied
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main (){
Mat image =imread("An0nymOu5.jpg");
namedWindow("image",cv::WINDOW_FREERATIO);
imshow("image",image);
waitKey(0);
}
Could be a compiler error; you can try in this link to find the toolchain
mingw-w64
Also assure the image file is near your program file.
try this aswell.
Mat image;
image = imread("An0nymOu5.jpg", CV_LOAD_IMAGE_COLOR);

Why Scalar does not work?

Following code results in build success, but no window. Without "m = Scalar(255,0,0);", it creates black window. Why including scalar does not work?
#include <iostream>
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace std;
int main() {
Mat m = Mat::zeros(200,200,CV_8UC3);
m = Scalar(255,0,0); //without this, it creates window.
imshow("m", m);
waitKey();
}
It will not give you any compilation error as Mat and Scalar are basically array types of OpenCV.
This is a possible duplicate of How to set all pixels of an OpenCV Mat to a specific value?
Your code have to work!
take a look at the doc:
C++: void imshow(const string& winname, InputArray mat)
and I can confirm you is working fine on my environment(opencv320, VisualStudio2017):
you need to clean the project and build again.

Compiling c++ code gives an error "‘nameWindow’ was not declared in this scope"

I am new in openCV. I have a c++ code like below.
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main(int argc, const char *argv[])
{
Mat img(480,640,CV_8UC3,Scalar(255,0,0));
if(img.empty())
{
cout<<"Picture can not load..."<<endl;
return -1;
}
nameWindow("test",CV_WINDOW_AUTOSIZE);
imshow("test",img);
waitKey(0);
destroyWindow("test");
return 0;
}
I try to compile this code in ubuntu 14.04. But when i do
g++ resimac.cpp
it gives an error:
error: ‘nameWindow’ was not declared in this scope
nameWindow("test",CV_WINDOW_AUTOSIZE);
^
What is the problem? How to solve it?
nameWindow("test",CV_WINDOW_AUTOSIZE);
You are missing the 'd'. The correct format should be
namedWindow("test",CV_WINDOW_AUTOSIZE);
http://docs.opencv.org/modules/highgui/doc/user_interface.html#namedwindow

Call functions in other files in C++ and OpenCV

I know this might be a possible duplicate but I have actually gone through several posts on SO to try and solve this problem
I have the following code in OpenCV and C++
test.cpp
#include "opencv2/core/core_c.h"
#include "opencv2/core/core.hpp"
#include "opencv2/flann/miniflann.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp"
#include <stdio.h>
#include <iostream>
#include <vector>
#include <math.h>
#include <string.h>
#include "draw_shapes.h"
using namespace cv;
using namespace std;
int main(){
Mat normal = imread("/INPUT 2.jpg");
Mat gray;
cvtColor(normal, gray,CV_RGB2GRAY);
Mat binary = gray > 128;
Point start = cv::Point(5.0,5.0);
Point end = cv::Point(200.0,200.0);
draw_line(binary, start, end, 0, 255, 0);
imshow("Draw_Line", binary);
while(waitKey(0)!=27){
;
}
return 0;
}
draw_shapes.cpp
#include "opencv2/core/core_c.h"
#include "opencv2/core/core.hpp"
#include "opencv2/flann/miniflann.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp"
#include <stdio.h>
#include <iostream>
#include <vector>
#include <math.h>
#include <string.h>
#include "draw_shapes.h"
void draw_line(cv::Mat img, cv::Point start, cv::Point end, int B, int G, int R){
int thickness = 2;
int lineType = 8;
line(img,start,end,Scalar(B,G,R),thickness,lineType);
}
void draw_rectangle(cv::Mat img, cv::Point p1, cv::Point p2, int B, int G, int R){
int thickness = 2;
int lineType = 8;
rectangle(img, p1, p2, Scalar(B,G,R),thickness, lineType);
}
draw_shapes.h
#ifndef DRAWSHAPES_H
#define DRAWSHAPES_H
void draw_line(cv::Mat, cv::Point, cv::Point, int, int, int);
void draw_rectangle(cv::Mat, cv::Point, cv::Point, int, int, int);
#endif
compile_opencv.sh
#!/bin/bash
echo "compiling $1"
if [[ $1 == *.c ]]
then
gcc -ggdb `pkg-config --cflags opencv` -o `basename $1 .c` $1 `pkg-config --libs opencv`;
elif [[ $1 == *.cpp ]]
then
g++ -ggdb `pkg-config --cflags opencv` -o `basename $1 .cpp` $1 `pkg-config --libs opencv`;
else
echo "Please compile only .c or .cpp files"
fi
echo "Output file => ${1%.*}"
./${1%.*}
I have gone through almost every post on StackOverflow related to header files and including functions from external CPP files and this is what I got.
Now, when I compile, I get
undefined reference to 'draw_line(cv::Mat, cv::Point_<int>, cv::Point_<int>, int, int, int)'
I am taking a wild guess and saying that this might be because I am not compiling draw_shapes.cpp separately, but I tried a simple int add(int x, int y) in a C file and it worked directly. Where am I going wrong here?
(Based on Ben's comment)
This command should work:
g++ -ggdb `pkg-config --cflags opencv` test.cpp draw_shapes.cpp `pkg-config --libs opencv`
Note the backquotes around "pkg-config --cflags opencv" and "pkg-config --libs opencv". Reqular quotes ('), won't work - you really need backquotes here (`). The backquote key is just below the "Esc" key on most keyboards. Or just copy-paste them from your script.
Just as #Ben mentioned, You need to compile the draw_shapes.cpp file. The twist is, if You want to compile Your files separately You need to compile them as objects with -c switch, and then link them together. Further explanation in Your second thread

Clang compiling C++ to object file, Error: unable to interface with target machine

I'm attempting to use clang programatically to compile some C++ to an object file and I get the error "unable to interface with target machine" during the codegenaction. I'm wondering if I have missed some step or otherwise what the problem could be.
Below is the output of the program:
C:\Application\Debug>Application
Initializing targets
Targets initialized
clang -cc1 version 3.4 based upon LLVM 3.4svn default target i686-pc-win32
#include "..." search starts here:
#include <...> search starts here:
c:\mingw\lib\gcc\mingw32\4.7.2\include\c++
c:\mingw\lib\gcc\mingw32\4.7.2\include\c++\mingw32
c:\mingw\lib\gcc\mingw32\4.7.2\include\c++\backward
c:\mingw\lib\gcc\mingw32\4.7.2\include
c:\mingw\include
c:\mingw\lib\gcc\mingw32\4.7.2\include-fixed
End of search list.
error: unable to interface with target machine
1 error generated.
Here is my (very simple) program:
#include <string>
#include <memory>
#include <iostream>
#include <limits.h>
#include <llvm/Bitcode/ReaderWriter.h>
#include "clang/CodeGen/CodeGenAction.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Driver/Compilation.h"
#include "clang/Driver/Driver.h"
#include "clang/Driver/Tool.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/CompilerInvocation.h"
#include "clang/Frontend/FrontendDiagnostic.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/JIT.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/Support/system_error.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include <Windows.h>
using namespace std;
using namespace llvm;
using namespace clang;
using namespace clang::driver;
int main(int argc, const char **argv, char * const *envp) {
llvm::InitializeNativeTarget();
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
TextDiagnosticPrinter *DiagClient =
new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient);
driver::ArgStringList CCArgs;
CCArgs.push_back("-cc1");
CCArgs.push_back("-triple");
CCArgs.push_back("i686-pc-win32");
CCArgs.push_back("-emit-obj");
CCArgs.push_back("-O3");
CCArgs.push_back("-nostdsysteminc");
CCArgs.push_back("-mrelax-all");
CCArgs.push_back("-disable-free");
CCArgs.push_back("-v");
CCArgs.push_back("-coverage-file");
CCArgs.push_back("test.o");
CCArgs.push_back("-main-file-name");
CCArgs.push_back("test.cpp");
CCArgs.push_back("-mrelocation-model");
CCArgs.push_back("static");
CCArgs.push_back("-mdisable-fp-elim");
CCArgs.push_back("-fmath-errno");
CCArgs.push_back("-masm-verbose");
CCArgs.push_back("-mconstructor-aliases");
CCArgs.push_back("-target-cpu");
CCArgs.push_back("pentium4");
CCArgs.push_back("-internal-isystem");
CCArgs.push_back("c:\\mingw\\lib\\gcc\\mingw32\\4.7.2\\include\\c++");
CCArgs.push_back("-internal-isystem");
CCArgs.push_back("c:\\mingw\\lib\\gcc\\mingw32\\4.7.2\\include\\c++\\mingw32");
CCArgs.push_back("-internal-isystem");
CCArgs.push_back("c:\\mingw\\lib\\gcc\\mingw32\\4.7.2\\include\\c++\\backward");
CCArgs.push_back("-internal-isystem");
CCArgs.push_back("c:\\mingw\\lib\\gcc\\mingw32\\4.7.2\\include");
CCArgs.push_back("-internal-isystem");
CCArgs.push_back("c:\\mingw\\include");
CCArgs.push_back("-internal-isystem");
CCArgs.push_back("c:\\mingw\\lib\\gcc\\mingw32\\4.7.2\\include-fixed");
CCArgs.push_back("-std=c++11");
CCArgs.push_back("-fdeprecated-macro");
CCArgs.push_back("-ferror-limit");
CCArgs.push_back("19");
CCArgs.push_back("-fmessage-length");
CCArgs.push_back("150");
CCArgs.push_back("-mstackrealign");
CCArgs.push_back("-fms-extensions");
CCArgs.push_back("-fmsc-version=1300");
CCArgs.push_back("-fobjc-runtime=gcc");
CCArgs.push_back("-fobjc-default-synthesize-properties");
CCArgs.push_back("-fcxx-exceptions");
CCArgs.push_back("-fexceptions");
CCArgs.push_back("-fdiagnostics-show-option");
CCArgs.push_back("-fcolor-diagnostics");
CCArgs.push_back("-backend-option");
CCArgs.push_back("-vectorize-loops");
CCArgs.push_back("-x");
CCArgs.push_back("c++");
CCArgs.push_back("-o");
CCArgs.push_back("test.o");
CCArgs.push_back("test.cpp");
OwningPtr<CompilerInvocation> CI(new CompilerInvocation);
CompilerInvocation::CreateFromArgs(*CI,
const_cast<const char **>(CCArgs.data()),
const_cast<const char **>(CCArgs.data()) +
CCArgs.size(),
Diags);
// Create a compiler instance to handle the actual work.
CompilerInstance Clang;
Clang.setInvocation(CI.take());
// Create the compilers actual diagnostics engine.
Clang.createDiagnostics();
if (!Clang.hasDiagnostics())
return 1;
OwningPtr<CodeGenAction> Act(new EmitObjAction());
if (!Clang.ExecuteAction(*Act))
return 1;
return 0;
}