Open CV, image loading issue on Mac OS X - xcode

I think my question is pretty basic. I was trying to get Open CV to install on my OSX Lion. I had followed all the steps recommended on this link http://tilomitra.com/opencv-on-mac-osx/
However, when I run the C++ code recommended on the website in Xcode, it fails to load an image with the cvLoadImage( ) function. I have placed my image in the project folder (as recommended). Here is the code I was running:
// Example showing how to read and write images
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv/cvaux.hpp>
int main(int argc, char** argv)
{
IplImage * pInpImg = 0;
// Load an image from file - change this based on your image name
pInpImg = cvLoadImage("my_image.jpg", CV_LOAD_IMAGE_UNCHANGED);
if(!pInpImg)
{
fprintf(stderr, "failed to load input image\n");
return -1;
}
// Write the image to a file with a different name,
// using a different image format -- .png instead of .jpg
if( !cvSaveImage("my_image_copy.png", pInpImg) )
{
fprintf(stderr, "failed to write image file\n");
}
// Remember to free image memory after using it!
cvReleaseImage(&pInpImg);
return 0;
}
So during execution, the code builds successfully, but always ends up in the following loop and halts execution:
if(!pInpImg)
{
fprintf(stderr, "failed to load input image\n");
return -1;
}
Has anyone faced such a problem before? How could I solve this?
(During installation of 'Macports' and 'Cmake', I had received an alert saying that Xcode was not installed or was installed without 'Command Line Tools'. But as per another thread on this forum, I had installed these from the Xcode-->Preferences-->Downloads folder on installing Xcode.
However, still during installation, 'Macports' and 'Cmake' gave me warnings, but installed anyway. But could this be the issue? )
Thank you!

The problem is that you're putting the image into the project folder instead of the folder containing your executable. You can either put the image file in the folder with the executable or put the full path to the image in the call to cvLoadImage.
Older versions of Xcode put the executable in either the build/Debug or build/Release folder in the project folder. Newer versions of Xcode put the build products in the project folder in the DerivedData folder. You can find the DerivedData folder by going to File -> Project Settings… and clicking the arrow next to the folder path:

In the project folders, go to 'Products' > right click on the executable file > 'Show in Finder' > put the input image there. The output image ('.png' file) will go here as well.

Related

Firefox auto deletes xpi from Profiles folder on load

I created a Firefox add-on.
Now when I place it into the current Profiles folder manually and load the browser, it prompts for installation.
But when I copy the .xpi file to the current Profiles folder using a VC++ executable and then reload the browser, it auto-deletes the .xpi file.
Please guide where this is going wrong. I believe copying via the VC++ executable is raising some issue.
Here's the code for the same :
FILE* pnReadFile = fopen(spnDirPtr->d_name, "r");
if (pnReadFile)
{
char strDestFileName[MAX] = { 0 };
sprintf(strDestFileName, "%s\%s", arDestPath, xpi);
FILE* pnWriteFile = fopen(strDestFileName, "w"); /*File Pointer to write in file*/
if (pnWriteFile)
{
char buffer[MAX] = { 0 }; /*Buffer to store files content*/
while (fgets(buffer, MAX, pnReadFile))
{
fputs(buffer, pnWriteFile);
}
fclose(pnWriteFile);
}
else
{
printf("\n Unable to open file %s", strDestFileName);
}
fclose(pnReadFile);
}
i was able to figure out the same. The file I/O methods did not copy the xpi completely because of which the xpi went corrupt and thus getting automatically deleted on firefox load. I used system() copy command and was able to accomplish the same suuccessfully.

Packaging mp3 files into Qt app

Since I found mp3 files can't be retrieved by QMediaPlayer from resource, I figure it out to use them as local file. I use this cmd with a little modification to copy files into installation directory:
copydata.commands = $(COPY_DIR) $$PWD/resources/sound $$OUT_PWD/HomeControl.app/Contents/MacOS
first.depends = $(first) copydata
export(first.depends)
export(copydata.commands)
QMAKE_EXTRA_TARGETS += first copydata
So those files existed in the /HomeControl.app/Contents/MacOS/sound. From Qt Creator the app plays the mp3 perfectly, but if executed from the build directory it doesn't play the mp3 at all! Really don't know why.
arm away btn clicked
current media: "file:///Dev/Qt_Sample/build-test_widgets-Desktop_Qt_5_3_0_clang_64bit-Debug/HomeControl.app/Contents/MacOS/sound/System_Arm_Away.mp3"
disarm stay btn clicked
current media: "file:///Dev/Qt_Sample/build-test_widgets-Desktop_Qt_5_3_0_clang_64bit-Debug/HomeControl.app/Contents/MacOS/sound/System_Disarmed.mp3"
Is there any difference from executing in the Qt creator & directly from the App? Like debug mode or what?
Or is there other ways to correctly deploy mp3 files into the bundle?
This is how I build in resources for OS X bundles:
mac {
Resources.files = dirInTheProjectDirectory
Resources.path = Contents/MacOS
QMAKE_BUNDLE_DATA += Resources
}
You can call Resources whatever you fancy.
That copies them to Contents/MacOS/dirInTheProjectDirectory. You can then access them with QDir(QCoreApplication::applicationDirPath()+"/dirInTheProjectDirectory/");
The likely problem is when you're accessing them in your code you're pointing to the path in the build directory, and not the path that's within your bundle.
Here's my final solution. Thank you #nicholas-smith.
In the .pro file:
# deploy with mp3 folder
mac {
Resources.files = ./resources/sound
Resources.path = Contents/MacOS
QMAKE_BUNDLE_DATA += Resources
}
Then accessing those file with:
// pass the path of mp3 to the QMediaPlayer
playAudio(QCoreApplication::applicationDirPath()
+ "/sound/System_Disarmed.mp3");
From log it shows:
arm away btn clicked
current media: "file:///Dev/Qt_Sample/build-test_widgets-Desktop_Qt_5_3_0_clang_64bit-Debug/HomeControl.app/Contents/MacOS/sound/System_Arm_Away.mp3"

