Loading and displaying image using opencv in vs2012 - c++

Thanks for all the replies!
I've added the ..\wavFile.wav in the command argument.
But I still cant use the command window.
It still pops up and close immediately.
Maybe its because I use the console application to run this program?
Or are there other reasons?
I am new to opencv and I tried the following code to load and display an image
(using visual studio 2012)
I ran it using the debug mode, but I always get a window shows that
Usage: display_image ImageToLoadAndDisplay,and the window close immediately
(seems like argc is always equal to 2?)
The window wont stay there and wait for a command to load my image.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return 0;
}
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
cvNamedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
Might be a really stupid question but I really cant figure it out for a long time.
Hope someone can help me! THANKS A LOT!

Right click your project in Solution Explorer and select Properties from the menu
Go to Configuration Properties -> Debugging
Set the Command Arguments in the property list.
source : https://stackoverflow.com/a/3697320/4499919
OR
The Mozilla.org FAQ on debugging Mozilla on Windows is of interest here.
In short, the Visual Studio debugger can be invoked on a program from the command line, allowing one to specify the command line arguments when invoking a command line program, directly on the command line.
This looks like the following for Visual Studio 8 or 9
devenv /debugexe 'program name' 'program arguments'
It is also possible to have an explorer action to start a program in the Visual Studio debugger.
source : Debugging with command-line parameters in Visual Studio
OR
https://stackoverflow.com/questions/24202291/opencv-imread-from-command-line-argv1

Related

Load raw image data in Mat object in opencv c++ [duplicate]

I want to load an image using Mat in openCV
My code is:
Mat I = imread("C:/images/apple.jpg", 0);
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", I );
I am getting the following error in a message box:
Unhandled exception at 0x70270149 in matching.exe: 0xC0000005: Access violation
reading location 0xcccccccc.
Please note that I am including:
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <iostream>
#include <math.h>
I've talked about this so many times before, I guess it's pointless to do it again, but code defensively: if a method/function call can fail, make sure you know when it happens:
Mat I = imread("C:\\images\\apple.jpg", 0);
if (I.empty())
{
std::cout << "!!! Failed imread(): image not found" << std::endl;
// don't let the execution continue, else imshow() will crash.
}
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", I );
waitKey(0);
Note that Windows' path uses backslash \ instead of the standard / used on *nix systems. You need to escape the backslash when passing the filename: C:\\images\\apple.jpg
Calling waitKey() is mandatory if you use imshow().
EDIT:
If it's cv::imread() that is throwing the exception the only solution I know to work is downloading OpenCV sources and building it on the machine, since re-installing OpenCV doesn't fix the issue.
I don't know why you don't have include problem because normally it .hpp file so you're suppose to do
#include <opencv2/highgui/highgui.hpp>
#include <opencv2\core\eigen.hpp>
But your code seems good but add a cv::waitKey(0); after your imshow.
Have you checked that I exists after imread? Perhaps the file read failed
After reading a file do if ( I.empty() ) to check if it failed
Are you using visual studio 2010 to run the OpenCV code? If so, try compiling in Release mode.
As pointed out by #karlphillip, however trivial it may sound but this statement " You need to escape the backslash when passing the filename: C:\images\apple.jpg" is really important.

Unhandled exception Microsoft C++ exception: cv::Exception at memory location

