Imread causing fatal error on the program - c++

This should be a really straightforward question. When I run the following code:
#include "main.h"
using namespace cv;
int main( int argc, char** argv )
{
Mat image;
image = imread("C:\\Users\\Public\\Pictures\\Sample Pictures\\Lighthouse.jpg");
namedWindow("k");
imshow("k",image);
waitKey(0);
}
This error shows up when I try to run the imread line. Anyone knows what am I doing wrong here?
Thank's in advace.
Additional Info
This is being done in visual studio 2008 professional editions on a windows 7 32-bit machine.
I'm trying to debug the "Release" version.
This is the main.h file:
#pragma once
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0600
#endif
#pragma warning( disable: 4996 )
#include <cv.h>
#include <highgui.hpp>
#include <core.hpp>
My additional include directories (Configuration Properties -> C\C++ -> General) are:
...\OpenCV2.2\include\opencv2\highgui
...\OpenCV2.2\include\opencv2\
...\OpenCV2.2\include\opencv
...\OpenCV2.2\include
The (...) are merely for my privacy, the actual code contains the full path.
My "Additional Dependencies" (Configuration Properties -> Linker -> Input) are:
"...\OpenCV 2.2.0\OpenCV2.2\lib\opencv_core220.lib"
"...\OpenCV 2.2.0\OpenCV2.2\lib\opencv_highgui220.lib"

Related

reduce executable size mixing dynamically / static compilations

I am coding a project using c++ under visual c++ 2017 , but I would like to know if I could compile dynamically my project , and add #pragma comment(lib,"ws2_32") only once for all the project my project looks like
./project.cpp
utils.cpp
utils.h
server.cpp
server.h
api.cpp
api.h
60+ files
main.cpp
#include "utils.h"
#include "server.h"
#pragma comment(lib,"ws2_32")
Server server;
int main(int argc, char **argv) {
server.monitoring();
getchar();
return 0;
}
I want to compile all project dinamically , and only add #pragma comment(lib,"ws2_32") and be useful for all the project . I am compiling the project like this cl project.cpp utils.cpp server.cpp api.cpp /DYNAMICBASE:NO /MD /Ot this give me 14k , and it's okay ,but if I change for /MT because I need the program is executed under every windows machine. I got 223k , but I would like to know if I could cut down a little bit more the memory if it's possible ~50k

