FLTK display image - c++

I am making a simple FLTK application (in windows) that needs to display PNG images in FL_Window, load them one after the other from disk. I have this code as a starting point but its not displaying the image which I can confirm is in the same folder as the executable:
int main(int argc, char **argv)
{
Fl_Window *main_window = NULL;
fl_register_images();
flw = new Fl_Window(1680,1050,title);
Fl_Shared_Image *a = Fl_Shared_Image::get("picture.png");
if (a != NULL)
{
cout << "Image loaded" << endl;
}
else
{
cout << "No image loaded" << endl; // <==== This is printed
}
flw->begin();
// add image to window code here, not sure what to write but
// image doesnt even load
flw->end();
main_window->show();
int fl_ret = Fl::run();
return fl_ret;
}
Any help greatly appreciated ..

Fl_Shared_Image class used for
"Find or load an image that can be shared by multiple widgets."
use Fl_PNG_Image class
int main() {
fl_register_images();
Fl_Window win(720,486);
Fl_Box box(10,10,720-20,486-20);
Fl_PNG_Image png("picture.png");
box.image(png);
win.show();
return(Fl::run());
}

You only loaded the image.
Birol's answer loaded and displayed box.image(png); the image.
If you are using an IDE, your IDE might be running an executable in another directory.
Give the absolute address of your image, and it should work.

Related

Program with SPI_SETDESKWALLPAPER Function Only Changes the Desktop Background to the Color Black when Trying to Change it to an Image using C++

I am trying to change the desktop background/wallpaper to a different image with a .png file. Although when I run the program, the background turns to solid black instead.
I am certain that I typed the file name, "ksa.png", correctly in my code to be the image I want to be on my background. I used an if condition to write out the last error on a file when the error occurred and used an else condition to write out "Success" if no errors occurred; but when I run the program, it writes "Success" to the file. I have thought about using a .jpg file instead, thinking that maybe .png files just don't work. I'll give an update when I tried using that.
#include <windows.h>
#include <fstream>
int main () {
const wchar_t *filenm = L"ksa.png";
std::ofstream log;
if (SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (void*)filenm, SPIF_UPDATEINIFILE) == FALSE) {
log.open("log.txt");
log << "Error: " << GetLastError();
log.close();
}
else {
log.open("log.txt");
log << "Success";
log.close();
}
return 0;
}
When I run this program, the desktop background is suppose to be set as the image "ksa.png". Instead it's solid black. Any help is appreciated for making this work, thank you.
UPDATE
Okay so I updated the code to where it would run a .jpg file and I'm still getting the same result. Also I moved the line log.open("log.txt") command before the SystemParametersInfo() function like Remy Lebeau suggested and it still writes out "Success" to the file. I'm still having the same problem.
Here is my updated code:
#include <windows.h>
#include <fstream>
int main () {
const wchar_t *filenm = L"3.jpg";
std::ofstream log;
log.open("log.txt");
if (SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (void*)filenm, SPIF_UPDATEINIFILE) == FALSE) {
log << "Error: " << GetLastError();
log.close();
}
else {
log.open("log.txt");
log << "Success";
log.close();
}
return 0;
}
Emmmm,there is a problem with your picture path. I've tried your code. You can't get pictures under relative paths unless you use absolute paths.
Like Cody Gray♦'s judgment .
const wchar_t *filenm = L"C:\\Users\\strives\\Desktop\\timg.bmp";

imread couldn't find image

I use visual studio 2013 and opencv. I run a simple code that reads a image and shows it. I add the image to resource files of my project from Donloads file. When I run below code, image.data is empty.
#include "stdafx.h"
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\opencv.hpp>
int _tmain(int argc, _TCHAR* argv[])
{
cv::Mat image;
image = cv::imread("im.png", CV_LOAD_IMAGE_COLOR); // Read the file
if (!image.data) // Check for invalid input
{
std::cout << "Could not open or find the image" << std::endl;
return -1;
}
cv::namedWindow("Display window", cv::WINDOW_AUTOSIZE);// Create a window for display.
imshow("Display window", image); // Show our image inside it.
cv::waitKey(0); // Wait for a keystroke in the window
return 0;
}
Try : C:\\Users\\pr\\Downloads\\im.png that should work if the path is correct.
This is because the default working directory in VS is the location of your vcxproj file (value of $(ProjectDir)).
If you would like to start your application through VS (i.e. by F5 key) then you should overwrite the debugging parameters of your project. For example you can set the application's working directory to the location of the program (exe) that the linker creates by setting the value of Project menu -> Properties -> Configuration properties -> Debugging -> Working directory to $(OutDir).

imread in OpenCV

