OpenCV let user choose to open image - c++

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.

Related

Can't save Images using QImage

I wish to use QImage in Qt to load and save images. While I can load an image, for whatever reason it wont let me save the image.
So I started writing a simple code, made a simple png test file using paint, put it into the same folder as the project itself.
#include <QImage>
#include <iostream>
int main(){
QImage image;
image.load("test.png");
if (image.isNull()){
std::cout << "ERROR!\n";
}
else{
std::cout << "IMAGE LOADED!\n";
}
image.save("test1.png");
return 0;
}
During running the program I get the message of "IMAGE LOADED!" from the application output, however when I check the folder I expect the same image saved as test1.png, which doesn't appear at all.
So, how do I actually save an image? What did I miss?
In the other comments mentioned the image got saved into the working directory, which is not the same as the folder where the project is located.
In my specific case I got the image by adding a full path to the project like
int i = image.save("C:\\Users\\UserName\\Documents\\QT\\testing_ground_cpp\\" + string);
to find the image.
Ideally I should directly access with the working directory.

Show image on windows using imagemagick

I am using ImageMagick for a few modifications in images. My requirement is to capture desktop, update the captured image and show in the window (Canvas, Form or simple Win32 API image).
Everything working perfectly except showing a converted image on the window.
As per this discussion, display functionality of image magick is only supported by Linux and Mac (Please correct me if anything wrong).
The same forum suggested to use im_display to show the image on window. However, I am not able to locate any function related im_display() in the image magick library (Please correct me here if any additional include required).
When I am trying to call following code from visual studio then getting "delegate library support not built-in '' (X11) # error/display.c/DisplayImages/16224" error:
int main(int argc, char **argv)
{
try
{
Magick::InitializeMagick(NULL);
Image screen("screenshot:");
screen.display();
}
catch (exception &error_)
{
cout << "Caught exception: " << error_.what() << endl;
return 1;
}
return 0;
}
So the following are two primary questions:
Is it possible to show an image using image magick on windows (Form or canvas or win32)
If it's not possible then HBITMAP will help to render an image on windows. However, I am not able to find a way to convert Image Magick data to BITMAP. Can you please provide suggestions?

OpenCV C++ Xcode on Mac Mojave error NSCameraUsageDescription

I have installed the opencv on MacPro and am trying to write a program which allow me to activate the cam, it is only to test opencv it build successfully, however, the cam is not on and I receive this message
saved enable noise cancellation setting is the same as the default(=1) pentest[30782:364297] [access] This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSCameraUsageDescription key with a string value explaining to the user how the app uses this data
my code is:
#include <iostream>
#include<opencv2/opencv.hpp>
using namespace cv;
int main(int argc, const char * argv[]) {
// insert code here...
VideoCapture cap(0);
while(true){
Mat Webcam;
cap.read(Webcam);
imshow("webcam",Webcam);
}
return 0;
}
I recently posted another answer that addresses this situation:
Put the Info.plist file with the desired NSCameraUsageDescription, NSMicrophoneUsageDescription (or others) with the assembled file from XCode (See screenshots below). For the Release and Debug versions.

Loading and displaying image using opencv in vs2012

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

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