Read & Display Image


Read Image from File and Display


Here I am going to explain how to read an image from a file and display the content using OpenCV library functions. First of all, open your C++ IDE and create a new project. You have to configure your new project  in order to use OpenCV library functions. If you have not configured the project for OpenCV yet, please refer to Installing & Configuring with Visual Studio.

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, const char** argv )
{
  Mat img = imread("MyPic.JPG", CV_LOAD_IMAGE_UNCHANGED); //read the image data in the file "MyPic.JPG" and store it in 'img'

if (img.empty()) //check whether the image is loaded or not
{
cout << "Error : Image cannot be loaded..!!" << endl;
          //system("pause"); //wait for a key press
return -1;
}

namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
imshow("MyWindow", img); //display the image which is stored in the 'img' in the "MyWindow" window

waitKey(0); //wait infinite time for a keypress

     destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"

return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Before you run this program, put any image file (MyPic.JPG) into the folder where your c++ file is.Otherwise you have to change the first argument of imread() function and give the absolute path to your image file.

You can download this OpenCV visual c++ project from here. (The downloaded file is a compressed .rar folder. So, you have to extract it using Winrar or other suitable software)



Load & Diplay Image from File
Load & Diplay Image from File

Explanation


Let's review the above OpenCV code line by line.
  • #include "stdafx.h"
If you are developing your application in Visual Studio, don't forget to put this line above the code.

  • #include "opencv2/highgui/highgui.hpp"
imread()namedWindow()imshow() and waitKey() functions are declared in the above header file. So you must include it.

We are using Mat data structure in the above program. It is declared in "opencv2/core/core.hpp" header file. Then why don't we include this? It's because "opencv2/highgui/highgui.hpp" header file include that header file inside it. So, we don't need to include it again in our program.

  • using namespace cv;
All the data structures and functions in "opencv2/core/core.hpp" and "opencv2/highgui/highgui.hpp" are declared inside cv namespace. So, we have to add the above line in the top of our program. Otherwise we have to append 'cv::' specifier before each OpenCV functions and data structures. (e.g - cv::Mat, cv::imread() , etc). If you are not familiar with namespaces, please refer this article

'#include <iostream>' and 'using namespace std' are added because we are using 'cout' to display some strings in the console. This is basic C++ and you should be familiar with this.

  • Mat img = imread(const string& filename, int flags=CV_LOAD_IMAGE_COLOR)
Mat is a data structure to store images in a matrix. It is declared in "opencv2/core/core.hpp" header file.

imread() is a function declared in "opencv2/highgui/highgui.hpp" header file. It loads an image from a file and stores it in Mat data structure.

Arguments of imread() function
  • filename - location of the file. If you just give the filename only, that image should be in the same folder as your C++ file. Otherwise you have to give the full path to your image.
  • flags - There are four possible inputs
    • CV_LOAD_IMAGE_UNCHANGED - image-depth=8 bits per pixel in each channel,  no. of channels=unchanged 
    • CV_LOAD_IMAGE_GRAYSCALE - image depth=8 bits,  no. of channels=1
    • CV_LOAD_IMAGE_COLOR - image-depth=?,  no. of channels=3
    • CV_LOAD_IMAGE_ANYDEPTH - image-depth=unchanged ,  no. of channels=?
    • CV_LOAD_IMAGE_ANYCOLOR - image-depth=?,  no. of channels=unchanged 

You can combine these above parameters to get desired image output.
e.g -
CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR - image-depth=unchanged,  no. of channels=unchanged 
CV_LOAD_IMAGE_COLOR | CV_LOAD_IMAGE_ANYDEPTH - image-depth=unchanged,  no. of channels=3

If you are not sure what to do, use CV_LOAD_IMAGE_COLOR as the 2nd parameter of imread() function.


To understand image-depth and concept of channels, you should be familiar with theory of image processing. So, let's discuss little bit of theory of image processing.

Any digital image consists of pixels. Any pixel should have some value. The minimum value for a pixel is 0 and it represents black.When the value of the pixel is increased, the intensity of that pixel is also increased.  In a computer memory, ). In decimal, it is 255. fixed number of bits are allocated for every pixel. Say the number of allocated bits per pixel is 8. Then the maximum number that a pixel can have is 255 (11111111 in binary)

Now what is image-depth? The image-depth means the number of bits allocated for each pixel. If it is 8, each pixel can have a value between 0 and 255. If it is 4, each pixel can have a value between 0 to 15 (1111 in binary). 

Here is a simple model of a image with image-depth of 8 bits. Each small box represents a pixel. So, each box may contain a value between 0 to 255.

Here is some properties of the following image.
    •  Image-depth 8 bit
    • 1 channel ( So, this is a grayscale image )
    • The height is 4 pixel 
    • The width is 5 pixels
    • The resolution of this image is 4x5.
This is a grayscale image (black and white image) because this image has no color content. If the value of this pixel is higher, it will be shown more brighter. If the value is low, it will be shown more darker.


Grayscale Image with image depth of 8
Grayscale Image with image depth of 8


Following image is a simple model of a color image. Color image should consist of at least 3 planes; Red, Green and Blue. Any color can be created using a particular combination of these 3 colors. Any pixel is a combination of three 3 values. (255, 0, 0) represent pure red. (0, 255, 0) represent pure green. (255, 0, 255) represents pure violate. In the same way, you can create many color. Image-depth is 24 because each pixel is represented with 8 x 3 bits (8 bits from each channel).

Here is some properties of the following image.
    •  Image-depth 24 bit
    • 3 channels ( So, this is a color image )
    • The height is 4 pixel 
    • The width is 5 pixels
    • The resolution of this image is 4x5.

Red, Green, Blue planes of a color image
R, G, B planes of a color image

In the above model, top left pixel is (23, 231, 46). It will be shown as a greenish color because the green value(231) of that pixel is larger than the red(23) and blue(46) value.

  • if (img.empty())
If imread() function fails to load the image, 'img' will not be loaded any data. Therefore 'img.empty()' should return true. It's a good practice to check whether the image is loaded successfully and if not exit the program. Otherwise your program will crash when executing imshow() function.

  • bool Mat::empty()
This function returns true, if Mat::data==NULL or Mat::total() == 0

  •  //system("pause");
If you are using Visual Studio, it's better to uncomment this line because it will pause the program until user press any key. If we don't uncomment it, the program will exit immediately so that user will not see the error message.
  • void namedWindow(const string& winname, int flags = WINDOW_AUTOSIZE);
This function creates a window.

Parameters -
    • winname - Title of the window. That name will display in the title bar of the newly created window
    • flags - determine the size of the window. There are two options
      • WINDOW_AUTOSIZE - User cannot resize the image. Image will be displayed in its original size
      • CV_WINDOW_NORMAL - Image will resized if you resize the the window

  • void imshow(const string& winname, InputArray mat);
