C++ - SiftFeatureDetector is not loading - c++

I am currently trying to use OpenCV's SiftFeatureDetector. However, this happens:
Click This Link For Image
I saw that I needed the nonfree files. This is where i got my files from:
features2d.hpp:
sourceforge.net/p/emgucv/opencv/ci/3ad471d9c187b6509ca4aab439290bc76c7a258f/tree/modules/nonfree/include/opencv2/nonfree/features2d.hpp
nonfree.hpp:
sourceforge.net/p/emgucv/opencv/ci/3ad471d9c187b6509ca4aab439290bc76c7a258f/tree/modules/nonfree/include/opencv2/nonfree/nonfree.hpp
These are my imports:
Click This Link For Image
Can somebody please tell me what's the problem?

You are getting files from EmguCV site. EmguCV is a wrapper around OpenCV developed for .net framework. If you are going to use OpenCV with c++ then you should use OpenCV library and header files downloadable from: http://opencv.org.
It seems that SIFT moved out of default install of OpenCV from version 3.0. So the answer depends on the version of OpenCV you are using.
NOTE: If you don't want to be bothered with building OpenCV yourself, you should consider using 2.4.13.2, or version less than 3.0.
Tested on Windows 10 with Visual Studio 2015.
OpenCV 2.4.13.2
You can download prebuilt library from this link. The prebuilt library includes SIFT. (In my case, since I'm on Windows, I've used opencv-2.4.13.2-vc14.exe) You can use SIFT like the following code.
#include <opencv2/opencv.hpp>
#include <opencv2/nonfree/features2d.hpp>
int main(int argc, const char* argv[])
{
const cv::Mat input = cv::imread("D:/lenna.png", 0); //Load as grayscale
// Detect
cv::SiftFeatureDetector detector;
std::vector<cv::KeyPoint> keypoints;
detector.detect(input, keypoints);
// Add results to image and save.
cv::Mat output;
cv::drawKeypoints(input, keypoints, output);
cv::imwrite("D:/lenna_sift.jpg", output);
return 0;
}
original image / feature detected image
(copied this minimal example from here)
OpenCV 3.2
You can download prebuilt library for this version from this link, but this version doesn't include SIFT as a default installed feature. (I've checked opencv-3.2.0-vc14.exe and it doesn't have it) It seems that the OpenCV community decided to remove patented algorithms like SIFT and SURF from default install starting from version 3.0(link). To use these, you have to download the opencv and opencv_contrib source code and build the library yourself. You have to configure your build settings so you can build with SIFT enabled. Then you can do SIFT like follows.
#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>
int main(int argc, const char* argv[])
{
const cv::Mat input = cv::imread("D:/lenna.png", 0); //Load as grayscale
// Detect
cv::Ptr<cv::Feature2D> f2d = cv::xfeatures2d::SiftFeatureDetector::create();
std::vector<cv::KeyPoint> keypoints;
f2d->detect(input, keypoints);
// Add results to image and save.
cv::Mat output;
cv::drawKeypoints(input, keypoints, output);
cv::imwrite("D:/lenna_sift.jpg", output);
return 0;
}
original image / feature detected image
I expect using SIFT with other versions before 3.0 should be same as 2.4.13.2, and version including and after 3.0 to be same as 3.2. If it's not, please let me know so I can improve this post.

Related

UE5 OpenCV: Can’t grab VideoCapture

I’m trying to read camera input using the built-in OpenCV library in UE5 but no matter what I do, I can’t seem to make cv::VideoCapture.read() return anything. cv::VideoCapture.grab() also returns false every time.
It works fine on the same machine with regular C++ with OpenCV 4.6.0 and the VideoCapture is definitely open and the camera turns on as expected.
Is there something about Unreal’s built-in implementation I need to know about? (Other than it used OpenCV 4.5.5). I can’t seem to find any info online about this.
This is what my header files look like:
#pragma once
#include "PreOpenCVHeaders.h"
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/imgproc.hpp>
#include "PostOpenCVHeaders.h"
#include "CameraReader.generated.h"
And my Plugin’s Build file:
PublicDependencyModuleNames.AddRange(
new string[]
{
"Core",
"OpenCVHelper",
"OpenCV",
// ... add other public dependencies that you statically link with here ...
}
);
Using Windows 11 with Kinect 2.0.
I thought maybe I was missing some OpenCV DLLs since I can only find a custom Unreal version of the OpenCV World DLL, so I tried overriding the default OpenCV plugin and adding the FFMPEG and MSMF DLLs myself but that didn’t change anything.
Edit: After many attempted building OpenCV from source, I only managed to get GStreamer and FFMPEG to work.
The OpenCV library that comes with UE5 is not built with FFmpeg or with the Media Foundation library, so it cannot read video from a file or from a camera.
You can try to build your own version of OpenCV with FFmpeg or Media Foundation support and use that instead of the one that comes with UE5.

