Let's display an image using opencv in visual studio. Our previous tutorial was how to setup opencv for visual studio. 
- First open your visual studio and create a new new project.
 - Following examples will guide you on image loading.
 
Example 1:
=======================================================================
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, const char** argv)
{
       Mat Image = imread("C:/Users/hashan/Desktop/hashan.JPG", CV_LOAD_IMAGE_UNCHANGED);
       //Mat Image =
imread("hashan.JPG", CV_LOAD_IMAGE_UNCHANGED);
       if (Image.empty()) 
       {
              cout << "Erro" << endl;
              return 0;
       }
       namedWindow("NewWindow", CV_WINDOW_NORMAL);
       imshow("NewWindow", Image); 
       waitKey(0);  
       destroyWindow("MyWindow"); 
       return 0;
}
=======================================================================
Explanation 
We create a matrix called “Image”.  Find the image where it locates ( by given
address) and store the image to the matrix we created. 
If your image location is as same as the project location, just
give the image name. 
·        
//Mat Image =
imread("hashan.JPG", CV_LOAD_IMAGE_UNCHANGED);
You can load an image in five different ways.
Choose the image type as
your requirement. The followings will guide you to choose the image type. 
CV_LOAD_IMAGE_UNCHANGED  
 | 
  
Loads the image as is  
 | 
 
CV_LOAD_IMAGE_GRAYSCALE  
 | 
  
Loads the image in grayscale 
 | 
 
CV_LOAD_IMAGE_COLOR  
 | 
  
Loads the image in RBG format 
 | 
 
CV_LOAD_IMAGE_ANYDEPTH  
 | 
  
Loads
  image in 16-bit/32-bit when the input has the
  corresponding depth, otherwise convert it to 8-bit. 
 | 
 
CV_LOAD_IMAGE_ANYCOLOR  
 | 
  
The image is read in any possible color format. 
 | 
 
·        
if (Image.empty())
Checking whether image is successfully loaded or not 
·        
namedWindow("NewWindow", CV_WINDOW_NORMAL);
Create a new window which named NewWindow
CV_WINDOW_NORMAL  
 | 
  
Can resize window 
 | 
 
CV_WINDOW_AUTOSIZE 
 | 
  
Cant resize window 
 | 
 
CV_WINDOW_FREERATIO  
 | 
  
Adjusts the image with no respect to its ratio 
 | 
 
CV_WINDOW_KEEPRATIO 
 | 
  
Keeps the image ratio. 
 | 
 
·       
imshow("NewWindow", Image);
Display the stored image
·        
waitKey(0);  
Wait infinite time or wait until press the keyboard
button.
Note
waitKey(x);
when x > 0  then  
x milliseconds time wait.
When x <=0 then wait infinite time or wait until press the
keyboard button. 
·       
destroyWindow("MyWindow");
Distroy
the window which name is “Mywindow”
Example
2
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
       Mat Image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // read the Image
       //Mat Image =
imread("hashan.JPG", CV_LOAD_IMAGE_UNCHANGED);
       if (!Image.data)   // check for invalid image
       {
              cout << "Erro" << endl;
              return 0;
       }
       namedWindow("NewWindow");
       imshow("NewWindow", Image); 
       waitKey(0);  
       destroyWindow("MyWindow"); 
       return 0;
}
Before run this code you need to follow the below
steps. 
·        
First Right click the on project and go to
properties.
Then go to Debugging à Command Arguments
and give your image location and image name as follows.
Example
3
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
       IplImage *Image= cvLoadImage("C:/Users/hashan/Desktop/hashan.jpg", CV_LOAD_IMAGE_COLOR); //load image
       cvNamedWindow("NewWindow"); // create a new window
       cvShowImage("NewWindow", Image);  // display the
image
       waitKey(0);  
       destroyWindow("MyWindow"); 
       return 0;
}
the video demo is shown in below video.
- Thank you very much for reading this tutorial. Our next tutorial is about how to develop python using visual studio.
 - If you have problems regarding this tutorial feel free to make a comment below or contact us by sending message through our facebook page or by sending email to this address progtpoint@gmail.com
 - Follow us on facebook and twitter also subcribe us youtube
 







These day I follow your Image Processing Tutorials. It explained step by step very clearly using both article and video demo. I think this is very helpful for beginners and also me. I am very thankful. This is a great work and keep it up...!!!
ReplyDeleteThank you very much for your feedback. :) We will upload more videos on every Saturday on our youtube channel. Stay with us.
DeleteLearn how to load and display images using OpenCV in C++ with just a few simple lines. This is perfect for visual projects, especially when showcasing products like those found in shop sneaker deals .
ReplyDelete