Xcode command line tool - how to run in terminal?

When you create a Command Line Tool project in Xcode you get this, in main.m:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
#autoreleasepool {
// insert code here...
NSLog(#"Hello, World!");
}
return 0;
}
I can run this from Xcode. But I want to compile it so that I can run it from Terminal. What are the steps?
Assuming your executable is named "my_program", and it's in the "/foo/bar/Debug" directory:
cd /foo/bar
./my_program
If you aren't sure how to find the program file itself, you can right-click it (i.e.: the "product") and "Show in Finder" as shown in this screenshot:
You can have the terminal launch everytime you run the application through editing the scheme. I believe this became available from Xcode 8.0.
Access Edit Scheme
Switch Console from Use Xcode to Use Terminal
In Xcode 9 you can try the following (works for me in June 2018):
instead of 'edit scheme' click 'new scheme', give it a name and save
now choose that new scheme you've just created and click 'edit
scheme'
go to the 'Info' tab and in a menu 'Executable' choose
'Other...'
in file window go to search input field and type
'terminal' and click on its icon when you find it. Now you should see 'Terminal.app' in 'Executable' field
go to the 'Arguments' tab, click on + and copy and paste this line there: ${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME}
click 'close' and run your program with your new scheme selected
Normally Xcode will open terminal for you. If not, you may also turn off any debug related fields in the 'Info' tab. Hope this helps!
Full tutorial here:
https://www.raywenderlich.com/163134/command-line-programs-macos-tutorial-2

Where is the "App Bundle" looking for my media folder? Folder found when running app from Qt Creator, "file not found" when using Terminal

I'm having a little trouble making my app bundle work from the terminal or just double clicking it.
This App actually compiles, links and runs perfectly within the Qt Creator IDE. But, if I try to open it from the terminal I get a "media/file.x file not found" error. The App bundle nor the /Contents/MacOS/executable is finding the "media" folder that is supposed to be beside the executable.
In my app I do something like:
openFile("media/file.x");
In Windows and Linux, this file WILL be found if the "media" folder is exactly in the same hierarchical position of the executable (beside it). On the Mac I have discovered it works differently cause Qt Creator builds an "App Bundle" and the actual executable is inside the /Contents/MacOS folder, so I copied the "media" manually there. This worked without any hassle when "playing" my app from the Qt Creator but as mentioned before it doesn't work when running the bundle itself.
So does anyone know where or how can I homogenize the look for this "media" folder so it works on both: Qt Creator and the App bundle?
Lately, I have been using the following command to "install" the folder on the bundle.
mac {
MediaFiles.files = media
MediaFiles.path = Contents/MacOS
QMAKE_BUNDLE_DATA += MediaFiles
}
Thanks for your help.
After looking for a few days I found a couple of posts discussing the relative path problem. I was just not searching with the right words... The answer is displayed in:
Relative Paths Not Working in Xcode C++
http://www.experimentgarden.com/2009/06/how-to-load-resource-from-your.html
Basically it is necessary to add this:
//On the include part
#ifdef __APPLE__
#include "CoreFoundation/CoreFoundation.h"
#endif
// On the main of your app
// This makes relative paths work in C++ in Xcode by changing directory to the Resources folder inside the .app bundle
#ifdef __APPLE__
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
char path[PATH_MAX];
if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX))
{
// error!
}
CFRelease(resourcesURL);
chdir(path);
std::cout << "Current Path: " << path << std::endl;
#endif
// ----------------------------------------------------------------------------
IMPORTANT:
The chdir, changes the working path of the app, so it is not necessary to change you relative code after that...

C++ includes problem with NetBeans 7.0 under MacOS X using sshfs

That's what we have
1) MacOS X Lion, NetBeans 7.0
2) C++ project with source files located on sshfs mounted volume (using Fuse4X)
3) Some libraries header files alse located on sshfs mounted volume.
When I open some source file Code Assistance displays a lot of errors "Couldn't resolve an include" near all lines with include directive and file path containing uppercase characters
#include "SomeModule.h" // error here
#include <Lib/Header.h> // also error here
#include <otherlib/file.h> // wow, no errors
Of course directories containing these headers are added to include path and if I hold 'cmd' button and hover any of these includes correct include path is displayed, but NetBeans still says he couldn't locate that file.
Besides if I create 2 files: File.h и file.h on sshfs volume and try to include them
#include <sshfs/file.h> // includes file.h
#include <sshfs/File.h> // displays an error
My question is what should I do for NetBeans could see, parse and provide content assistance in projects located on sshfs volume?

Resources