opencv program compiled, but it does not execute ( c++ )

I found a below program in c++ on internet,
#include <opencv2/opencv.hpp>
using namespace cv;
int main(void) {
// Read image in GrayScale mode
Mat image = imread("emily.jpg", 0);
// Save grayscale image
imwrite("emilyGray.jpg", image);
// To display the image
imshow("Grayscale Image", image);
waitKey(0);
return 0;
}
Then I compiled the above program with the following,
g++ opencv1.cpp -o opencv1.exe -IC:\\opencv\\build\\install\\include -LC:\\opencv\\build\\install\\x64\\mingw\\lib -lopencv_calib3d452 -lopencv_core452 -lopencv_highgui452 -lopencv_imgcodecs452
After running opencv1.exe, theres no change, no image being created.
Note:- I am using mingw for compiling and my opencv is built with mingw and cmake.
Actually I did not set the path to opencv dlls after I built opencv using mingw32-make.
After setting C:\opencv\build\bin to PATH variable of environment variables I could see the output.
Regards

cv has no member BackgroundSubtractorMOG

I am new to opencv and followed instructions to install it as described here:
http://docs.opencv.org/doc/tutorials/introduction/windows_install/windows_install.html#windows-installation
I used the section "Installation by Making Your Own Libraries from the Source Files", which worked well (using Visual Studio 2013). I am able to run basic commands, like read an image, write an image, run edge detection, video processing etc.
But now I tried to use BackgroundSubtractorMOG and I get the error that BackgroundSubtractorMOG is not a member of cv. The simplest code is below and I don't know where to start. Am I missing something in my installation? Any ideas?
#include "stdafx.h"
#include<opencv2/opencv.hpp>
int main()
{
cv::BackgroundSubtractorMOG bg;
return 0;
}
with opencv3.0, BackgroundSubtractorMOG was moved to the opencv_contrib repo
to use the remaining BackgroundSubtractorMOG2 or BackgroundSubtractorKNN you'd have to use:
Ptr<BackgroundSubtractorMOG2> bgm = createBackgroundSubtractorMOG2(...);
you forgot to include the header
#include <background_segm.hpp>
Reference: http://physics.nyu.edu/grierlab/manuals/opencv/classcv_1_1BackgroundSubtractorMOG.html
path to the header file could be: /opencv2/video/background_segm.hpp

OpenCV 2.4.2 imread function causing runtime error

I'm a starter in OpenCV. My programming environment is VC++ Express 2010 + OpenCV 2.4.2 + Win 7 64 bit.
I use purely 32bit configuration in my VC++ and Path.
I type in the following code:
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\core\core.hpp"
using namespace cv;
int main(int argc, char** argv) {
char* imgPath = "logo.png";
Mat img = imread(imgPath);
namedWindow( "Example1", WINDOW_AUTOSIZE);
imshow("Example1", img);
waitKey(0);
return 0;
}
Then I compile and run. It does come up with a window (but without picture) but then gave me this (a runtime error?)
Unhandled exception at 0x770515de in Helloworld2.exe: Microsoft C++ exception: cv::Exception at memory location 0x001ef038..
Then I change the imread into cvLoadImage and it works without any errors.
Can someone tell me what's wrong?
I have tried the code you have given. It works perfectly fine with my installation of OpenCV.
However I am getting a warning at the line:
char* imgPath = "logo.png";
main.cpp:6:21: warning: deprecated conversion from string constant to 'char*' [-
Wwrite-strings]
Which i think is nothing serious to make the code crash, however it might be the problem in your case, as I am not using VC++ to compile.
What you can try to check if this is the issue is to replace imgPath with directly the string, so the code will now be like
Mat img = imread("logo.png");
I also got this issue, but I fixed it with the following. The point is using absolute path other than relative path, and change "\" with "/" in the file path.
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
int main() {
cv::Mat image = cv::imread("D:/projects/test/Debug/1.jpg");
if (image.empty()) {
return 0;
}
cv::namedWindow("my image");
cv::imshow("my image", image);
cv::waitKey(5000);
return 1;
}
The difference between cvLoadImage and imread can be seen on opencv documentation:
C++: Mat imread(const string& filename, int flags=1 )
and
C: CvMat* cvLoadImageM(const char* filename, int
iscolor=CV_LOAD_IMAGE_COLOR )
But there is an implicit conversion from const char * to string. As masad noted, this conversion is deprecated, so it is very compiler dependent.
As cvLoadImage works for you, it seems that you should change your code to something like this:
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\core\core.hpp"
#include <string>
using namespace cv;
int main(int argc, char** argv) {
std::string imgPath("logo.png");
Mat img = imread(imgPath);
namedWindow( "Example1", WINDOW_AUTOSIZE);
imshow("Example1", img);
waitKey(0);
return 0;
}
There has been some problems with C++ interface in Visual Studio, but you may try to see if it works for you.
I had the same problem and I came across Installing OpenCV 2.4.3 in Visual C++ 2010 Express and it mentions to use the updated set of libraries *d.lib when adding dependencies for the Linker. I tried it and the C++ interface worked. Not sure if this is the case in general. I am using OpenCV 2.4 and Visual Studio 2010 on a 64 bit Windows machine.
I got the similar problem with you. When I use the imread function, the program crash with system error message:
opencv_debug.exe 中的 0x0036299f 处未处理的异常: 0xC0000005: 读取位置 0xcccccccc 时发生访问冲突
Then I change the imread into cvLoadImage and it works without any errors.
Finally, I fix the problem, it comes because I used VS2008 project with VS2010 compiled dll.
Here is my story.
Environment: VS2008 + opencv2.4.7
First, I just follow the link http://docs.opencv.org/doc/tutorials/introduction/windows_visual_studio_Opencv/windows_visual_studio_Opencv.html, and compile the my test project.
But, When I run my test project, the system error dialog tells me that I lost MSVCP100D.dll MSVCR100D.dll. So I download the two dll, the dll lost problem is solved, but the problem is crash on run time.
Notice that ahead link, it says:
it means that, in opencv2.4.7, they only provide dlls which compiled in vs2010 or vs2012, not vs2008.
When I compiling vs2008 project with dlls which compiled by vs2010, I get the wired problem.
How can I fix this problem?
use the older opencv version such as opencv2.3, this version contains the vs2008 compiled dll.
compile the opencv by yourself.
I have noticed that your environment is: VC++ Express 2010 + OpenCV 2.4.2 + Win 7 64 bit.
Be sure OPENCV_DIR is set right.
setx -m OPENCV_DIR D:\OpenCV\Build\x64\vc10