I just started with OpenCV. I downloaded OpenCV 2.4.9, and installed MSVS 2010. My Windows is X64. I followed the following steps:
a. Under Configuration Properties, click Debugging -> Environment and copy paste: PATH=C:\opencv\build\x86\vc10\bin
b. VC++ Directories -> Include directories and add the entries: C:\opencv\build\include
c. VC++ Directories -> Library directories and add the entries: C:\opencv\build\x86\vc10\lib
d. Linker -> Input -> Additional Dependencies and add the following:
opencv_calib3d249.lib;opencv_contrib249.lib;opencv_core249.lib;opencv_features2d249.lib;opencv_flann249.lib;opencv_gpu249.lib;opencv_nonfree249.lib;opencv_highgui249.lib;opencv_imgproc249.lib;opencv_legacy249.lib;opencv_ml249.lib;opencv_objdetect249.lib;opencv_ts249.lib;opencv_video249.lib;
I ran the following code:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main() {
// read an image
cv::Mat image= cv::imread("img.jpg");
// create image window named "My Image"
cv::namedWindow("My Image");
cv::waitKey(1000);
// show the image on window
cv::imshow("My Image", image);
// wait key for 5000 ms
cv::waitKey(50);
return 1;
}
To get the error:
Unhandled exception at 0x76d2b727 in BTP1.exe: Microsoft C++ exception: cv::Exception at memory location 0x003af414
I figured this might be because of the X64 and x86 mismatch. On changing the entries in a. to PATH=C:\opencv\build\ x64 \vc10\bin and in c. to C:\opencv\build\ x64 \vc10\lib, I get the following error:
The application was unable to start correctly (0xc000007b). Click OK to close the application.
Any tips on how I can get over this issue?
This is probably happening because the image you are trying to display is empty, perhaps because the image isn't in the right folder. To confirm this, change your code to
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream> // std::cout
int main() {
// read an image
cv::Mat image= cv::imread("img.jpg");
// add the following lines
if(image.empty())
std::cout << "failed to open img.jpg" << std::endl;
else
std::cout << "img.jpg loaded OK" << std::endl;
... // the rest of your code
Resolved the problem. On some tinkering, I found that the program was running in the Release mode, and not the Debug mode.
It was a problem with the Additional Dependencies. Did not add the Debug versions of the same. (XYZ249d.lib)
To add to the other answers, this also commonly occurs if you pass a color image into a tool that requires a grayscale image (ie single channel).
You can convert it to grayscale using the following code:
cv::Mat img_gray;
cv::cvtColor(img_color, img_gray, cv::COLOR_BGR2GRAY);
You can extract and combine individual color channels using the following code:
cv::Mat img_bgr[3];
cv::split(img_color, img_bgr);
//Note: OpenCV uses BGR color order
//img_bgr[0] = blue channel
//img_bgr[1] = green channel
//img_bgr[2] = red channel
cv::Mat img_gray = img_bgr[2] - img_bgr[1]; //laser line extraction is typically red channel minus green channel
I had a similar problem, I just had to give the path of the image file
for example -
D:\image.png

load image with openCV Mat c++

I want to load an image using Mat in openCV
My code is:
Mat I = imread("C:/images/apple.jpg", 0);
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", I );
I am getting the following error in a message box:
Unhandled exception at 0x70270149 in matching.exe: 0xC0000005: Access violation
reading location 0xcccccccc.
Please note that I am including:
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <iostream>
#include <math.h>
I've talked about this so many times before, I guess it's pointless to do it again, but code defensively: if a method/function call can fail, make sure you know when it happens:
Mat I = imread("C:\\images\\apple.jpg", 0);
if (I.empty())
{
std::cout << "!!! Failed imread(): image not found" << std::endl;
// don't let the execution continue, else imshow() will crash.
}
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", I );
waitKey(0);
Note that Windows' path uses backslash \ instead of the standard / used on *nix systems. You need to escape the backslash when passing the filename: C:\\images\\apple.jpg
Calling waitKey() is mandatory if you use imshow().
EDIT:
If it's cv::imread() that is throwing the exception the only solution I know to work is downloading OpenCV sources and building it on the machine, since re-installing OpenCV doesn't fix the issue.
I don't know why you don't have include problem because normally it .hpp file so you're suppose to do
#include <opencv2/highgui/highgui.hpp>
#include <opencv2\core\eigen.hpp>
But your code seems good but add a cv::waitKey(0); after your imshow.
Have you checked that I exists after imread? Perhaps the file read failed
After reading a file do if ( I.empty() ) to check if it failed
Are you using visual studio 2010 to run the OpenCV code? If so, try compiling in Release mode.
As pointed out by #karlphillip, however trivial it may sound but this statement " You need to escape the backslash when passing the filename: C:\images\apple.jpg" is really important.

OpenCV let user choose to open image

I am trying to create a simple image processor in opencv. I so far have experimented to open a set image from file with this code.
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat im = imread("c:/image.jpg");
if (im.empty())
{
cout << "Cannot load image!" << endl;
return -1;
}
imshow("Image", im);
waitKey(0);
}
As this only allows a set image file to be open, how could i modify it so it allows the user to select an image?
Is this possible or can i only load a set image from file?
Thanks.
If you want your program to run in console only, let the user to input the path of the image file ( or may be using command line arguments).
If you want to make it GUI application, (some fancy window will show up when you click a "Open File" button ) then you have to learn some GUI programming. Choose some GUI programming tool depending on your platform ( Windows, Linux etc) or go for cross platform ( Give a try to Qt )
If you want the user to be able to browse for an image on their computer, you can use the open file dialog box. You can find a sample on MSDN.

