I run this sample cod, and i get run time exception
#include "stdafx.h"
#include <iostream>
using namespace std;
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
int _tmain(int argc, char** argv)
{
//IplImage* img = cvLoadImage( "Walk1001.jpg" ,1 );
IplImage* img =cvLoadImage( argv[1] );
if(!img)
cout << "Could not open or find the image" << endl ;
cvNamedWindow( "Example1", 1 );
cvShowImage( "Example1", img );
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "Example1" );
return 0;
}
when i use IplImage* img = cvLoadImage( "Walk1001.jpg" ,1 ); instead of this IplImage* img =cvLoadImage( argv[1] ); program runs fine. but otherwise i got error.
What is to do with argv. i have came across many programs in which image is loaded through some argv[] syntex! how to use this array (argv[]) or what else?
to use the argv array, you've got to supply arguments to your program (from the cmdline, or similar)
prog.exe Walk1001.jpg 19
now argv holds 3 elements, [ "prog.exe", "Walk1001.jpg", "19" ], and argc==3
in your program , do:
char * imgPath="Walk1001.jpg"; // having defaults is a good idea
if ( argc > 1 ) // CHECK if there's actual arguments !
{
imgPath = argv[1]; // argv[0] holds the program-name
}
int number = 24;
if ( argc > 2 ) // CHECK again, if there's enough arguments
{
number = atoi(argv[2]); // you get the picture..
}
sidenote: you seem to be a beginner (nothing wrong with that!), the opencv api has changed over the years, please don't use IplImage* and cv*Functions(the 1.0 api),
use cv::Mat and functions from cv:: namespace.
using namespace cv;
int main(int argc, char** argv)
{
char * imgPath="Walk1001.jpg";
if ( argc > 1 )
{
imgPath = argv[1];
}
Mat img = imread( imgPath );
if ( img.empty() )
{
cout << "Could not open or find the image" << endl ;
return 1;
}
namedWindow( "Example1", 1 );
imshow( "Example1", img );
waitKey(0);
// no, you don't have to release Mat !
return 0;
}
I am getting run time exception. the error is line 2482 unknown function in array.cpp? I think i get this message from imshow after debugging. I'm using Mat img=imread("walk100.jpg"); but the img.total() returning NULL. why imread is returning NULL. cvload is working perfectly.
I got this solved, on the web i came to know about ***d.dll. while adding dll files, I omitted the d, that is for release mode not for debug mode. So I just place "d", like opencv_core244d.dll instead of opencv_core244.dll
thanks all for your contribution
Related
I have write SDL code in vscode to pop the window and when i
compile it using g++ file.cpp -lSDL2 no error was thier but
when i run it by
"./a.out" it does not show any thing to me.
#include<iostream>
#include<SDL2/SDL.h>
using namespace std;
// main function
int main( int argc, char *args[] )
{
// Initialize SDL video sub system
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
cout << endl << SDL_GetError();
// create a window and pop it
SDL_Window *window = SDL_CreateWindow( "SDL Linux", 100, 100, 700, 500, SDL_WINDOW_SHOWN );
// wait for 6 second
SDL_Delay(6000);
return 0;
}
//it should pop a window
I have tried below simple program to use XShmGetImage to get the desktop image.
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include <X11/extensions/XShm.h>
#include <X11/extensions/Xfixes.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main(int argc, const char *argv[])
{
int screen;
Window root;
Display* display;
XImage* img,
int shm=0;
XShmSegmentInfo shminfo;
/* My Desktop Screen Resolution */
int width=1360;
int height=768;
display = XOpenDisplay(getenv("DISPLAY"));
shm = XShmQueryExtension(display);
if ( shm) {
printf ("Ha... QueryExtension Successful..\n");
int scr = XDefaultScreen (display);
printf ("\n Screen Number is %d ", scr);
img = XShmCreateImage (display, DefaultVisual(display, scr),
DefaultDepth ( display, scr),
ZPixmap,
NULL,
&shminfo,
width,
height);
printf ("\n Bytes Per Line %d ", img->bytes_per_line);
shminfo.shmid = shmget (IPC_PRIVATE, img->bytes_per_line * img->height, IPC_CREAT | 0777);
if ( shminfo.shmid == -1 ) {
printf ("\n Can not get the shared Memory ...");
} else {
printf ("\n Greate I am able to get shared memory..");
}
shminfo.shmaddr = img->data =shmat (shminfo.shmid, 0,0);
shminfo.readOnly = False;
if (!XShmAttach (display, &shminfo)) {
printf ("\n i am unable to attach now..");
} else {
printf ("\n Super.. i am able to attach Shared memory to extension ");
}
if ( !XShmGetImage (display, RootWindow(display, DefaultScreen(display)), img, 0,0, AllPlanes)){
printf ("\n Now you should have your image in XImage");
} else {
printf ("\n Ooops.. Something wrong.");
}
}
Output:
Ha... QueryExtension Successful..
Screen Number is 0
Bytes Per Line 5440
Greate I am able to get shared memory..
Super.. i am able to attach Shared memory to extension
Ooops.. Something wrong.
Unfortunately, XShmGetImage fails, and no information is displayed. Please help.
There was a blunder from my side. Actually, it works properly, and I misinterpreted the return value of XShmGetImage API().
The correct one is
if ( !XShmGetImage (display, RootWindow(display, DefaultScreen(display)), img, 0,0, AllPlanes)){
printf ("\n Ooops.. Something wrong.");
} else {
printf ("\n Now you should have your image in XImage");
}
So I'm trying to do some image processing on visual studio 2013 and wrote a piece of code. When I use the built-in local windows debugger to run the code, the code works and produce a output file in my project folder. But when I run the '.exe' file, built by the same code through visual studio 2013, in cmd with the same argument, I couldn't find any output file. And based on the speed, the '.exe' file should be running, but it just doesn't produce anything..... What could be happening... Helps appreciated...
code shown below...it's just calling some other function and save the output.
#include "saliency/saliency.h"
#include <cstdio>
#include <iostream>
#include <string>
int main( int argc, char * argv[] ) {
if (argc < 2) {
printf("Usage: %s image\n", argv[0] );
return -1;
}
Mat im = imread( argv[1] );
Saliency saliency;
Mat_<float> sal = saliency.saliency( im );
double adaptive_T = 2.0 * sum( sal )[0] / (sal.cols*sal.rows);
while (sum( sal > adaptive_T )[0] == 0)
adaptive_T /= 1.2;
printf(argv[1]);
std::string a (argv[1]);
std::string b ("\\");
unsigned found = a.find_last_of(b);
std::string c = a.substr(found+1);
std::string d = ("saliency_");
std::string e = d + c;
//imshow( "im", im );
//imshow( "sal", sal );
imwrite(e, sal*255);
//imshow( "T", sal > adaptive_T );
waitKey();
return 0;
}
Update:
I find the output file at C:\Windows\SysWOW64.... Anyone knows why it's saved there?
Actually the coding below is what I tried to execute. When I tried to execute this using Microsoft Visual Studio 2012, there is an error.
Error error LNK2019: unresolved external symbol _cvPyrSegmentation referenced in function "void __cdecl START_SEGMENT(int)" (?START_SEGMENT##YAXH#Z)
Error error LNK1120: 1 unresolved externals
But I asked my friend to execute this code using his laptop, it works just fine.No error. But he is using Microsoft Visual Studio 2010 instead of me using Visual Studio 2012..I wonder why there is an error when I execute this code but when my friend execute it, no error.
So I removed some header and the coding looks like the one that I ask from the beginning( from this Question.The one with ""Error error C3861: 'cvPyrSegmentation': identifier not found ""..
After I removed some header, there is no more Error LNK1120. But there is ""Error error C3861: 'cvPyrSegmentation': identifier not found""...
Blank ...Too many error for a newbie.Learning process never be easy .yeahhh!
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/legacy/legacy.hpp>
#include <stdio.h>
static void help(void)
{
printf("\nThis program present the function of pyramid segmentation which is
cvcvPyrSegmentation()\n""we can controlled the value of threshold by creating
the taskbar\n""Usage :\n");
}
IplImage* image[2] = { 0, 0 }, *image0 = 0, *image1 = 0;
CvSize size;
int w0, h0,i;
int threshold1, threshold2;
int l,level = 4;
int sthreshold1, sthreshold2;
int l_comp;
int block_size = 1000;
float parameter;
double threshold;
double rezult, min_rezult;
int filter = CV_GAUSSIAN_5x5;
CvConnectedComp *cur_comp, min_comp;
CvSeq *comp;
CvMemStorage *storage;
CvPoint pt1, pt2;
static void START_SEGMENT(int a)
{
(void) a;
cvPyrSegmentation(image0, image1, storage, &comp, level, threshold1+1,
threshold2+1);
cvShowImage("Segmentation", image1);
}
int main( int argc, char** argv )
{
char* filename;
help();
filename = argc == 2 ? argv[1] : (char*)"C:/Users/acer/Documents/Visual Studio
2012/Projects/me2.jpg";
if( (image[0] = cvLoadImage( filename, 1)) == 0 )
{
help();
printf("Cannot load fileimage - %s\n", filename);
return -1;
}
cvNamedWindow("Source", 0);
cvShowImage("Source", image[0]);
cvNamedWindow("Segmentation", 0);
storage = cvCreateMemStorage ( block_size );
image[0]->width &= -(1<<level);
image[0]->height &= -(1<<level);
image0 = cvCloneImage( image[0] );
image1 = cvCloneImage( image[0] );
// segmentation of the color image
l = 1;
threshold1 =255;
threshold2 =30;
START_SEGMENT(1);
sthreshold1 = cvCreateTrackbar("Threshold1", "Segmentation", &threshold1, 255,
START_SEGMENT);
sthreshold2 = cvCreateTrackbar("Threshold2", "Segmentation", &threshold2, 255,
START_SEGMENT);
cvShowImage("Segmentation", image1);
cvWaitKey(0);
cvDestroyWindow("Segmentation");
cvDestroyWindow("Source");
cvReleaseMemStorage(&storage );
cvReleaseImage(&image[0]);
cvReleaseImage(&image0);
cvReleaseImage(&image1);
return 0;
}
#ifdef _EiC
main(1,"pyramid_segmentation.c");
#endif
Having fixed your compilation problem, you now have a linker problem.
You need to follow the instructions in the OpenCV documentation to find out how to link their libraries to your binary.
You'll start with -lcv and potentially have to add some additional libraries.
While I was working on a project, I noticed that I am not able to load images. Here is the simple code that I used to check:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.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 -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;
}
Mat gray_image;
cv::cvtColor(image, gray_image, CV_RGB2GRAY);
imwrite("Gray_Image.jpg",gray_image);
return 0;
}
Here is its output when I execute it:
root#beaglebone:~# ./tryit lena.jpg
Could not open or find the image
I tried to directly use the address of the image ("/home/root/lena.jpg") instead of argv[1] but nothing changed.
What can be the problem?
ps: I am cross-compiling this OpenCV program and then running it on my BeagleBone which has Angstrom Linux installed on it. Can this problem related to it?
I solved my problem by deleting existing OpenCV libraries on my angstrom image and replacing them with the working ones.
Try saving the image with a png extension and then opening it. For some reason, files with a png extension work better than other extensions like jpg/gif.