Can not read image with opencv2.3 imread

Hello I am trying to read an image by using imread function of opencv as in the link (http://opencv.itseez.com/doc/tutorials/introduction/display_image/display_image.html#display-image). I have VS2010 with 64 bit windows 7. Each time I try I get error message "no image data", however the image I want to read is in the same folder with codes. Can someone please help me how to read an image with imread function? My code is as below:
#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
using namespace cv;
int _tmain(int argc, _TCHAR* argv[])
{
Mat image;
image = imread("al.jpg");
if(argc != 2 || !image.data )
{
printf("no image data \n");
return -1;
}
namedWindow("Display Image", CV_WINDOW_AUTOSIZE);
imshow("Display Image", image);
waitKey(0);
return 0;
}
It you can load image using
IplImage *img=cvLoadImage("Image_Name);
Then you can convert this into cv::Mat using,
Mat mat(img);
I've had the same problem on WinXP with VS2005 and OpenCV 2.3. It seems that the C++ interface of OpenCV is not working well for Windows. I also had problems with imread(), which returned NULL data.
I solved the problem using the C interface of OpenCV instead. For more info, check Reading and Writing Images and Video.
I have met the same problems as yours. The C++ interface for OpenCV2.3 didn't work and some functions have to convert to C version.
But at last, I found what the ** problem is.
That is because the project properties when you compiled your OpenCV source code with CMake, is not the same as your current project properties.(May be your OpenCV's dll and libs are download from the internet, and these may be compiled with other wierd properties.)
So I check my CMake generated OpenCV source code project, I found the project property , C/C++-->Code Generation, the Runtime Library is Multi-threaded Debug DLL(/MDd).
Then, I change the corresponding property in my current project, and my project work well. At the same time, the other C++ interface for OpenCV bugs is solved well.
Take out the argc != 2 test. You aren't using the arguments passed to your program so it's superfluous. And if argc is, in fact, not eaual to 2, your program is terminating before even getting to the !image.data test.
Edit: I just looked at the code sample you linked to. It loads an image whose name is passed to the program on the command line. That's why the argc != 2 test is there. You definitely want to take that out because you are most likely not passing a file name on the command line so your argc is 1, thus the test will always fail.
OpenCV offers support for the image formats Windows bitmap (bmp), portable image formats (pbm, pgm, ppm) and Sun raster (sr, ras).
I experience that same problem, This may come from VS project compilation in VS 10.
In visual studio 2010:
Go to project --> properties --> Character set --> Use multi byte instead of unicode.
Set the Common Language Runtime Support (/clr) in Visual C++.
It works.