Error with OpenCV - c

I'm trying to run simple code with installed OpenCV library
And have an error: The application was unable to start correctly (0xc0150002).
visual studio 2010
OpenCV 2.1.0
I've searched everywhere but cant find how to solve this problem...
This is my code. What can I do?
#include "stdafx.h"
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#ifdef _EiC
#define WIN32
#endif
static CvMemStorage* storage_face = 0; //Memory Storage to Sore faces
static CvHaarClassifierCascade* cascade_face = 0;
void detect_and_draw( IplImage* image );
//Haar cascade - if your openc CV is installed at location C:/OpenCV2.0/
const char* cascade_name_face ="C:/OpenCV2.0/data/haarcascades/haarcascade_frontalface_alt.xml";
/////////////////////////////////////////////////////////////////////////////////
int main()
{
IplImage *image =0;
image = cvLoadImage("picci.jpg",1);
if(!image)
{
printf("Error loading image\n");
return -1;
}
cascade_face = (CvHaarClassifierCascade*)cvLoad( cascade_name_face, 0, 0, 0 );
if( !cascade_face )
{
printf("ERROR: Could not load classifier of face cascade\n" );
return -1;
}
storage_face = cvCreateMemStorage(0);
cvNamedWindow( "result", 1 );
// Call function to detect and Draw rectagle around face
detect_and_draw( image);
// Wait for key event.
cvWaitKey(0);
// release resourses
cvReleaseImage( &image );
cvReleaseHaarClassifierCascade(&cascade_face );
cvReleaseMemStorage( &storage_face);
cvDestroyWindow("result");
return 0;
}
//////////////////////////// Function To detect face //////////////////////////
void detect_and_draw( IplImage* img )
{
double scale = 2;
// create a gray image for the input image
IplImage* gray = cvCreateImage( cvSize(img->width,img->height), 8, 1 );
// Scale down the ie. make it small. This will increase the detection speed
IplImage* small_img = cvCreateImage( cvSize( cvRound (img->width/scale),cvRound (img->height/scale)),8, 1 );
int i;
cvCvtColor( img, gray, CV_BGR2GRAY );
cvResize( gray, small_img, CV_INTER_LINEAR );
// Equalise contrast by eqalizing histogram of image
cvEqualizeHist( small_img, small_img );
cvClearMemStorage( storage_face);
if( cascade_face )
{
// Detect object defined in Haar cascade. IN our case it is face
CvSeq* faces = cvHaarDetectObjects( small_img, cascade_face, storage_face,
1.1, 2, 0/*CV_HAAR_DO_CANNY_PRUNING*/,
cvSize(30, 30) );
// Draw a rectagle around all detected face
for( i = 0; i < (faces ? faces->total : 0); i++ )
{
CvRect r = *(CvRect*)cvGetSeqElem( faces, i );
cvRectangle( img, cvPoint(r.x*scale,r.y*scale),cvPoint((r.x+r.width)*scale,(r.y+r.height)*scale),CV_RGB(255,0,0),3,8,0 );
}
}
cvShowImage( "result", img );
cvReleaseImage( &gray );
cvReleaseImage( &small_img );
}

Two potential problems:
Your app cannot find the OpenCV DLLs, you might need to copy the OpenCV DLLs into your respective bin/release or bin/debug dir.
Or the OpenCV binaries were built with a different version of CRT than what you have installed on your system. Did you build OpenCV binaries on your system? That could help.

Related

How to load multiple images as command line arguments to sprintf() and cvLoadImage() in OpenCV...?