This function shows the image which is stored in the 'mat' in a window specified by winname. If the window is created with WINDOW_AUTOSIZE flag, image will be displayed in its original size. Otherwise image may be scaled to the size of the window.

Parameters -
    • winname - Title of the window. This name is used to identify the window created by namedWindow() function.
    • mat - hold the image data

  • int waitKey(int delay = 0)
waitKey() function wait for keypress for certain time, specified by delay (in milliseconds). If delay is zero or negative, it will wait for infinite time. If any key is pressed, this function returns the ASCII value of the key and your program will continue. If there is no key press for the specified time, it will return -1 and program will continue.
  • void destroyWindow(const string& winname)
This function closes the opened window, with the title of winname and deallocate any associated memory usage. This function is not essential for this application because when the program exits, operating system usually close all the opened windows and deallocate any associated memory usage.


Summary

When running this program, the image of 'MyPic.JPG' is loaded into the variable, 'img' of type Mat. Then a window named 'MyWindow' is opened. After that  'img' is loaded to that window. The window with the image will be displayed until any key is pressed.



Create a Blank Image & Display

This program is also very much similar to the previous application. The only difference is that this program creates a blank image instead of loading an existing image from a file.

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, const char** argv )
{
  Mat img(500, 1000, CV_8UC3, Scalar(0,0, 100)); //create an image ( 3 channels, 8 bit image depth, 500 high, 1000 wide, (0, 0, 100) assigned for Blue, Green and Red plane respectively. )

if (img.empty()) //check whether the image is loaded or not
{
cout << "Error : Image cannot be loaded..!!" << endl;
          //system("pause"); //wait for a key press
return -1;
}

namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
imshow("MyWindow", img); //display the image which is stored in the 'img' in the "MyWindow" window

waitKey(0);  //wait infinite time for a keypress

     destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"

return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Before you run this program, put any image file (MyPic.JPG) into the folder where your c++ file is.Otherwise you have to change the first argument of imread() function and give the absolute path to your image file.

You can download this OpenCV visual c++ project from here. (The downloaded file is a compressed .rar folder. So, you have to extract it using Winrar or other suitable software)

Create & Display Image
Create & Display Image

New OpenCV functions
  •  Mat::Mat(int rows, int cols, int type, const Scalar& s);
This is the one of the many constructor available in Mat class. It initialize the Mat object with the value given by the Scalar object

Parameters :
    • rows - Number of rows in the 2D array ( height of the image in pixels)
    • cols - Number of columns in the 2D array ( width of the image in pixels)
    • type - specify the bit depth, data type and number of channels of the image. I gave CV_8UC3 and it specify 8 bit unsigned integers with 3 channels. Here are some of possible inputs for this parameter
      • CV_8UC1 - 8 bit unsigned integers with single channel
      • CV_8UC3 - 8 bit unsigned integers with 3 channels
      • CV_64FC1 - 64 bit floating point value with 1 channels
If you want more details about this, please refer to Data Types for Arrays in the Basics of OpenCV API

    • s - Initialize each array element with the value given by s. In the above application, I gave Scalar(0,0,100). So, it initialize my first channel (Blue plane) with 0, 2nd channel (Green plane) with 0 and 3rd channel (Red Plane) with 100. So, my final image is red. You can try different combinations of these three and see the output image.
Summary
In this program, I created a 3 channel image with 500 height and 1000 width. 8 bit unsigned integer is allocated for each pixel in each channel. (8x3 = 24 bits per each pixel)  And each pixel is assigned with (0,0,100) scalar value. That means 1st channel is all zero, 2nd channel is also all zero and the 3rd channel is all 100. Therefore we can see a red image as the output of the program.



Next Tutorial : Video from File or Webcam

Previous Tutorial : Basics of OpenCV API


122 comments:

  1. great tutorial! thanks

    ReplyDelete
    Replies

    1. windows 7 keys free , windows 7 ultimate product key , Buy Windows 7 Product Key Online , free upgrade key home basic to professional , Office Professional Plus 2016 sale , product key for windows 7 home premium activation key , windows 7 prouct kay , windows 7 home basic sp1 product key , lJfLtg

      cheap office project 2013 product keys

      buy windows 7 ultimate sp1 product key

      Online Windows 10 Pro Product Key Store

      Windows 7 Home Premium SP1 key code

      buy cheap windows 7 product keys

      Delete
  2. really thank you a lot , great work :)

    ReplyDelete
  3. this is the best tutorial i have ever seen!Thank you so much!!!!FANTASY!

    ReplyDelete
  4. thanks for the tutorial. although the setup for vs2010 was a little different, it wasn't hard to figure out given your instructions. keep up the good work!

    ReplyDelete
  5. If trying this tutorial on a 64bit machine what changes need to be made? Can a win32 application be developed using a 64 bit machine? Thx for any help in advance.

    ReplyDelete
    Replies
    1. i am also having a same problem

      Unhandled exception at 0x000007fefced9e5d in NEW-PROJECT.exe: 0xC06D007E: Module not found.


      I am getting this error while debugging ur code for reading an image.....

      I am using Microsoft Visual Studio 2010 and OpenCV 2.4.5

      My system is also 64 Bits

      Delete
    2. i am also trying to develop a win32 console application using 64 bit machine

      Delete
    3. Please follow this lesson
      http://opencv-srf.blogspot.com/2013/05/installing-configuring-opencv-with-vs.html

      You will need to replace x86 with x64 because your system is 64 bit

      Delete
  6. Hi. I am developing on a x64 bit machine. I followed your instructure on the Installing & Configuring page, but I keep getting the following error:

    error C1083: Cannot open include file: 'core_c.h': No such file or directory

    ReplyDelete
  7. hi.. i already replace x86 to x64.. but still unable to build.. error: module machine type 'x64' conflicts with target machine type 'x86'..

    ReplyDelete
    Replies
    1. yes.. the prob already solved by changing the win32 to x64 for the debug.. i'm using vs2012..

      Delete
    2. hey anonymous, how to change that? I am getting same error. and thanks a ton Mr.Shermal. Great tutorial. :)

      Delete
    3. ^^ Anonymous was right! Thanks.

      Just change win32 by Debug, to x64 (Configuration Manager\Plataform\x64)

      Delete
  8. Splendid! A very good tutorial for beginner in OpenCV. Good job man!

    ReplyDelete
  9. I have Error which is " Cannot open include file: 'opencv2/highgui/highgui.hpp'"...

    ReplyDelete
    Replies
    1. Check that /opencv2/highgui/ to see whether that file is there.

      Delete
    2. You have to set the the include directory in the visual studio project as described in the http://opencv-srf.blogspot.com/2013/05/installing-configuring-opencv-with-vs.html.

      Go to that directory. Then go to opencv2/highgui/ directory. Then check whether the "highgui.hpp" file is exist or not

      Delete
    3. I did checked it. My location : highgui.hpp nam trong folder C:\OpenCV2.3\build\include\opencv2\highgui

      I were replaced #include to #include also, but it does'nt work.
      I checked your link above, but it died. Help me, please!

      Delete
    4. It does'nt work in my computer.

      These are my problems :
      1>------ Build started: Project: Lecture01, Configuration: Debug Win32 ------
      1>Compiling...
      1>read_image2.cpp
      1>Linking...
      1>read_image2.obj : error LNK2019: unresolved external symbol "void __cdecl cv::imshow(class std::basic_string,class std::allocator > const &,class cv::_InputArray const &)" (?imshow@cv@@YAXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV_InputArray@1@@Z) referenced in function _main
      1>read_image2.obj : error LNK2019: unresolved external symbol "public: __thiscall cv::_InputArray::_InputArray(class cv::Mat const &)" (??0_InputArray@cv@@QAE@ABVMat@1@@Z) referenced in function _main
      1>read_image2.obj : error LNK2019: unresolved external symbol "void __cdecl cv::namedWindow(class std::basic_string,class std::allocator > const &,int)" (?namedWindow@cv@@YAXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@Z) referenced in function _main
      1>read_image2.obj : error LNK2019: unresolved external symbol "class cv::Mat __cdecl cv::imread(class std::basic_string,class std::allocator > const &,int)" (?imread@cv@@YA?AVMat@1@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@Z) referenced in function _main
      1>read_image2.obj : error LNK2019: unresolved external symbol "void __cdecl cv::fastFree(void *)" (?fastFree@cv@@YAXPAX@Z) referenced in function "public: __thiscall cv::Mat::~Mat(void)" (??1Mat@cv@@QAE@XZ)
      1>read_image2.obj : error LNK2019: unresolved external symbol "public: void __thiscall cv::Mat::copySize(class cv::Mat const &)" (?copySize@Mat@cv@@QAEXABV12@@Z) referenced in function "public: class cv::Mat & __thiscall cv::Mat::operator=(class cv::Mat const &)" (??4Mat@cv@@QAEAAV01@ABV01@@Z)
      1>read_image2.obj : error LNK2019: unresolved external symbol "public: void __thiscall cv::Mat::deallocate(void)" (?deallocate@Mat@cv@@QAEXXZ) referenced in function "public: void __thiscall cv::Mat::release(void)" (?release@Mat@cv@@QAEXXZ)
      1>C:\Users\Tuyen Khuc\Desktop\Lecture01\Debug\Lecture01.exe : fatal error LNK1120: 7 unresolved externals
      1>Build log was saved at "file://c:\Users\Tuyen Khuc\Desktop\Lecture01\Lecture01\Debug\BuildLog.htm"
      1>Lecture01 - 8 error(s), 0 warning(s)
      ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

      How can I fix it to run fine ?

      Delete
    5. I have Error which is " Cannot open include file: 'opencv2/highgui/highgui.hpp'"...how did u solve this error i am facing same error

      Delete
  10. hi, how can i access to mat img elements? for example a i want the element [0][0] of img and pass it to a double variable, and what type is mat img a double, float?? thank you

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  11. im solved, thanks anyway :D with uchar img.at(0,0)

    ReplyDelete
  12. there's an error that says there's a missing opencv_core246d.dll, but it's there.
    what do i do to get it working?

    ReplyDelete
    Replies
    1. You may have not configured the "path" environment variable correctly. Please refer to http://opencv-srf.blogspot.com/2013/05/installing-configuring-opencv-with-vs.html

      Delete
    2. Copy the dll files inside the C:\\build folder to the Debug folder inside your project folder which will be something like this Documents\\Projects\\Debug

      Delete
    3. thanks Shubham Bajpai, for solution

      Delete
  13. First of all thanks for such a nice post and also for the following one
    http://opencv-srf.blogspot.in/2013/05/installing-configuring-opencv-with-vs.html
    I am having trouble while running the image read program

    #include "stdafx.h"
    //*
    #include "opencv2/highgui/highgui.hpp"
    #include

    using namespace cv;
    using namespace std;

    int main( int argc, const char** argv )
    {
    Mat img = imread("C:\\Users\\JOSHI\\Desktop\\Images\\lUXURY\\1.jpg", CV_LOAD_IMAGE_UNCHANGED); //read the image data in the file "MyPic.JPG" and store it in 'img'

    if (img.empty()) //check whether the image is loaded or not
    {
    cout << "Error : Image cannot be loaded..!!" << endl;
    //system("pause"); //wait for a key press
    return -1;
    }

    namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
    imshow("MyWindow", img); //display the image which is stored in the 'img' in the "MyWindow" window

    waitKey(0); //wait infinite time for a keypress

    destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"

    return 0;
    }

    I am always getting Error : Image cannot be loaded..!!

    my machine is 64bit and the path is J:\op\opencv\build\x64\vc9\bin;
    I am using OpenCV 2.4.6.0 an VS 2008

    ReplyDelete
    Replies
    1. It seems that the given file in "C:\\Users\\JOSHI\\Desktop\\Images\\lUXURY\\1.jpg" does not exist

      Delete
    2. The path is correct and the file exists there the problem is that when I use IplImage in my programs I found no problem whenever I use Mat I get the same problem that unable to load image. Can you tell me what will be the reason because same thing happens for the following code


      #include "stdafx.h"
      //*#include < cv.h >
      #include < cxcore.h>
      //#include < core.h >
      #include < opencv2/core/core.hpp >
      #include < opencv2/highgui/highgui.hpp >
      #include < highgui.h >
      #include < iostream >
      #include < math.h >
      using namespace cv;

      int main( int argc, char** argv )
      {
      Mat I = imread("F:\\1.jpg", 0);
      if (I.empty())
      {
      std::cout << "!!! Failed imread(): image not found" << std::endl;
      // don't let the execution continue, else imshow() will crash.
      system("pause"); //wait for a key press
      return -1;
      }

      namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
      imshow( "Display window", I );
      waitKey(0);

      }

      Don't why IplImage is working well but Mat is not .

      Delete
    3. Since I am not able to read image through Mat I have to use the following code everytime

      #include "opencv2/highgui/highgui.hpp"
      #include
      #include
      using namespace std;
      using namespace cv;
      int main(int argc, char *argv[])
      {
      ///Loading image to IplImage
      //IplImage *img=cvLoadImage(argv[1]);
      IplImage *img;
      img = cvLoadImage("C:\\Users\\JOSHI\\Desktop\\Images\\lUXURY\\1.jpg",1);
      cvShowImage("Ipl",img);
      ///converting IplImage to cv::Mat
      Mat image=cvarrToMat(img);
      imshow("Mat",image);
      waitKey();
      return 0;
      }

      Delete
    4. Had the same problem, all you gotta do is to use
      Mat image = imread( "c:/3.jpg", CV_LOAD_IMAGE_UNCHANGED );
      instead of
      Mat image = imread( "c:\\3.jpg", CV_LOAD_IMAGE_UNCHANGED );

      Actually i don't know the reason, I'm coming from a Java background, where it made no difference using any of the two schemes...

      Delete
    5. Have the same problem of not loading image regardless of the path syntax or image (I tried several). I was able to get it to load and display using PRAKASH's code above. No idea why the original code doesn't work for me but maybe I will as I learn more.

      Thx for the tutorial.

      Delete
    6. I notice in the imread() documentation that cvLoadImage() is the C equivalent of the C++ imread() function. So why would the C version work while the C++ doesn't? Odd.

      For the record I'm using opencv 2.4.8 and VS 2012 (Win32).

      Delete
    7. hi people just want to give you a quick update here i was having the same problem with imread.
      This is the solution go to your project properties where you include your lib files. MAKE SURE THEY ALL END WITH A d.
      For debug mode only use debug library those ending with d and for release use without d. This only applies for windows.

      Delete
    8. Thanks Nikhil Sadalkar. Your answer is really useful for me.

      Delete
    9. I was having the same issue, moved .jpg into build folder with the executable not the .cpp, works fine for me

      Delete
  14. Thanks for this fabulous tutorial ! However I've problem with loading an image whereas generating colored windows is OK. My app crashes and i get following erros:

    First-chance exception at 0x000007FCAF58649F (msvcr110d.dll) in opencv.exe: 0xC0000005: Access violation reading location 0x0000007A585A0047.
    Unhandled exception at 0x000007FCAF58649F (msvcr110d.dll) in opencv.exe: 0xC0000005: Access violation reading location 0x0000007A585A0047.
    First-chance exception at 0x000007FCAF58649F (msvcr110d.dll) in opencv.exe: 0xC0000005: Access violation reading location 0x0000007A585A0047.
    Unhandled exception at 0x000007FCAF58649F (msvcr110d.dll) in opencv.exe: 0xC0000005: Access violation reading location 0x0000007A585A0047.
    First-chance exception at 0x000007FCAF58649F (msvcr110d.dll) in opencv.exe: 0xC0000005: Access violation reading location 0x0000007A585A0047.
    Unhandled exception at 0x000007FCAF58649F (msvcr110d.dll) in opencv.exe: 0xC0000005: Access violation reading location 0x0000007A585A0047.
    The program '[5680] opencv.exe' has exited with code 0 (0x0).

    I'll be grateful for any suggestions.

    ReplyDelete
  15. Hello, im still new user in opencv..
    where should i put my image in my directory? so i can load my image

    ReplyDelete
    Replies
    1. Put your image in the directory where the executable file of your c++ program is created. Then you can give only the name of the image file.
      e.g. - Mat img = imread("MyPic.JPG", CV_LOAD_IMAGE_UNCHANGED);

      OR

      Put your image in any location and give the full absolute path of the image.
      e.g. - Mat img = imread("D:/MyFolder/MyPic.JPG", CV_LOAD_IMAGE_UNCHANGED);

      Delete
  16. I want to read images one by one from file...how can I do that..?

    ReplyDelete
    Replies
    1. using Visual studio 2008 and opencv 2.4.6 I try like the following

      array^ file = System::IO::Directory::GetFiles( "C:\\ImageFolderPath" );

      for (int f=0; fLength; f++)
      {
      const char* charsMatch = (const char*)(Marshal::StringToHGlobalAnsi(file[f])).ToPointer();
      Mat data=cv::imread(charsMatch,0);
      imshow("Mat",data);
      }

      may be this will help you. Only you have to change a little bit inside the loop for dynamically creating the object of Mat. At present it is overwriting the images in the same image.

      Delete
  17. This comment has been removed by the author.

    ReplyDelete
  18. This is so spectacular. It makes my boring data look like a Piet Mondrian art piece ! Thanks for sharing !

    ReplyDelete
  19. Hi I am getting this error...


    features.h:393: error: #define/#defeval requires an identifier or a single macro call

    ReplyDelete
  20. I am working on linux i am getting this much and licence agreement and error that i had already uploaded


    ////////
    //
    // 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
    //
    // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
    // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
    // Third party copyrights are property of their respective owners.
    //
    // Redistribution and use in source and binary forms, with or without modification,
    // are permitted provided that the following conditions are met:
    //
    // * Redistribution's of source code must retain the above copyright notice,
    // this list of conditions and the following disclaimer.
    //
    // * Redistribution's in binary form must reproduce the above copyright notice,
    // this list of conditions and the following disclaimer in the documentation
    // and/or other materials provided with the distribution.
    //
    // * The name of the copyright holders may not be used to endorse or promote products
    // derived from this software without specific prior written permission.
    //
    // This software is provided by the copyright holders and contributors "as is" and
    // any express or implied warranties, including, but not limited to, the implied
    // warranties of merchantability and fitness for a particular purpose are disclaimed.
    // In no event shall the Intel Corporation or contributors be liable for any direct,
    // indirect, incidental, special, exemplary, or consequential damages
    // (including, but not limited to, procurement of substitute goods or services;
    // loss of use, data, or profits; or business interruption) however caused
    // and on any theory of liability, whether in contract, strict liability,
    // or tort (including negligence or otherwise) arising in any way out of
    // the use of this software, even if advised of the possibility of such damage.
    //
    //M*/


    /*! file core.hpp
    brief The Core Functionality
    */
    /*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.
    //
    //

    ReplyDelete
  21. I built the very first program on Visual Studio 2012 x86, but my operating system is x64. When I configured OpenCV lib as x86, there are always some errors:
    'Test1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msctf.dll'. Cannot find or open the PDB file.
    'Test1.exe' (Win32): Loaded 'C:\PROGRA~2\OPTIMI~1\OPTPRO~1.DLL'. Module was built without symbols.
    'Test1.exe' (Win32): Unloaded 'C:\PROGRA~2\OPTIMI~1\OPTPRO~1.DLL'
    'Test1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\apphelp.dll'. Cannot find or open the PDB file.
    'Test1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cmd.exe'. Cannot find or open the PDB file.
    'Test1.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\cmd.exe
    Does anyone have this problem?

    ReplyDelete
    Replies
    1. You have to configure OpenCV for x64 architecture

      Delete
    2. I have the same problem. I had configured for 32bit and my system is also 32 bit

      Delete
  22. hello, i'm still new with opencv..i've followed all steps to configure opencv in vs12..then i try to build solution of your first coding example..it state that: Rebuild All: 1 succeeded, 0 failed, 0 skipped..but why it do not show or pop out any image? i've put image file (MyPic.JPG) into the folder where c++ file is.

    1>------ Rebuild All started: Project: opencv, Configuration: Debug Win32 ------
    1>Build started 11/17/2013 1:30:41 AM.
    1>_PrepareForClean:
    1> Deleting file "Debug\opencv.lastbuildstate".
    1>InitializeBuildStatus:
    1> Creating "Debug\opencv.unsuccessfulbuild" because "AlwaysCreate" was specified.
    1>ClCompile:
    1> stdafx.cpp
    1> opencv.cpp
    1>Link:
    1> opencv.vcxproj -> c:\users\hello\documents\visual studio 2012\Projects\opencv\Debug\opencv.exe
    1>FinalizeBuildStatus:
    1> Deleting file "Debug\opencv.unsuccessfulbuild".
    1> Touching "Debug\opencv.lastbuildstate".
    1>
    1>Build succeeded.
    1>
    1>Time Elapsed 00:00:02.81
    ========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

    ReplyDelete
  23. You might not use the waitkey(0) function

    ReplyDelete
  24. Thanks man, this tutorial is really helpful for beginners as me!

    ReplyDelete
  25. when i run application this error occurs(the program can not run because MSVCP110.dll is missing from your computer Try reinstalling the program to fix this problem))
    how to solve this ??

    ReplyDelete
  26. #include "stdafx.h"
    #include "opencv2/highgui/highgui.hpp"
    #include
    #include
    #include

    using namespace cv;
    using namespace std;

    int main( int argc, const char** argv )
    {
    Mat img = imread("MyPic.JPG", CV_LOAD_IMAGE_UNCHANGED);

    if (img.data == NULL)
    {
    cout << "Image cannot be loaded..!!" << endl;
    system("pause");
    return -1;
    }

    namedWindow("MyImage", CV_WINDOW_AUTOSIZE);

    imshow("MyImage", img);

    waitKey(0);

    return 0;
    }



    when i use this code, i'm getting the error "Image cannot be loaded..!!" ..

    but when i use 'IplImage* img = cvLoadImage( "D:\MyImage.jpg" );' its working..

    I'm a beginner and i have no idea what is going wrong.. I'm using Opencv 2.4.7 and visual studio express 2012..

    I'm supposed to use 'imread' according to my professor.. please someone help me out..

    Thank you in advance..

    ReplyDelete
    Replies
    1. The problem is the location of the image. I think that the location of the image is "D:\MyImage.jpg". Therefore replace " imread("MyPic.JPG", CV_LOAD_IMAGE_UNCHANGED)" with imread("D:\MyImage.jpg", CV_LOAD_IMAGE_UNCHANGED)

      Delete
  27. Hi ,
    m using exactly ur code for sample run on visual studio 2012 and opencv_2.4.6
    BUT i am getting following warning - "warning C4129: 'M' : unrecognized character escape sequence"


    //#include "stdafx.h"
    #include "opencv2/highgui/highgui.hpp"
    #include

    using namespace cv;
    using namespace std;

    int main( int argc, const char** argv )
    {
    Mat img = imread("Desktop\MyPic.JPG", CV_LOAD_IMAGE_UNCHANGED);

    if (img.data == NULL)
    {
    cout << "Image cannot be loaded..!!" << endl;
    system("pause");
    return -1;
    }

    namedWindow("MyImage", CV_WINDOW_AUTOSIZE);

    imshow("MyImage", img);

    waitKey(0);

    return 0;
    }



    I have 64 bit architecture but i am using 32 bit configuration as 64 bit configuration was generation loads of errors

    ReplyDelete
    Replies
    1. Did you configure your IDE for OpenCV in 32 bit mode?

      Delete
  28. Firs I am so grateful for this inf.
    I have a problem when I wanna read and display an image.
    after running the code, the output image is a gray image instead of my real image.
    Thank a lot for your help in advance.
    This is also what the debug shows after running the code:

    'OpenCVTest.exe': Loaded 'C:\Users\Alireza\Documents\Visual Studio 2010\Projects\OpenCVTest\Debug\OpenCVTest.exe', Symbols loaded.
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\opencv248\build\x86\vc10\bin\opencv_core248d.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\msvcp100d.dll', Symbols loaded.
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
    'OpenCVTest.exe': Loaded 'C:\opencv248\build\x86\vc10\bin\opencv_highgui248d.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\user32.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\gdi32.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\sechost.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\sspicli.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\cryptbase.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\ole32.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\oleaut32.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.windows.common-controls_6595b64144ccf1df_5.82.7601.18201_none_ec80f00e8593ece5\comctl32.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\avifil32.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\winmm.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\msacm32.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\msvfw32.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\shell32.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\shlwapi.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\avicap32.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\version.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll', Cannot find or open the PDB file
    'OpenCVTest.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll', Cannot find or open the PDB file
    The program '[5720] OpenCVTest.exe: Native' has exited with code -1 (0xffffffff).

    ReplyDelete
    Replies
    1. Also I tried both having image in the source file and give a direction. none of them works :(

      Delete
    2. Change the 2nd parameter of imread as follows
      imread("MyPic.JPG", CV_LOAD_IMAGE_COLOR)

      Delete
    3. when I put the image inside the folder it works and show the image but it does not work when I have an image outside the folder, do u know why it happens??

      Delete
  29. thanks for your tutorial, it's really helpful.
    do i have to reconfigure openCv library for every project?

    ReplyDelete
  30. Nice tutorial ... Thanks a lot ayya :) :)

    ReplyDelete
  31. its a great tutorial...code is working perfectly but i have a problem.Image show in window is not full.Its slowing a part of my original image.It may be due to small window size than image.How can I get full image in window as you have done?

    ReplyDelete
  32. I've followed the tutorial as closely as I could (with the exception of using VS2013) and made the changes for x64, but I continue to get an error. It claims I'm missing an MSVCP110.dll file, which I went to Microsoft's website to download. I've tried repairing VS and uninstalling/reinstalling it. Everytime I run the code, the output module displays the following:

    'OKAY.exe' (Win32): Loaded 'C:\Users\Melissa's\Documents\Visual Studio 2013\Projects\OKAY\x64\Debug\OKAY.exe'. Symbols loaded.
    'OKAY.exe' (Win32): Loaded 'C:\Windows\System32\ntdll.dll'. Symbols loaded.
    'OKAY.exe' (Win32): Loaded 'C:\Windows\System32\kernel32.dll'. Symbols loaded.
    'OKAY.exe' (Win32): Loaded 'C:\Windows\System32\KernelBase.dll'. Symbols loaded.
    'OKAY.exe' (Win32): Loaded 'C:\opencv\build\x64\vc11\bin\opencv_core245d.dll'. Cannot find or open the PDB file.
    The program '[7208] OKAY.exe' has exited with code -1073741515 (0xc0000135) 'A dependent dll was not found'.


    Any suggestions? I'm trying to learn to use OpenCV for object detection, and then I need to figure out how to move the code over to be used on a BeagleBone Black.

    ReplyDelete
  33. i am trying to run the following code:


    #include
    #include
    #include

    using namespace std;
    using namespace cv;

    int main()
    {
    Mat image = imread("trigl.jpg,CV_LOAD_IMAGE_COLOR");
    if(image.empty())
    return -1;
    imshow("TEST",image);
    waitKey();

    return 0;
    }

    my error are as follows
    'final.exe': Loaded 'C:\Users\Disha_2\Documents\Visual Studio 2010\Projects\final\Debug\final.exe', Symbols loaded.
    'final.exe': Loaded 'C:\Windows\System32\ntdll.dll', Symbols loaded (source information stripped).
    'final.exe': Loaded 'C:\Windows\System32\kernel32.dll', Symbols loaded (source information stripped).
    'final.exe': Loaded 'C:\Windows\System32\KernelBase.dll', Symbols loaded (source information stripped).
    'final.exe': Loaded 'C:\Users\Disha_2\Documents\Visual Studio 2010\Projects\final\Debug\opencv_core248d.dll', Cannot find or open the PDB file
    'final.exe': Loaded 'C:\Windows\System32\msvcp100d.dll', Symbols loaded.
    'final.exe': Loaded 'C:\Windows\System32\msvcr100d.dll', Symbols loaded.
    'final.exe': Loaded 'C:\opencv\build\x86\vc10\bin\opencv_highgui248d.dll', Cannot find or open the PDB file
    'final.exe': Loaded 'C:\Windows\System32\user32.dll', Symbols loaded (source information stripped).
    'final.exe': Loaded 'C:\Windows\System32\gdi32.dll', Symbols loaded (source information stripped).
    'final.exe': Loaded 'C:\Windows\System32\lpk.dll', Symbols loaded (source information stripped).
    'final.exe': Loaded 'C:\Windows\System32\usp10.dll', Symbols loaded (source information stripped).
    'final.exe': Loaded 'C:\Windows\System32\msvcrt.dll', Symbols loaded (source information stripped).
    'final.exe': Loaded 'C:\Windows\System32\ole32.dll', Symbols loaded (source information stripped).
    'final.exe': Loaded 'C:\Windows\System32\rpcrt4.dll', Symbols loaded (source information stripped).
    'final.exe': Loaded 'C:\Windows\System32\oleaut32.dll', Symbols loaded (source information stripped).
    'final.exe': Loaded 'C:\Windows\System32\advapi32.dll', Symbols loaded (source information stripped).
    'final.exe': Loaded 'C:\Windows\System32\sechost.dll', Symbols loaded (source information stripped).
    'final.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.windows.common-controls_6595b64144ccf1df_5.82.7600.16385_none_ebf82fc36c758ad5\comctl32.dll', Symbols loaded (source information stripped).
    'final.exe': Loaded 'C:\Windows\System32\avifil32.dll', Symbols loaded (source information stripped).
    'final.exe': Loaded 'C:\Windows\System32\winmm.dll', Symbols loaded (source information stripped).
    'final.exe': Loaded 'C:\Windows\System32\msacm32.dll', Symbols loaded (source information stripped).
    'final.exe': Loaded 'C:\Windows\System32\msvfw32.dll', Symbols loaded (source information stripped).
    'final.exe': Loaded 'C:\Windows\System32\shell32.dll', Symbols loaded (source information stripped).
    'final.exe': Loaded 'C:\Windows\System32\shlwapi.dll', Symbols loaded (source information stripped).
    'final.exe': Loaded 'C:\Windows\System32\avicap32.dll', Symbols loaded (source information stripped).
    'final.exe': Loaded 'C:\Windows\System32\version.dll', Symbols loaded (source information stripped).
    'final.exe': Loaded 'C:\Windows\System32\imm32.dll', Symbols loaded (source information stripped).
    'final.exe': Loaded 'C:\Windows\System32\msctf.dll', Symbols loaded (source information stripped).
    The program '[1644] final.exe: Native' has exited with code -1 (0xffffffff).

    what is the problem?

    ReplyDelete
    Replies
    1. see this line Mat image = imread("trigl.jpg",CV_LOAD_IMAGE_COLOR); correct ur code

      Delete
  34. thanks you veru much.

    ReplyDelete
  35. here for reading the image
    "imread" function will not work , instead use " cvLoadImage in place of "imread" .

    ReplyDelete
  36. I successfully build this but the image won't appear.. what's the problem with this?

    ReplyDelete
    Replies
    1. Check that the Solution Configurations is set not to Release but to Debug.

      Delete
  37. ive got this when i try to run the program. Any thoughts on how i can fix this?

    'ConsoleApplication4.exe' (Win32): Loaded 'C:\Users\Irham\Documents\Visual Studio 2013\Projects\ConsoleApplication4\Debug\ConsoleApplication4.exe'. Symbols loaded.
    'ConsoleApplication4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
    'ConsoleApplication4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
    'ConsoleApplication4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
    The program '[8760] ConsoleApplication4.exe' has exited with code -1073741701 (0xc000007b).

    ReplyDelete
    Replies
    1. While the project is open in Visual Studio, check that the Solution Configurations is set not to Release but to Debug. If set to release, set it to Debug.
      If the problem still persists, try copying the dll files from the opencv\build\x64\bin folder to the debug folder inside your project folder. Change the paths according to your situation. These two worked for me. :)

      Delete
  38. hey im working with VS 2013 and OpenCV 2.4.8

    I configured the VS with the OpenCV as per the previous tutorial, Path variables and everything. im trying to run the above code and im not getting an image. The build was successful, but when i was debugging i getting the problem:
    missing opencv_core248d.dll, but it's there. then i put all the dll files into the C:windows/system and then its not showing an error. but the image wont load, and this is what it says:

    'Img.exe' (Win32): Loaded 'C:\Windows\System32\avicap32.dll'. Cannot find or open the PDB file.
    'Img.exe' (Win32): Loaded 'C:\Windows\System32\version.dll'. Cannot find or open the PDB file.
    'Img.exe' (Win32): Loaded 'C:\Windows\System32\imm32.dll'. Cannot find or open the PDB file.
    'Img.exe' (Win32): Loaded 'C:\Windows\System32\msctf.dll'. Cannot find or open the PDB file.
    'Img.exe' (Win32): Loaded 'C:\PROGRA~1\GS-ENA~1\ASSIST~1.DLL'. Module was built without symbols.
    'Img.exe' (Win32): Unloaded 'C:\PROGRA~1\GS-ENA~1\ASSIST~1.DLL'
    The program '[8124] Img.exe' has exited with code -1 (0xffffffff).

    ReplyDelete
    Replies
    1. While the project is open in Visual Studio, check that the Solution Configurations is set not to Release but to Debug. If set to release, set it to Debug.
      If the problem still persists, try copying the dll files from the opencv\build\x64\bin folder to the debug folder inside your project folder. Change the paths according to your situation. These two worked for me. :)

      Delete
  39. #include "stdafx.h"
    #include
    #include
    #include
    #include
    using namespace cv;
    using namespace std;




    int _tmain(int argc, _TCHAR** argv[])
    {
    Mat img;
    img = imread("C:\\Users\\user\\Desktop\\snapshot\\IMG_000",CV_LOAD_IMAGE_UNCHANGED);
    if (img.empty())
    {
    std::cout << "!!! Failed imread(): image not found" << std::endl;
    // don't let the execution continue, else imshow() will crash.
    return -1;
    }
    namedWindow("Original Image", CV_WINDOW_AUTOSIZE);
    imshow("Original Image",img);
    waitKey(0);
    return 0;
    }
    When I try to run my code the following errors are shown any help please
    Error 2 error LNK2001: unresolved external symbol "public: void __thiscall cv::Mat::copySize(class cv::Mat const &)" (?copySize@Mat@cv@@QAEXABV12@@Z) c:\Users\user\documents\visual studio 2010\Projects\ConsoleProject\ConsoleProject.obj
    Error 3 error LNK2001: unresolved external symbol "void __cdecl cv::fastFree(void *)" (?fastFree@cv@@YAXPAX@Z) c:\Users\user\documents\visual studio 2010\Projects\ConsoleProject\ConsoleProject.obj
    Error 4 error LNK2001: unresolved external symbol "public: void __thiscall cv::Mat::deallocate(void)" (?deallocate@Mat@cv@@QAEXXZ) c:\Users\user\documents\visual studio 2010\Projects\ConsoleProject\ConsoleProject.obj
    Error 5 error LNK2001: unresolved external symbol "public: __thiscall cv::_InputArray::_InputArray(class cv::Mat const &)" (??0_InputArray@cv@@QAE@ABVMat@1@@Z) c:\Users\user\documents\visual studio 2010\Projects\ConsoleProject\ConsoleProject.obj
    Error 6 error LNK2001: unresolved external symbol "class cv::Mat __cdecl cv::imread(class std::basic_string,class std::allocator > const &,int)" (?imread@cv@@YA?AVMat@1@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@Z) c:\Users\user\documents\visual studio 2010\Projects\ConsoleProject\ConsoleProject.obj
    Error 7 error LNK2001: unresolved external symbol "void __cdecl cv::namedWindow(class std::basic_string,class std::allocator > const &,int)" (?namedWindow@cv@@YAXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@Z) c:\Users\user\documents\visual studio 2010\Projects\ConsoleProject\ConsoleProject.obj
    Error 8 error LNK2001: unresolved external symbol "void __cdecl cv::imshow(class std::basic_string,class std::allocator > const &,class cv::_InputArray const &)" (?imshow@cv@@YAXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV_InputArray@1@@Z) c:\Users\user\documents\visual studio 2010\Projects\ConsoleProject\ConsoleProject.obj
    Error 9 error LNK2001: unresolved external symbol "int __cdecl cv::waitKey(int)" (?waitKey@cv@@YAHH@Z) c:\Users\user\documents\visual studio 2010\Projects\ConsoleProject\ConsoleProject.obj
    Error 10 error LNK1120: 8 unresolved externals c:\users\user\documents\visual studio 2010\Projects\ConsoleProject\Release\ConsoleProject.exe

    ReplyDelete
  40. Thanks alot really your tutorial is perfect for learning opencv and computer vision

    ReplyDelete
  41. Hi i'm working with VS 2013 and OpenCV 2.4.8
    with this tutorial and it is not working
    this is the error can you help me ?

    'test.exe' (Win32): Loaded 'C:\Users\Few Days\Documents\Visual Studio 2013\Projects\test\Release\test.exe'. Symbols loaded.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\opencv\build\x86\vc12\bin\opencv_core248d.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp120d.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcr120d.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\opencv\build\x86\vc12\bin\opencv_highgui248d.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\user32.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\gdi32.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\lpk.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\usp10.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ole32.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\oleaut32.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\winsxs\x86_microsoft.windows.common-controls_6595b64144ccf1df_5.82.7601.18201_none_ec80f00e8593ece5\comctl32.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvfw32.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\winmm.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\shell32.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\shlwapi.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\avifil32.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msacm32.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\avicap32.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\version.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\imm32.dll'. Cannot find or open the PDB file.
    'test.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msctf.dll'. Cannot find or open the PDB file.
    First-chance exception at 0x777F8B51 (msvcr120d.dll) in test.exe: 0xC0000005: Access violation reading location 0x504A2E63.
    Unhandled exception at 0x777F8B51 (msvcr120d.dll) in test.exe: 0xC0000005: Access violation reading location 0x504A2E63.

    ReplyDelete
  42. Im using Eclipse so it might be a little different but about the image, I think it needs to be in the same place as the folder which contains the .cpp file, not the exact same place as the .cpp file. Worked for me.

    ReplyDelete
  43. spelling error: (255,0,255) does not give you violate, its gives you violet... though both do involve some purple.

    ReplyDelete
  44. Thank you very much for the great tutorial. I have a small question:

    I wrote: namedWindow("MyWindow", CV_WINDOW_AUTOSIZE)
    However the heading on my GUI window displays +ÄG where the + is more like a cross. Would you know why this is happening?

    ReplyDelete
  45. I have this error
    'ConsoleApplication1.exe' (Win32): Loaded 'C:\Users\Devi~chan\Documents\Visual Studio 2012\Projects\ConsoleApplication1\Debug\ConsoleApplication1.exe'. Symbols loaded.
    'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\System32\ntdll.dll'. Cannot find or open the PDB file.
    'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\System32\kernel32.dll'. Cannot find or open the PDB file.
    'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\System32\KernelBase.dll'. Cannot find or open the PDB file.
    'ConsoleApplication1.exe' (Win32): Loaded 'C:\Users\Devi~chan\Documents\Visual Studio 2012\Projects\ConsoleApplication1\Debug\opencv_core249.dll'. Cannot find or open the PDB file.
    The program '[13552] ConsoleApplication1.exe' has exited with code -1073741515 (0xc0000135) 'A dependent dll was not found'.
    how can I fix this? I already copied dll files to the debug folder but it still doesn't work, please help me

    ReplyDelete
  46. Thank you so much for the tutorial!

    ReplyDelete
  47. Hi

    This is a great tutorial. I'm working on OCR application for my native language and do you have any tutorials or sample projects on OCR.

    Thanks :)

    ReplyDelete
  48. Tried this tutorial and the one in OpenCV doc.... and keep having these errors when trying to compile my code in VS2010....

    objdetect.hpp(338): error C2589: '(' : illegal token on right side of '::'
    objdetect.hpp(338): error C2059: syntax error : '::'
    objdetect.hpp(338): error C2589: '(' : illegal token on right side of '::'

    Anyone knows what's the problem?

    ReplyDelete
  49. This comment has been removed by the author.

    ReplyDelete
  50. Hey Guys!
    Learn openCV from begining with video tutorials...from what is open cv, installation, to face tracking and openCV and then its application with Embedded Electronics
    https://www.udemy.com/learn-open-cv-with-microsoft-visual-c/
    use this CODE for 90% discount- opencv90
    or use this link -https://www.udemy.com/learn-open-cv-with-microsoft-visual-c/?couponCode=opencv90

    ReplyDelete
  51. Hi, Shermal, thank you for this tutorial. Well presented. Yet in practice it did not work for me.
    On my PC Windows XP SP3 I installed VS-2010 and openCV2.1 and performed all the steps to Configure Microsoft Visual Studio 2010 as prescribed in your http://opencv-srf.blogspot.ru/2011/09/getting-started-with-opencv_16.html

    Created a project and copied this code of yours there. Yet on debugging got an error message other people are also getting:
    " Cannot open include file: 'opencv2/highgui/highgui.hpp' "
    Indeed my openCV2.1 did nit create directory opencv2/highgui/ at all. File highgui.hpp is sitting in
    C:\OpenCV2.1\include\opencv

    I changed
    #include "opencv2/highgui/highgui.hpp"
    to
    #include "OpenCV2.1/include/opencv/highgui.hpp"
    yet the error persisted.

    I tried to replace "/" with "\" to match Windows notation - did not help
    I renamed folder OpenCV2.1 to OpenCV2 (to avoid dot in directory name) and changed the code and the VS2010 configuration accordingly - did not help.

    Tried to use the absolute path to this file C:/OpenCV2.1/include/opencv/highgui.hpp - got a syntax error.

    Also my XP and VS2010 are installed on drive F: while openCV2.1 folder is sitting on drive C (this drive has no OS on it, just my data) - perhaps this can cause thge error? I copied the whole openCV2.1 folder on F: and changed VS2010 configuration accordingly - no joy

    What am I doing wrong?

    ReplyDelete
  52. Hey,

    Great work! This was really helpuful.

    Thanks

    ReplyDelete
  53. I tried running the same code. It builds successfully but the image window closes immediatly and no image is displayed.
    Can anyone please guide me with this?

    ReplyDelete
  54. Excellent tutorial, gave me a good start into OpenCV.
    cheers

    ReplyDelete
  55. stdafx.h not found
    what do i do?

    ReplyDelete
  56. Error 1 error LNK1112: module machine type 'x64' conflicts with target machine type 'X86'

    how to solve?

    ReplyDelete
  57. hey I am getting this error , what should I do now . Please suggest me
    Error 1 error LNK1104: cannot open file 'opencv_haartraining_engined.lib'

    ReplyDelete
  58. HEY !! How can i get these 3 boxes( intesity ) from photo in c++?

    ReplyDelete
  59. Sir ! I don't understand this error . Please suggest me!
    >>fatal error C1083: Cannot open include file: 'opencv2/highgui/highgui.hpp': No such file or directory

    ReplyDelete
    Replies
    1. hey i also got the same error ,did u got the solution

      Delete
    2. you need to configurate include directories as shown in http://opencv-srf.blogspot.com.br/2013/05/installing-configuring-opencv-with-vs.html
      but instead of Active(Win32) (show on the top of the config window) you should use x64 if you are running in a x64 machine. Also in (Build->Config. Manager) change your platform to x64 before build. That worked for me.

      Delete
  60. Thanks shermal for given us such a nice tutorial. I installed visual studio 2013 and installed opencv 2.4.11. My operating system is 64 bit. I edit path in the environmental variable is this "%OPENCV_DIR%\x64\vc11\bin".I run this code when it reach imread line so it give following error "The program '[3968] open-cvtest.exe' has exited with code -1 (0xffffffff)." so please tell me what this error mean I am very thankful to you

    --
    Abdul Wahab

    ReplyDelete
  61. Hey, mate, thanks for everything. Really appreciate the time and effort you spent on these extremely helpful entries ;)

    ReplyDelete
  62. Hey please solve this issue:

    Error 1 error LNK1112: module machine type 'x64' conflicts with target machine type 'X86' C:\Users\modaduguakhilkumar\documents\visual studio 2012\Projects\imageprocessing\imageprocessing\opencv_world300.lib(opencv_world300.dll) imageprocessing

    ReplyDelete
    Replies
    1. hey i got the solution

      Check your properties options in your linker settings at: Properties > Configuration Properties > Linker > Advanced > Target Machine. Select MachineX64 if you are targeting a 64 bit build, or MachineX86 if you are making a 32 bit build.

      Select Build > Configuration Manager from the main menu in visual studio. Make sure your project has the correct platform specified. It is possible for the IDE to be set to build x64 but an individual project in the solution can be set to target win32. So yeah, visual studio leaves a lot of rope to hang yourself, but that's life.

      Check your library files that they really are of the type of platform are targeting. This can be used by using dumpbin.exe which is in your visual studio VC\bin directory. use the -headers option to dump all your functions. Look for the machine entry for each function. it should include x64 if it's a 64 bit build.

      In visual studio, select Tools > Options from the main menu. select Projects and Solutions > VC++ Directories. Select x64 from the Platform dropdown. Make sure that the first entry is: $(VCInstallDir)\bin\x86_amd64 followed by $(VCInstallDir)\bin.

      Delete
  63. Certainly the best tutorial on the web. I am currently developing Android application using OpenCV

    ReplyDelete
  64. great tutorial, many thanks!

    ReplyDelete
  65. hellow. I'm new to opencv. I need to know how could i give a image path to the imread function. Thanks

    ReplyDelete
    Replies
    1. Mat img = imread("C:\\Users\\TharinduJ\\Desktop\\Cute Dolls37.jpg");

      Delete
  66. can you show us how to display frame from a video?

    ReplyDelete
  67. 'openCvFirstProject.exe' (Win32): Loaded 'C:\Windows\System32\advapi32.dll'. Cannot find or open the PDB file.
    'openCvFirstProject.exe' (Win32): Loaded 'C:\Windows\System32\msvcrt.dll'. Cannot find or open the PDB file.
    The thread 0x2604 has exited with code -1073741701 (0xc000007b).
    The thread 0x2334 has exited with code -1073741701 (0xc000007b).
    The thread 0x131c has exited with code -1073741701 (0xc000007b).
    The program '[9720] openCvFirstProject.exe' has exited with code -1073741701 (0xc000007b).

    ReplyDelete
  68. Even I get the same error,any one can help ?
    What is the issue with visual studio symbol loading ??????
    AlwYs that comes up and program exits

    ReplyDelete