I found that this question has been asked many times here, but I haven't found any solution or work-around to this problem. Here's my code (copied from here: http://docs.opencv.org/doc/tutorials/introduction/display_image/display_image.html):
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int _tmain(int argc, char** argv)
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
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;
}
namedWindow( "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;
}
I compiled this using Visual Studio 2008 and 2010, and got different results (both don't work). The program compiled using VS 2008 has run-time error at imread(), and the other displays the message "Could not open or find the image".
Anybody can help me with this?
The problem here is your main() function._tmain does not exist in C++. main does.
_tmain is a Microsoft extension. Here is a nice explanation of these two methods.
Further more if you want to add default argument in Visual studio please follow these steps.
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.
Hope this solves your problem!
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
if( argc != 2)
{
cout <<"No Commandline Aurgument Found!: Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
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;
}
namedWindow( "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;
}
set argv[1] to be a known image page "C:\test.jpg"
Ok, Ive read all the comments and I'm going to answer the main question along with the sub questions.
Why Doesn't my Code Work in VS2008?
The reason your code doesn't work in VS2008 is because you are using the compiled libraries for 2010, at least I think this is a pretty accurate assumption. If you want to be completely accurate then build the libraries, for the compiler you are using.
What is tmain & what is main
This stack overflow question answers the subject a lot better than I ever could, but effectively it is a windows specific main and does not actually exist in C++. It get removes by the compiler on compile time and converter to main.
Can you please try this: Use known-to-work image and absolute path until it works so you can be sure that there is no problem with image or relative path.
download http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png to C:\Lenna.png
rename your main function to something else and try this: if it does not work, please tell me the displayed name of the output window.
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
Mat image;
image = imread("C:/Lenna.png", CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.cols ) // Check for invalid input
{
cout << "Could not open or find the image. Press any key to exit." << std::endl ;
cv::waitKey(0)
return -1;
}
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
Please try this and if it does not work, please tell me the displayed name of the output window.

First steps with opencv and visual studio

i'm starting using opencv with visual studio, these are my sw components and environment:
windows 8.1 (64bit)
visual studio professional 2013
opencv 2.4.9
first of all i've downloaded opencv and extracted all in "C:\OpenCV-2.4.9", then i've created a new environment variable (user variable) OPENCV_DIR with value "C:\OpenCV-2.4.9\opencv\build" and added the string ";%OPENCV_DIR%\x86\vc12\bin" to the existing environment variable Path.
first question: do i should set x64 instead of x86 in the above string value? in general, are all settings right?
then i've created a new win32 console project with visual studio, it creates a function _tmain(int argc, _TCHAR* argv[])inside a .cpp file named .cpp.
second question: what is the reason because i've to use such '_tmain' function instead the classic 'main' function? which is the structure of the calls when i 'run' the project? is that useful? and if i wish use the classic 'main'?
then, i've defined the following code
int main(int argc, char* argv[])
{
if (argc != 2)
{
cout << " Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], IMREAD_COLOR); // Read the file
if (image.empty()) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl;
return -2;
}
namedWindow("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;
}
when i compile and debug with visual studio everything is ok! but i cannot be able to give the requested parameter to the function... it starts and ends with output code -1 (the first if check).
moreover, if i find the .exe and try to run it using the prompt, an error occurs:
"Impossible to run the program because opencv_core249d.dll is not present...... try to reinstall the program".
it is clear that these are common problems but i'm finding these issue so hard... thanks to all!

Newbie problem with QT C++ - Qimage dont work?

I am trying to do console application to read pixels from image:
#include <QtCore/QCoreApplication>
#include <QtGui/QImage>
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QImage *img = new QImage("adadad.jpg");
//std::cout << "Type filename:" << std::endl;
img->isNull();
return a.exec();
}
That doesn't work I got: (IT doesn't compile, but anyway file isn't exist yet...)
File not found: tmp/obj/debug_shared/main.o:: In function `main':
What is going on? Is it impossible to use Qimage with console app?!
EDIT:
screen
It is possible to use QImage in a console application, you must make sure that QtGui is configured though. If you chose a console app, your .pro file might contain something like
CONFIG += console
QT -= gui
If that's the case, remove the QT -= gui line.
QImage("adadad.jpg");
Will probably look for a file called adadad.jpg on the current working directory for your application. Check if that file is present. Otherwise, use a fully qualified path.
img->isNull() doesn't do anything on it's own, try this instead:
if(img->isNull())
std::cout << "Image isNull!\n";
else
std::cout << "Image loaded\n";
My guess is that the local directory of the executable is not the same as the location of that image, so Qt can't find the file. Try specifying the complete path.
EDIT: Ahh... didn't realize it was a compilation problem. That looks suspiciously like a moc issue. What build system are you using? and can you confirm that the moc step is executing?
This modification of your code will compile and run as expected if there is a valid image file in the current working directory when you run the app. It will display Image loaded
#include <QtGui/QImage>
#include <iostream>
int main(int argc, char *argv[])
{
QImage *img = new QImage("adadad.jpg");
if(img->isNull())
std::cout << "Image is null";
else
std::cout << "Image loaded";
return 0;
}
You do not need to create an instance of QCoreApplication unless you have subclassed it and put your program code in that subclass.
Update:
Your program does not exit so you are probably getting that compile error because it can't replace the executable because it is still running (and locked). The file locking is more likely to be an issue under Windows.
An important note when you are loading a file using directly "adadad.jpg" in your code. Even if you put the file inside the debug/release folder, QImage will always be null if loaded this way.
I run into this problem yesterday and I fixed it by using the Qt library to get the full path: QCoreApplication::applicationDirPath().
There is two way to achieve that, first one is when you create the img object.
QImage img( QCoreApplication::applicationDirPath() + "adadad.jpg");
if( img.isNull())
{
qDebug() << "Loading Error - file: adadad.jpg.";
return false;
}
or using the load function
QImage img;
if( !img.load(QCoreApplication::applicationDirPath() + "adadad.jpg"))
{
qDebug() << "Loading Error - file: adadad.jpg.";
return false;
}