This is the code i have., which will detect the faces in the image and draws rectangle around the face.
CvHaarClassifierCascade *cascade;
CvMemStorage *storage;
void detectFaces (IplImage *newframe);
int key;
int iImg;
int Num_Images=20;
int main( int argc, char** argv )
{
string filename = M:/Trend/FaceDetection/MyCascadeFolder/haarcascade_frontalface_alt2.xml";
storage = cvCreateMemStorage(0) ;
cascade = ( CvHaarClassifierCascade* )cvLoad( filename.c_str());
**//to load a single image**
**IplImage* inImage = cvLoadImage("M:/Trend/FaceDetection775/MyImages/face1.jpg",1);**
//IplImage* inImage = cvLoadImage(argv[1],1); >> It also works fine through command line
detectFaces( inImage );
cvShowImage("FaceDetection2",inImage);
cvSaveImage("M:/FaceDetection/MyImages/OpImages/faces.jpg",inImage);
cvReleaseImage( &inImage);
// **to load no. of images or a complete folder**
char buf[20];
for(iImg=0; iImg<=Num_Images; iImg++)
{
**sprintf(buf, "M:/FaceDetection/ImagesFolder/P%04d.jpg", iImg);**
*// sprintf(buf, argv[2], iImg); // I Tried with this., but its not working*
printf("\n");
inImage = cvLoadImage(buf, CV_LOAD_IMAGE_UNCHANGED);
printf("Input Image = P%04d.jpg\n", iImg);
detectFaces( inImage );
cvShowImage("FaceDetection",inImage);
cvReleaseImage( &inImage );
key= cvWaitKey(0);
if ( key == 'q' || key == 'Q' )
return(0);
}
cvReleaseHaarClassifierCascade( &cascade );
cvReleaseMemStorage( &storage );
return 0;
}
**// the actual detectfaces() function goes like this**
void detectFaces( IplImage *newframe)
{
CvSeq *faces = cvHaarDetectObjects( newframe, cascade, storage, 1.15, 5, 0, cvSize( 30, 30 ) );
for( int i = 0 ; i < ( faces ? faces->total : 0 ) ; i++ )
{
CvRect *r = ( CvRect *)cvGetSeqElem( faces, i );
cvRectangle(newframe,cvPoint( r->x, r->y ),cvPoint( r->x + r->width, r->y + r->height ),CV_RGB( 0, 255, 0 ), 2, 8, 0 );
}
}
Here., sprintf() method is taking multiple images statically (within the program itself). But I want to pass those multiple images as a single folder through command line argument.
I try to use cvLoadImage() function, but it also takes only one image as input instead of a folder.
Also I am not able to count the number of rectangles it draws for every single face in the image., how can I do that also...?
Please help me in this regards.
I am working on face detection project "www.MahdiRezaei.com" for my academics., and I am using visual studio-2010 with C, C++ and OpenCV.
Thankz in advance.
You can change the following code to use it with arguments.
#include<Windows.h>
#include<iostream>
#include<opencv2\highgui\highgui.hpp>
using namespace cv;
using namespace std;
int main()
{
WIN32_FIND_DATA data;
HANDLE h = FindFirstFile(L"c:\\Images\\*.*", &data); // first file is the current directory
FindNextFile(h, &data); //second file is the parent directory
vector<Mat> Images;
if (h != INVALID_HANDLE_VALUE)
{
while (FindNextFile(h, &data))
{
string filename = data.cFileName;
sprintf(filename, "%s%s","C:\\Images\\", filename.c_str());
Images.push_back(imread(filename, 1));
delete[] nPtr;
}
}
else
cout << "Error: No such folder." << endl;
FindClose(h);
return 0;
}

I want to display output image in different windows in OpenCV