C2065 'cout': undeclared identifier [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
So I'm a complete noob to C++ and I'm trying to make a simple "Hello world" program for an assignment. My code is below:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main() {
cout<<"hello world!";
return 0;
}
For some reason, VS2017 is throwing an error at cout, saying it's undefined. I've done some reading on old posts about this and added in #include "stdafx.h to see if that would solve it as per old advice, but it continues to give me the error. Any ideas?
EDIT:
Again a complete noob but there are multiple version of stdafx.h that come up when I search for it, here's what looks like the "main" one:
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently,
// but are changed infrequently
#pragma once
#ifndef STRICT
#define STRICT
#endif
#include "targetver.h"
[!if SERVICE_APP]
#define _ATL_FREE_THREADED
[!else]
#define _ATL_APARTMENT_THREADED
[!endif]
#define _ATL_NO_AUTOMATIC_NAMESPACE
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be explicit
[!if PREVIEW_HANDLER || THUMBNAIL_HANDLER || SEARCH_HANDLER]
#ifdef _MANAGED
#error File type handlers cannot be built as managed assemblies. Set the Common Language Runtime options to no CLR support in project properties.
#endif
#ifndef _UNICODE
#error File type handlers must be built Unicode. Set the Character Set option to Unicode in project properties.
#endif
#define SHARED_HANDLERS
[!endif]
[!if SUPPORT_MFC]
#include <afxwin.h>
#include <afxext.h>
#include <afxole.h>
#include <afxodlgs.h>
#include <afxrich.h>
#include <afxhtml.h>
#include <afxcview.h>
#include <afxwinappex.h>
#include <afxframewndex.h>
#include <afxmdiframewndex.h>
#ifndef _AFX_NO_OLE_SUPPORT
#include <afxdisp.h> // MFC Automation classes
#endif // _AFX_NO_OLE_SUPPORT
[!endif]
[!if SUPPORT_COMPLUS]
#include <comsvcs.h>
[!endif]
#define ATL_NO_ASSERT_ON_DESTROY_NONEXISTENT_WINDOW
#include "resource.h"
#include <atlbase.h>
#include <atlcom.h>
#include <atlctl.h>
error C2065: 'cout': undeclared identifier is a result of the absence of #include <iostream>. The first cause might be stdafx.h content. The one you provided, I'm not so sure how it is related to your main.cpp/project. Let's start from a fresh project: ...VS2017 IDE: Create new project, ConsoleApplication project-type, & replace the main() function with yours.
A VS2017 IDE (15.8.2) fresh ConsoleApplication project: ConsoleApplication1
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
int main() {
cout << "hello world!";
return 0;
}
stdafx.h: (Generated by the IDE)
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
// TODO: reference additional headers your program requires here
stdafx.cpp: (Generated by the IDE)
// stdafx.cpp : source file that includes just the standard includes
// ConsoleApplication1.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
targetver.h: (Generated by the IDE)
#pragma once
// Including SDKDDKVer.h defines the highest available Windows platform.
// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.
#include <SDKDDKVer.h>
** This code runs perfectly. **
--
"there are multiple versions of stdafx.h that come up when I search for it" - What do you mean? In your project? On the internet? You can't just take one stdafx.h from the internet. stdafx.h content is tailored per project, not universal. What I provided above, for example, is the IDE default new ConsoleApplication project stdafx.h. You may add to the file according to your project needs.

Visual Studio 2010 64bit Library Build

Dear StackOverFlow experts,
I have searched and searched for a solution to this problem but have not found an answer. I have found people with similar questions but not an answer. I humbly ask for your forgiveness if I have overlooked a solution. With this I ask you to please consider my submission.
I have a simple test code for OpenCV 2.4.11 in Visual Studio 2010 x64
#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <opencv2\opencv.hpp>
#include <opencv2\core\types_c.h>
using namespace cv;
int main(){
IplImage* img=cvLoadImage("C:\\Users\\Russ\\Pictures\\3-7-15\\_DSC8489.jpg"); //change the name
cvNamedWindow("Example1",CV_WINDOW_NORMAL );
cvShowImage("Example1",img);
cvWaitKey(0);
cvReleaseImage(&img);
cvDestroyWindow( "Example1");
return 0;
}
When I Build this I get an error
C:\opencv\build\include\opencv2/core/types_c.h(55): fatal error C1083: Cannot open include file: 'assert.h': No such file or directory
All of the opencv includes are included in my properties path as are my libraries for OpenCV. When I open the types_c.h file I see the following
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
//M*/
#ifndef __OPENCV_CORE_TYPES_H__
#define __OPENCV_CORE_TYPES_H_
#if !defined _CRT_SECURE_NO_DEPRECATE && defined _MSC_VER
# if _MSC_VER > 1300
# define _CRT_SECURE_NO_DEPRECATE /* to avoid multiple Visual Studio 2005 warnings */
# endif
#endif
#ifndef SKIP_INCLUDES
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#if !defined _MSC_VER && !defined __BORLANDC__
# include <stdint.h>
#endif
#...
Since assert.h is used for debugging, I decided to comment out the assert.h line. When I Build the code again I got this error.
C:\opencv\build\include\opencv2/core/types_c.h(56): fatal error C1083: Cannot open include file: 'stdlib.h': No such file or directory
Clearly something is not set properly for Visual Studio 2010 as it can not find these libraries. Can someone tell how to fix this problem in Visual Studio 2010 x64. Thanks for your help.
For your case, you only need to include opencv2/opencv.hpp to make it work.
Also, since you're using C++, it's strongly recommended to use OpenCV's C++ API over deprecated C API. The code will be like:
#include <opencv2/opencv.hpp>
int main()
{
cv::Mat img = cv::imread("C:\\Users\\Russ\\Pictures\\3-7-15\\_DSC8489.jpg");
cv::namedWindow("Example1", CV_WINDOW_NORMAL);
cv::imshow("Example1", img);
cv::waitKey(0);
cv::destroyWindow("Example1");
return 0;
}
Updated:
If you still encounter with problems, you should follow this post to setup OpenCV correctly with VS.

Fatal error in starting up OpenCV 2.4.6 on VS 2008, file not found

I am using this tutorial to get started with OpenCV 2.4.6 on VS 2008:
http://docs.opencv.org/doc/tutorials/introduction/windows_visual_studio_Opencv/windows_visual_studio_Opencv.html
I followed all instructions (my OpenCV is not in the default Program Files (x86) folder, it's in
C:\opencv_built
Unlike in the tutorial, I put very simple code, just to make sure all included files are reachable and if it builds successfully etc:
#include "stdafx.h"
#include <iostream> // for standard I/O
#include <string> // for strings
#include <iomanip> // for controlling float print precision
#include <sstream> // string to number conversion
#include <opencv2/imgproc/imgproc.hpp> // Gaussian Blur
#include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp> // OpenCV window I/O
using namespace std;
using namespace cv;
double getPSNR ( const Mat& I1, const Mat& I2);
Scalar getMSSIM( const Mat& I1, const Mat& I2);
int main(int argc, char *argv[])
{
return 0;
}
But I get a fatal error when I try to build:
fatal error C1083: Cannot open include file: 'opencv2/imgproc/imgproc.hpp': No such file or directory c:\Users\Administrator\Documents\Visual Studio 2008\Projects\firstopencv\firstopencv\firstopencv.cpp 17
This is obviously referring to this line:
#include <opencv2/imgproc/imgproc.hpp> // Gaussian Blur
I don't know where to find the dll files, or what to do next? I know this must be really easy but I've searched for any of the dll files, eg.
opencv_core243d.lib
but I get no search results.
1. Check your path to \vc10 folder. It should be either:
C:\opencv_built\build\x86\vc10
or
C:\opencv_built\x86\vc10
2. Go to
Start>Edit environment variables for your account>Under System variables > New...
Variable Name: OPENCV_DIR
Variable Value: Insert your path from step 1 here.
3. Open Visual Studio, make new project, go to Property Pages
4. Under C/C++ > Additional Include Directories
Insert $(OPENCV_DIR)\..\..\include
5. Under Linker > General > Additional Include Directories
Insert $(OPENCV_DIR)\lib
6a. (For DEBUG property!) Under Linker > Input > Additional Dependencies
Insert
opencv_core246d.lib
opencv_imgproc246d.lib
opencv_highgui246d.lib
opencv_ml246d.lib
opencv_video246d.lib
opencv_features2d246d.lib
opencv_calib3d246d.lib
opencv_objdetect246d.lib
opencv_contrib246d.lib
opencv_legacy246d.lib
opencv_flann246d.lib
6b. (For RELEASE property!) Under Linker > Input > Additional Dependencies
Insert
opencv_core246.lib
opencv_imgproc246.lib
opencv_highgui246.lib
opencv_ml246.lib
opencv_video246.lib
opencv_features2d246.lib
opencv_calib3d246.lib
opencv_objdetect246.lib
opencv_contrib246.lib
opencv_legacy246.lib
opencv_flann246.lib
This should be enough. If you get missing .dll window after running code, copy desired .dll from your C:\opencv_built\build\x86\vc10\bin or C:\opencv_built\x86\vc10\bin to your project folder.
It's not a dll file, it's a header file. The file is called imgproc.hpp
If you compiler can't find it it's either because it isn't there, or it's because you haven't told your compiler where to find it.
The important part for VS is the “Additional Include Directories”, look at that part again on the web page you followed.
Okay the fix was this:
Under Linker -> General -> Additional Library Dependencies
I put:
C:\opencv_built\lib\Debug

Setup OpenCV-2.3 for Visual Studio 2010

I'm trying to use opencv 2.3 with Visual Studio 2010 Express. My code is from example:
#include "stdafx.h"
#include <highgui.h>
int _tmain(int argc, _TCHAR* argv[])
{
int c;
// allocate memory for an image
IplImage *img;
// capture from video device #1
CvCapture* capture = cvCaptureFromCAM(1);
// create a window to display the images
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
// position the window
cvMoveWindow("mainWin", 5, 5);
while(1)
{
// retrieve the captured frame
img=cvQueryFrame(capture);
// show the image in the window
cvShowImage("mainWin", img );
// wait 10 ms for a key to be pressed
c=cvWaitKey(10);
// escape key terminates program
if(c == 27)
break;
}
return 0;
}
What have I done so far?
Added build\bin and one of build\{x86|x64}\{vc9\vc10\mingw}\bin to my system path (to use DLLs).
Added build\{x86|x64}\{vc9\vc10\mingw}\lib or build\{x86|x64}\{vc9\vc10\mingw}\staticlib as library directories to my linker settings.
Added build\include and build\include\opencv as include directories to my compiler settings.
And the result is:
1>LINK : fatal error LNK1104: cannot open file 'c:\OpenCV2.3\build\x86\vc10\lib.obj'
There's no lib.obj in OpenCV folders. I've only unziped OpenCV-2.3.0-win-superpack.exe, without using CMake software.
What am I doing wrong?
Well, the official guide is for installing OpenCV 2.1 on VS2010, so I wrote some instructions below that shows how to properly install and configure the x86 version of OpenCV 2.3 on Visual Studio 2010 (Express), since a lot of folks seem to have problems setting it up correctly.
Download OpenCV-2.3.0-win-superpack.exe and execute it to extract all files to a folder named OpenCV2.3. Inside this folder there are 2 directories: build and opencv. All the setup on VS2010 will refer to the build directory. For practical purposes I moved the folder OpenCV2.3 to my C:\ drive, so pay attention to the paths I suggest on this guide as yours might be different.
On Visual Studio, create a new Win32 Console Application project and name it whatever you like. After that, a new window will show up. Click on the tab Application Settings and make sure the option Empty Project gets selected:
Add a new file main.cpp to the folder Source Files, then add this code to main.cpp:
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
int main(int argc, char* argv[])
{
if (argc < 2)
{
printf("Usage: ./opencv_hello <file.png>\n");
return -1;
}
IplImage* img = cvLoadImage(argv[1], CV_LOAD_IMAGE_UNCHANGED);
if (!img)
{
return -1;
}
cvNamedWindow("display", CV_WINDOW_AUTOSIZE);
cvShowImage("display", img );
cvWaitKey(0);
return 0;
}
At this point, we need to configure the project so it can locate OpenCV headers and libraries. Go to the Project Properties (ALT+F7), and once the new window shows up do the following:
On the Configuration box, select All Configurations
Open Configuration Properties > C/C++ > General, and edit the field Additional Include Directories to add these 3 paths (for the headers):
C:\OpenCV2.3\build\include\opencv
C:\OpenCV2.3\build\include\opencv2
C:\OpenCV2.3\build\include
Note that include\opencv is for the C interface of OpenCV and include\opencv2 if for the C++ interface. We are also adding the folder include to prevent our build from being broken by some headers of the C interface that refer to C++ headers as opencv2\core.
Then, add the path of the libraries on Configuration Properties > Linker > General, and on the Additional Library Directories field, add this: C:\OpenCV2.3\build\x86\vc9\lib:
Finally, for this simple test we are going to add the libraries opencv_core230.lib and opencv_highgui230.lib. So go to Configuration Properties > Linker > Input and add them:
When writing more complex applications you'll probably need to add other OpenCV libs that I did not
mentioned on this little project of ours.
Press F7 to Build Solution and you should see:
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
To be able to execute the application you'll need to modify the PATH environment variable of your system to add the location of OpenCV's DLLs. Add this to end of PATH:
; C:\OpenCV2.3\build\x86\vc9\bin
If you are struggling with editing the PATH environment variables, you can also copy the required .dll files to your project folder:
The dll files are located in this folder ../OpenCV2.3/build.x86/vc9/bin
Then copy them to the folder where .exe file is created:
c:\Users\PIMMES\Documents\Visual Studio 2010\Projects\eigenfaces\Debug (Ofcourse you have to change the path to your Debug folder)
You only have to copy the .dll files which you are using in your project (#include for example) For example if you get an error message saying opencv_core231d.dll is not found then get this .dll file from the above location (bin folder) and copy to your project Debug folder.
Hope this helps..
Whenever I make a program that uses opencv 2.2 or greater I include everything, and then comment out the libraries I don't need. Try this, I'm sure you need more than highgui.h
#include "opencv2\opencv.hpp"
using namespace cv;
//#pragma comment(lib, "opencv/opencv_calib3d231.lib")
//#pragma comment(lib, "opencv/opencv_contrib231.lib")
#pragma comment(lib, "opencv/opencv_core231.lib")
//#pragma comment(lib, "opencv/opencv_features2d231.lib")
//#pragma comment(lib, "opencv/opencv_flann231.lib")
//#pragma comment(lib, "opencv/opencv_gpu231.lib")
//#pragma comment(lib, "opencv/opencv_haartraining_engine.lib")
#pragma comment(lib, "opencv/opencv_highgui231.lib")
//#pragma comment(lib, "opencv/opencv_imgproc231.lib")
//#pragma comment(lib, "opencv/opencv_legacy231.lib")
//#pragma comment(lib, "opencv/opencv_ml231.lib")
#pragma comment(lib, "opencv/opencv_objdetect231.lib")
//#pragma comment(lib, "opencv/opencv_ts231.lib")
//#pragma comment(lib, "opencv/opencv_video231.lib")