SDL does not show its window if I use a logging library

I'm trying to use log4cplus in conjunction with SDL to make a little graphic application.
I'm using minGW and Eclipse CDT on windows.
My problem is that whenever I use the library, my SDL window is not shown.
Instead I get this on the console [New Thread 2624.0x1270] and that is it. No error message, no compilation/linking problem, nothing (see edit for precisions).
If I don't use the library, a similar message appears on the console and then disappears, and my SDL window is shown properly.
Below is an example of this behavior. If I comment the two lines starting with "Logger ", then everything is fine. If I do not, SDL window is not shown.
Edit: I've tried using just the logging library and commenting all the other code but this fails as well somehow. I cannot put a breakpoint on the logger code, eclipse tells me: Breakpoint attribute problem: installation failed and the code don't seem to be executed.
*Edit2:*I was on the wrong track, see my post below. Problem kind of solved.
Any Idea?
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_endian.h> /* Used for the endian-dependent 24 bpp mode */
#include "Screen.h"
#include <log4cplus/logger.h>
#include <iomanip>
using namespace log4cplus;
int main(int argc, char **argv) {
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Impossible d'initialiser SDL: %s\n", SDL_GetError());
exit(1);
}
SDL_Surface *screen;
screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
if ( screen == NULL ) {
fprintf(stderr, "Impossible de passer en 640x480 en 16 bpp: %s\n", SDL_GetError());
exit(1);
}
Logger root = Logger::getRoot();
Logger log_1 = Logger::getInstance(LOG4CPLUS_TEXT("test.log_1"));
while(true)
{
SDL_Event event;
SDL_WaitEvent(&event);
switch (event.type) {
case SDL_KEYDOWN:
printf("La touche %s a été préssée!\n",
SDL_GetKeyName(event.key.keysym.sym));
//SDL_Quit();
break;
case SDL_QUIT:
exit(0);
}
Screen::DrawPixel(screen,20,20,200,10,10);
}
}
I have no experience with log4cplus, but I do know that you should be aware that SDL does some obnoxious rewiring of stdout and stderr.
I was going to say that you should move your Logger setup to after your SDL_Init call, but I just noticed that you don't even have one. Try calling SDL_Init before setting up your display.
I found the problem. I hadn't noticed the error gdb gave me when running the program: "
gdb: unknown target exception 0xc0000135". In fact the program didn't start at all.
This means it could not load a dll because of some path problems. I tried putting the dll I use for log4cplus in the system path, tried other workarounds like starting eclipse from but to no avail. One way I found to make it work is simply to put the dll in the Debug folder.
The problem with this gdb error is quite widespread (see google). This is not a proper fix, but I will live with it for the time being, so I consider the problem solved.