I want to display the same image multiple times in different windows, for which I have used a for loop but I am getting only one window display. Can anybody provide me any suggestion on how to display output images in multiple windows? Following is the code in OpenCV with C API. Here, I am simply loading an image from argv[1] and trying to display it in 4 different windows.
#include "cv.h"
#include "highgui.h"
#include <stdlib.h>
#include <stdio.h>
int main( int argc, char** argv ) {
int i;
IplImage* img = cvLoadImage( argv[1],1);
cvMoveWindow("Example1", 100, 100);
cvNamedWindow( "Example1", 1);
for(i =0; i<=4;i++) // for loop to display the same image in 4 different windows
{
cvShowImage( "Example1", img );
}
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "Example1" );
}
P.S. I have asked a similar question show multiple images in different window in OpenCV before which was not solved and the code was difficult to understand so I am trying this question with a simpler code.
Here you go
int i;
IplImage* img = cvLoadImage("/home/khashayar/Downloads/bug14.png", 1);
cvMoveWindow("Example1", 100, 100);
cvNamedWindow("Example1", 1);
for (i = 0; i <= 4; i++)
{
char str[5] = "test";
str[4] = i+48;
cvShowImage(str, img);
}
cvWaitKey(0);
cvReleaseImage(&img);
cvDestroyWindow("Example1");
for(i =0; i<=4;i++) // hmm, i<=4 will actually run 5 times ...
{
cvShowImage( "Example1", img ); // <-- same window name for all == only 1 shown
}
but, please discard the c api and use c++ !. please !
#include "opencv2/opencv.hpp"
int main( int argc, char** argv ) {
cv::Mat img = cv::imread( argv[1],1);
for( int i=0; i<4; i++ ) // for loop to display the same image in 4 different windows
{
cv::String name = cv::format("Example%d",i);
cv::namedWindow( name, 1);
cv::imshow( name, img );
}
cv::waitKey(0);
}

Julia Set - CUDA by example - help me set up the project

I have been reading CUDA by Example by Jason Sanders and Edward Kandrot and have come across chapter 4 where they solve Julia Set using CUDA.
Also, there is a source code available on a books page; https://developer.nvidia.com/content/cuda-example-introduction-general-purpose-gpu-programming-0
When I try to include all the files necessary for the project (all header files and .cu ones) it won't compile.
My usual CUDA projects that I create from scratch work without problems (VS 2010, CUDA 5.0).
Did anyone had the same problem and can you please specify each step in order to make this Julia Set set up as it should be?
P.S. Here is a code that won't work in header file:
/*
* Copyright 1993-2010 NVIDIA Corporation. All rights reserved.
*
* NVIDIA Corporation and its licensors retain all intellectual property and
* proprietary rights in and to this software and related documentation.
* Any use, reproduction, disclosure, or distribution of this software
* and related documentation without an express license agreement from
* NVIDIA Corporation is strictly prohibited.
*
* Please refer to the applicable NVIDIA end user license agreement (EULA)
* associated with this source code for terms and conditions that govern
* your use of this NVIDIA software.
*
*/
#ifndef __GPU_ANIM_H__
#define __GPU_ANIM_H__
#include "gl_helper.h"
#include "cuda.h"
#include "cuda_gl_interop.h"
#include <iostream>
PFNGLBINDBUFFERARBPROC glBindBuffer = NULL;
PFNGLDELETEBUFFERSARBPROC glDeleteBuffers = NULL;
PFNGLGENBUFFERSARBPROC glGenBuffers = NULL;
PFNGLBUFFERDATAARBPROC glBufferData = NULL;
struct GPUAnimBitmap {
GLuint bufferObj;
cudaGraphicsResource *resource;
int width, height;
void *dataBlock;
void (*fAnim)(uchar4*,void*,int);
void (*animExit)(void*);
void (*clickDrag)(void*,int,int,int,int);
int dragStartX, dragStartY;
GPUAnimBitmap( int w, int h, void *d = NULL ) {
width = w;
height = h;
dataBlock = d;
clickDrag = NULL;
// first, find a CUDA device and set it to graphic interop
cudaDeviceProp prop;
int dev;
memset( &prop, 0, sizeof( cudaDeviceProp ) );
prop.major = 1;
prop.minor = 0;
HANDLE_ERROR( cudaChooseDevice( &dev, &prop ) );
cudaGLSetGLDevice( dev );
// a bug in the Windows GLUT implementation prevents us from
// passing zero arguments to glutInit()
int c=1;
char* dummy = "";
glutInit( &c, &dummy );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA );
glutInitWindowSize( width, height );
glutCreateWindow( "bitmap" );
glBindBuffer = (PFNGLBINDBUFFERARBPROC)GET_PROC_ADDRESS("glBindBuffer");
glDeleteBuffers = (PFNGLDELETEBUFFERSARBPROC)GET_PROC_ADDRESS("glDeleteBuffers");
glGenBuffers = (PFNGLGENBUFFERSARBPROC)GET_PROC_ADDRESS("glGenBuffers");
glBufferData = (PFNGLBUFFERDATAARBPROC)GET_PROC_ADDRESS("glBufferData");
glGenBuffers( 1, &bufferObj );
glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, bufferObj );
glBufferData( GL_PIXEL_UNPACK_BUFFER_ARB, width * height * 4,
NULL, GL_DYNAMIC_DRAW_ARB );
HANDLE_ERROR( cudaGraphicsGLRegisterBuffer( &resource, bufferObj, cudaGraphicsMapFlagsNone ) );
}
~GPUAnimBitmap() {
free_resources();
}
void free_resources( void ) {
HANDLE_ERROR( cudaGraphicsUnregisterResource( resource ) );
glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, 0 );
glDeleteBuffers( 1, &bufferObj );
}
long image_size( void ) const { return width * height * 4; }
void click_drag( void (*f)(void*,int,int,int,int)) {
clickDrag = f;
}
void anim_and_exit( void (*f)(uchar4*,void*,int), void(*e)(void*) ) {
GPUAnimBitmap** bitmap = get_bitmap_ptr();
*bitmap = this;
fAnim = f;
animExit = e;
glutKeyboardFunc( Key );
glutDisplayFunc( Draw );
if (clickDrag != NULL)
glutMouseFunc( mouse_func );
glutIdleFunc( idle_func );
glutMainLoop();
}
// static method used for glut callbacks
static GPUAnimBitmap** get_bitmap_ptr( void ) {
static GPUAnimBitmap* gBitmap;
return &gBitmap;
}
// static method used for glut callbacks
static void mouse_func( int button, int state,
int mx, int my ) {
if (button == GLUT_LEFT_BUTTON) {
GPUAnimBitmap* bitmap = *(get_bitmap_ptr());
if (state == GLUT_DOWN) {
bitmap->dragStartX = mx;
bitmap->dragStartY = my;
} else if (state == GLUT_UP) {
bitmap->clickDrag( bitmap->dataBlock,
bitmap->dragStartX,
bitmap->dragStartY,
mx, my );
}
}
}
// static method used for glut callbacks
static void idle_func( void ) {
static int ticks = 1;
GPUAnimBitmap* bitmap = *(get_bitmap_ptr());
uchar4* devPtr;
size_t size;
HANDLE_ERROR( cudaGraphicsMapResources( 1, &(bitmap->resource), NULL ) );
HANDLE_ERROR( cudaGraphicsResourceGetMappedPointer( (void**)&devPtr, &size, bitmap->resource) );
bitmap->fAnim( devPtr, bitmap->dataBlock, ticks++ );
HANDLE_ERROR( cudaGraphicsUnmapResources( 1, &(bitmap->resource), NULL ) );
glutPostRedisplay();
}
// static method used for glut callbacks
static void Key(unsigned char key, int x, int y) {
switch (key) {
case 27:
GPUAnimBitmap* bitmap = *(get_bitmap_ptr());
if (bitmap->animExit)
bitmap->animExit( bitmap->dataBlock );
bitmap->free_resources();
exit(0);
}
}
// static method used for glut callbacks
static void Draw( void ) {
GPUAnimBitmap* bitmap = *(get_bitmap_ptr());
glClearColor( 0.0, 0.0, 0.0, 1.0 );
glClear( GL_COLOR_BUFFER_BIT );
glDrawPixels( bitmap->width, bitmap->height, GL_RGBA,
GL_UNSIGNED_BYTE, 0 );
glutSwapBuffers();
}
};
#endif // __GPU_ANIM_H__
Line that is making a first error is :
PFNGLBINDBUFFERARBPROC glBindBuffer = NULL;
VS says: IntelliSense: PCH warning: header stop cannot be in a macro or #if block. An intellisense PCH file was not generated.
thank you very much
to run it properly you have to first download the source codes from :`CUDA by Example source code
then extract it. when you create your project in visual studio, if it is 2010 and newer you should go to the project properties and go to VC++ Directories and add the extracted folder as an include path. also add the lib folder as library path and the bin as the executable path.
then you can add a new cu file to your project and copy the content of the chapter 4 folder -which ever file you want from that- and paste it there and compile it.
if still it doesn't compile, you should go to project properties -> linker and find the "Additional Dependency" and add cudart.lib and you are all set.
also pay attention that the include phrases in the books code are like this: /.../common/book.h , you should change them to common/book.h format.

OpenCV: Error in cvHoughCircles usage

I am using cvHoughCircles to find the two white ovals in the following image:
I first used thresholding for locating the white regions and then used Hough Transforms. But the output is not coming correct as shown below:
I am not able to understand what is happening? Why it is detecting 3 circles and why only one is being detected correctly? Any suggestions?
Below is my code:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include<conio.h>
#include<malloc.h>
using namespace cv;
using namespace std;
int main( ) {
IplImage* image = cvLoadImage(
"testing.bmp",
CV_LOAD_IMAGE_GRAYSCALE
);
IplImage* src = cvLoadImage("testing.bmp");
CvMemStorage* storage = cvCreateMemStorage(0);
cvThreshold( src, src, 200, 255, CV_THRESH_BINARY );
CvSeq* results = cvHoughCircles(
image,
storage,
CV_HOUGH_GRADIENT,
3,
image->width/10
);
for( int i = 0; i < results->total; i++ )
{
float* p = (float*) cvGetSeqElem( results, i );
CvPoint pt = cvPoint( cvRound( p[0] ), cvRound( p[1] ) );
cvCircle(
src,
pt,
cvRound( p[2] ),
CV_RGB(0xff,0,0)
);
}
cvNamedWindow( "HoughCircles", 1 );
cvShowImage( "HoughCircles", src);
cvWaitKey(0);
}
Edit:
Since I am not get satisfactory results with Hough Transform, I am willing to go for some other way. I can assume that each white blob in the figure is of equal size (size is known)and also the distance between the blob is known. Is there a non-trivial way I can find a vertical line (a tangent) touching the left side of left white blob? Once I know this tangent, I get an idea of the boundary location, then I will draw a circle at x=(this location + radius(which is known)), y= this location. Can I find such x and y coordinates using some non-trivial ways?
Solved, by changing as per below:
cvThreshold(image, image, 220, 255, CV_THRESH_BINARY );
cvCanny(image, image, 255, 255, 3);
cvNamedWindow( "edge", 1 );
cvShowImage( "edge", image);
cvWaitKey(0);
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* results = cvHoughCircles(
image,
storage,
CV_HOUGH_GRADIENT,
4,
image->width/4, 100,100,0,50);
Here is the output:
It's all about the parameters:
IplImage* src = cvLoadImage(argv[1]);
if (!src)
{
cout << "Failed: unable to load image " << argv[1] << endl;
return -1;
}
//IplImage* image = cvLoadImage(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
IplImage* image = cvCreateImage(cvSize(src->width, src->height), IPL_DEPTH_8U, 1);
cvCvtColor(src, image, CV_RGB2GRAY);
cvThreshold(image, image, 220, 255, CV_THRESH_BINARY );
// cvNamedWindow( "thres", 1 );
// cvShowImage( "thres", image);
// cvWaitKey(0);
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* results = cvHoughCircles(
image,
storage,
CV_HOUGH_GRADIENT,
4,
image->width/3);
std::cout << "> " << results->total << std::endl;
for( int i = 0; i < results->total; i++ )
{
float* p = (float*) cvGetSeqElem( results, i );
CvPoint pt = cvPoint( cvRound( p[0] ), cvRound( p[1] ) );
cvCircle(src,
pt,
cvRound( p[2] ),
CV_RGB(0xff,0,0));
}
cvNamedWindow( "HoughCircles", 1 );
cvShowImage( "HoughCircles", src);
cvWaitKey(0);
If you've done a little bit more experimentation you would have eventually found out that with different parameters you get different results:
You should use edge detected image as input, not the thresholded.
Secondly, Hough circles will not work for elipses, unless they are very close to circles. I recommend reading about Generalized Hough Transform and implementing it for ellipses.

Detecting a face and saving the detected face in OpenCV

I am trying to detect the face in the image and trying to save the detected face as an image in OpenCV.
Having some problems in the detectfaces function below.
#include "stdafx.h"
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
CvHaarClassifierCascade *cascade;
CvMemStorage *storage;
void detectFaces( IplImage *img );
int _tmain(int argc, _TCHAR* argv[])
{
//CvCapture *capture;
IplImage *img;//*out;
int key = 0;
char *filename = "C:/OpenCV2.1/data/haarcascades/haarcascade_frontalface_alt.xml";
cascade = ( CvHaarClassifierCascade* )cvLoad( filename, 0, 0, 0 );
storage = cvCreateMemStorage( 0 );
img = cvLoadImage("Yurico.png");
assert( cascade && storage && img );
cvNamedWindow( "video:", 1 );
//cvNamedWindow( "video1:", 1 );
//out = detectFaces( img );
detectFaces( img );
cvWaitKey( 0 );
//cvShowImage( "video", out );
cvDestroyWindow( "video:" );
//cvDestroyWindow( "video1:" );
cvReleaseImage( &img );
cvReleaseHaarClassifierCascade( &cascade );
cvReleaseMemStorage( &storage );
return 0;
}
void detectFaces( IplImage *img )
{
int i;
CvRect *r;
CvSeq *faces = cvHaarDetectObjects(
img,
cascade,
storage,
1.1,
3,
0 /*CV_HAAR_DO_CANNY_PRUNNING*/,
cvSize( 40, 40) );
for( i = 0 ; i < ( faces ? faces->total : 0 ) ; i++ ) {
CvRect *r = ( CvRect* )cvGetSeqElem( faces, i );
cvRectangle( img,
cvPoint( r->x, r->y ),
cvPoint( r->x + r->width, r->y + r->height ),
CV_RGB( 255, 0, 0 ), 1, 8, 0 );
}
//cvShowImage( "video:", img );
cvSetImageROI(img, CvRect *r);
IplImage *img2 = cvCreateImage(cvGetSize(img),
img->depth,
img->nChannels);
cvSaveImage("Lakshmen.jpg",img2);
}
Have a error saying this :
Error 1 error C2664: 'cvSetImageROI' : cannot convert parameter 2 from 'CvRect *' to 'CvRect' c:\users\hp\documents\visual studio 2010\projects\facedetect\facedetect\facedetect.cpp 67 1 facedetect
Want to save the region of interest into another image. Any corrections or improvements do tell me..
you need to pass a CvRect and not a CvRect*, so you do not need the pointer (*) before r.
and since it is already a cvRect you should just write:
cvSetImageROI(img, &r);
cvSetImageROI() takes a cvRect as the 2nd argument, and it uses it as input parameter to clip the image to that area.
In other words, you need to create a cvRect with valid info. You can do it before calling the function, or inline:
cvSetImageROI(img_corr, cvRect(x_pos, y_pos, width, height));
I also noticed that in your code, you create CvRect* r; on at least 3 diferent locations inside the same function. Bad practice! Tip: create variables in your code at the moment you are going to use them, not before that.
Just replace the CvRect *r as r=(CvRect*)cvGetSeqElem( faces,i) and after the for-loop write the two lines
cvSetImageROI(img, cvRect(r->x,r->y,r->width,r->height));
cvSaveImage("C1.jpg",img);

Resources