There's an article describing how to do this here, that seems to have worked for other people but it does not compile for me.
Here's a copy of the .h file that was used:
//
// NSImage+OpenCV.h
//
#import <AppKit/AppKit.h>
#interface NSImage (NSImage_OpenCV) {
}
+(NSImage*)imageWithCVMat:(const cv::Mat&)cvMat;
-(id)initWithCVMat:(const cv::Mat&)cvMat;
#property(nonatomic, readonly) cv::Mat CVMat;
#property(nonatomic, readonly) cv::Mat CVGrayscaleMat;
#end
I'm on Xcode 4.4, using openCV 2.4.2. The compiler errors I'm getting for the header file are 4x of the following:
Semantic issue: Use of undeclared identifier 'cv'
This error seems rather obvious...the header file does not define what a cv::Mat is.
So I took a guess from looking at the OpenCV 2.4 tutorial, that I needed to add
#include <opencv2/core/core.hpp>
This generated 20 other errors, where the compiler was complaining about the core.hpp file.
The first of which says:
Semantic issue: Non-const static data member must be initialized out of line
So my question is what am I doing wrong? How do I get this code to work?
Another stackoverflow Q&A (link: How to include OpenCV in Cocoa Application?) had the missing piece on the undefined symbols.
To summarize:
The OpenCV headers have to be included before the Cocoa headers, due to a macro conflict.
Xcode automatically includes the cocoa headers at the top of every objective-C file (*.m, *.mm), which prevents you from adding the OpenCV headers first, as stated in point 1.
Xcode has a "-Prefix.pch" file that defines any code which is automatically prepended to any code.
Normally, it looks like:
#ifdef __OBJC__
#import <Cocoa/Cocoa.h>
#endif
Instead, the file should be changed to look like:
#ifdef __cplusplus
#import <opencv2/opencv.hpp>
#endif
#ifdef __OBJC__
#import <Cocoa/Cocoa.h>
#endif
So in the case of any *.cpp file, the opencv header gets added automatically. In the case of any *.m file, the Cocoa headers are added. And in the special case of *.mm files, they BOTH get added, and in the correct order.
Kudos to Ian Charnas and user671435 for figuring this out.
That is C++ syntax. You have to give your source file the .mm suffix to make it Objective-C++.
Related
I'm working right out of a book, and copied the following code (essentially the openCV equivalent of "hello world"):
//helloCV.cpp
#include <opencv2/opencv.hpp>
int main(int argc, char** argv){
cv::Mat img = cv::imread(argv[1], -1);
if (img.empty()) return -1;
cv::namedWindow("Example1", cv::WINDOW_AUTOSIZE);
cv::imshow("Example1", img);
cv::waitKey(0);
cv::destroyWindow("Example1");
return 0;
}//main
Unfortunately, when I run this code, I get a window with the header and nothing in it:
I suspect that I've messed up the environment or some such when installing OpenCV, but cmake is throwing no errors, and the code runs as expected, exiting correctly on a keystroke and all of that, with the glaring exception of a lack of a displayed photo.
Any tips?
Thanks!
Thanks to #DanMašek for the lead on this one, and to all the people on this page: http://answers.opencv.org/question/160201/video-window-not-loading-frame/
To repeat what they said, what worked for me was the following:
To resolve this, locate the file window_cocoa.mm; if built from source it'll be in opencv/modules/highgui/src.
Change the following:
#implementation CVView
#if defined(__LP64__)
#synthesize image;
#else // 32-bit Obj-C does not have automatic synthesize
#synthesize image = _image;
#endif
To this:
#implementation CVView
#synthesize image = _image;
Do the same thing for the CVWindow and CVSlider implementations to accommodate videos as well.
Recompile OpenCV and test out your code.
Hope this helps other people struggling with this issue!
I've got existing code (OS X, Obj-C, NSOpenGLView) that calls:
gluOrtho2D(0.0, newSize.width, newSize.height, 0.0);
It works fine except that I get a deprecated function warning that urges me to use GLKMatrix4MakeOrtho() instead. OK – but how? I can't seem to even find the existence of that function; I'm including:
#import <OpenGL/gl.h>
#import <OpenGL/glu.h>
and Xcode does not know of a function by that name. My OpenGL reference manual does not have any mention of it, or indeed, of any functions with the prefix GLK; what's going on there? And then if I managed to find the function and include the right header and link against whatever I need to link against, what then – what would an equivalent call be for GLKMatrix4MakeOrtho() that would do the same thing as my gluOrtho2D() call? I tried Googling, and found many hits showing that other people are getting the same deprecation warning, but I couldn't find anybody saying how to fix it...
The include you need for GLKMatrix4MakeOrtho() is:
#include <GLKit/GLKMatrix4.h>
Then you call the function with the same arguments you would use with glOrtho(), e.g.:
GLKMatrix4 orthoMat = GLKMatrix4MakeOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
How you use it depends on the kind of OpenGL you use. For fixed function with the legacy matrix stack, you can use for example:
glLoadMatrix(orthoMat.m);
With the programmable pipeline, you would typically load it as a uniform:
glUniformMatrix4fv(loc, 1, GL_FALSE, orthoMat.m);
Wow, that's a really unusual recommendation. GLKMatrix4MakeOrtho() is not a drop-in replacement for gluOrtho2D(); it's a function that would be used here if you were porting your application to OpenGL 3.2 or later. However, that port would be a much bigger task than just changing out this one call, as OpenGL 3.2 does not support any of the immediate mode APIs from earlier versions of OpenGL (e.g, glBegin()).
Bottom line is, so long as you continue to use OpenGL 1.x/2.x APIs, you will need to ignore this warning.
Here is one alternative to disable OpenGL deprecation warnings on macOS 10.15 and Xcode 11.2.1:
main.c
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#include <OpenGL/gl.h>
#include <GLUT/glut.h>
Another alternative would be via Build Settings:
I have a question and after couple hours searching for the answer I could not find. I am using visual studio to do AVR development. When I use only one file with all my functions and include everything works : Keyword define in my include files like, PINB, or PORTB4 are highlighted in pink, enum in turquoise blue and so long and so forth. But when I write my own .h and .c nothing seems to work properly. In my .h all the colors are correctly displayed, but this file contain hardly any code beside some define and function prototype.
In my .c file that goes along with the .h only words like, int, char, unsigned and long are colored blue like they should. is there a way to get my color highlighted in my library .c file?
Thanks you all
M.
I found the problem. I needed to add every .c files associated with my .h in the Source Files folder of my project!
Took me some time, but figured it out!
Hey i have searched every where about this issue and i still cant find my answers. Here's the deal, I'm a beginner and for my semester project i need to use the OpenCV. During its installation I'm getting two problems:
1.) the property pages that I'm supposed to make and save for further
use,i cant save them. The option is grayed out, i did everything
written here:
http://docs.opencv.org/doc/tutorials/introduction/windows_install/windows_install.html#windows-installation
2.) the headers im including i.e
#include <opencv2/imgproc/imgproc.hpp> // Gaussian Blur
#include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp>
give errors, fatal error C1083: Cannot open include file: 'opencv2/imgproc/imgproc.hpp': No such file or directory what am i doing wrong??
you probably need to adjust your "Additional Include Dirs", so that it points to
opencv/build/include
I am working on a program using the OpenCV library (though I am quite a noob on it). One of the things I need to do is to draw on the image. I looked at the OpenCV drawing functions and they all seem pretty simple (Circle, Line, etc), however the program won't compile! It says this to be exact: error C3861: 'Line': identifier not found.
Is there something I haven't installed? I used the tutorial on http://opencv.willowgarage.com/wiki/VisualC%2B%2B_VS2008 to install OpenCV on Visual Studio 2008 and so far this is the only real problem I have.
Please help me! I need this program working as soon as possible!
The function to draw a line in the OpenCV C API is named cvLine, not Line.
I think you have fallen victim of the following common mistake:
C includes are in #include <opencv/core.h> etc, whereas
C++ includes are:
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <oppencv2/highgui/highgui.hpp>
Include these for drawing and showing the image. Use using namespace cv; then
you don't have to write cv::line just line and everything will be working fine.
I had to battle with the very same problem when I began. ;)
(And btw use cv::Mat for c++.)
You can now easily paint on OpenCV images. For this you need to call the setMouseCallback(‘window_name’,image_name) function on opencv. After that you can easily handle the Mouse Callback Function upon your images. Then you need to detect the cv2.EVENT_LBUTTONDOWN, cv2.EVENT_MOUSEMOVE and cv2.EVENT_LBUTTONUP events. By checking the proper boolean condition you need to decide how you like to interact with the OpenCV images.
def paint_draw(event,former_x,former_y,flags,param):
global current_former_x,current_former_y,drawing, mode
if event==cv2.EVENT_LBUTTONDOWN:
drawing=True
current_former_x,current_former_y=former_x,former_y
elif event==cv2.EVENT_MOUSEMOVE:
if drawing==True:
if mode==True:
cv2.line(image,(current_former_x,current_former_y),(former_x,former_y),(0,0,255),5)
current_former_x = former_x
current_former_y = former_y
elif event==cv2.EVENT_LBUTTONUP:
drawing=False
if mode==True:
cv2.line(image,(current_former_x,current_former_y),(former_x,former_y),(0,0,255),5)
current_former_x = former_x
current_former_y = former_y
return former_x,former_y
For details you can see link: How to Paint on OpenCV Images and Save the Image
Output: