conversion from string^ to char * winforms c++ - winforms

i m reading path from textbox and then tried to open image
String^ P = path->Text;
IplImage* img = cvLoadImage(P);
It gives me the following error
Error 1 error C2664: 'cvLoadImage' : cannot convert parameter 1 from 'System::String ^' to 'const char *'
Can anyone tell me how to convert it to char *.

System::String ^ str = path->Text ;
char* str2 = (char*)Marshal::StringToHGlobalAnsi(str).ToPointer();
printf(str2);
Include the following header files
#using <mscorlib.dll>
using namespace System::Runtime::InteropServices;

It is Never TOO late.
here is code I am using
And Do not forget to include the header file in basic file #include "saveImg.h"
call this by
saveImg () ;
saveImg.h
int saveImg (){ // int argc , char* argv[] ) { // (int argc, char** argv) {
IplImage* img;
// if( argc == 2 && (img=cvLoadImage(argv[1], 1))!= 0) {
img=cvLoadImage("hello.jpg") ;
IplImage* gray = cvCreateImage( cvGetSize(img), 8, 1 );
IplImage* net1 = cvCreateImage( cvGetSize(img), 8, 3 );
CvMemStorage* storage = cvCreateMemStorage(0);
cvCvtColor( img, gray, CV_BGR2GRAY );
cvSmooth( gray, gray, CV_GAUSSIAN, 9, 9 ); // smooth it, otherwise a lot of false circles may be detected
CvSeq* circles = cvHoughCircles( gray, storage, CV_HOUGH_GRADIENT, 2, gray->height/4, 200, 100 );
int i;
for( i = 0; i < circles->total; i++ ) {
float* p = (float*)cvGetSeqElem( circles, i );
cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), 3, CV_RGB(0,255,0), -1, 8, 0 );
cvCircle( img, cvPoint(cvRound(p[1]),cvRound(p[2])), cvRound(p[3]), CV_RGB(50,0,100), 3, 8, 0 );
// cvWaitKey(0);
}
cvNamedWindow( "Original", 1 );
cvShowImage( "Original", img );
cvWaitKey(0);
cvNamedWindow( "circles", 1 );
cvShowImage( "circles", gray );
cvWaitKey(0);
return 0;
}

Char * to String ^ in Visual Studio C++ 10 and above
I tested this and OK while making a small Calculator....
// Variables :
char * number1 ;
double num1 ;
String ^ no1 ;
number1 = (char*)Marshal::StringToHGlobalAnsi(no1).ToPointer(); num1 = atof (number1) ;

Related

Using ROI from camera feed as Template for cvMatchTemplate

This is code that im rewriting that i wrote successfully before.
its suppose to use a a roi from a webcam and match it with cvMatchTemplate against other webcam frames...I took out the trackbars and windows to keep it short per guidelines but in the original you could move the trackbars to select a section of the frame in the top left window and in the bottom left window you saw your template
here is a picture of what it looked like:
http://i983.photobucket.com/albums/ae313/edmoney777/Screenshotfrom2013-10-21112021_zpsae11e3f0.png
Here is the error im getting
I tried changing the depth of src to 32F with no luck...read the templmatch.cpp
line 384 the error mssg gave me but no help there
OpenCV Error: Assertion failed (result.size() == cv::Size(std::abs
(img.cols - templ.cols) + 1, std::abs(img.rows - templ.rows) + 1)
&& result.type() == CV_32F) in cvMatchTemplat
Im new to opencv and could use a little help debugging the code below
#include <cv.h>
#include <highgui.h>
using namespace std;
int main(){
CvCapture* capture =0;
capture = cvCaptureFromCAM(0);
if(!capture){
printf("Capture failure\n");
return -1;
}
IplImage* frame=0;
double width=640.0;
double height=480.0;
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, width);
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, height);
while(true){
frame = cvQueryFrame(capture);
if(!frame) break;
frame=cvCloneImage(frame);
IplImage *src, *templ, *ftmp[6]; // ftmp will hold results
int i;
CvRect roi;
int rectx = 0;
int recty = 0;
int rectwidth = frame->width /10;
int rectheight = frame->height /10;
IplImage* img = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
// Read in the source image to be searched
src = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
roi=cvRect(rectx, recty, rectwidth, rectheight);
img = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
src = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
cvCopy(frame, img);
cvSetImageROI(frame, roi);
cvShowImage( "b", img );
cvReleaseImage(&img);
// Allocate Output Images:
int iwidth = src->width - frame->width + 1;
int iheight = src->height - frame->height + 1;
for(i = 0; i < 6; ++i){
ftmp[i]= cvCreateImage( cvSize( iwidth, iheight ), 32, 1 );
}
// Do the matching of the template with the image
for( i = 0; i < 6; ++i ){
cvMatchTemplate( src, frame, ftmp[i], i );
cvNormalize( ftmp[i], ftmp[i], 1, 0, CV_MINMAX );
}
// DISPLAY
cvReleaseImage(&src);
cvResetImageROI(frame);
cvReleaseImage(&frame);
//Wait 50mS
int c = cvWaitKey(10);
//If 'ESC' is pressed, break the loop
if((char)c==27 ) break;
}
cvDestroyAllWindows() ;
cvReleaseCapture(&capture);
return 0;
}
I am new to OpenCV and really don't know what to do with this error-message. Anyone an idea/pointer what to do? Your help is very appreciated! Cheers,
As you are performing template matching on the selected ROI of the image, you have to create the output images according to the size of ROI.
// Allocate Output Images:
int iwidth = src->width - rectwidth + 1;
int iheight = src->height - rectheight + 1;
Your code contains memory leaks which may crash the program e.g. all 6 images of ftmp are being allocated in each iteration but not being released anywhere. Either release the images at the end of iteration, or create them only once before while loop.
Also, OpenCV documentation explicitly states not to modify the frame returned by cvQueryFrame. So you may consider removing cvReleaseImage(&frame);. Check this answer for details.

C/CUDA - Help needed to write a program to store images in a buffer

I am a new to CUDA programming and I need help in writing a program to store images in a memory buffer. I tried modifying the code in the CUDA-OpenGL interop example, given in the CUDA-By Example book, to store 2 images one after another in a buffer. How should I write the program if I tried to avoid infinite loops but I am not sure if I succeeded? Any help in writing a correct program would be very much appreciated!
#include "book.h"
#include "cpu_bitmap.h"
#include "cuda.h"
#include <cuda_gl_interop.h>
PFNGLBINDBUFFERARBPROC glBindBuffer = NULL;
PFNGLDELETEBUFFERSARBPROC glDeleteBuffers = NULL;
PFNGLGENBUFFERSARBPROC glGenBuffers = NULL;
PFNGLBUFFERDATAARBPROC glBufferData = NULL;
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, char *file, int line, bool abort=true)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) system ("pause");
}
}
#define DIM 512
#define IMAGESIZE_MAX (DIM*DIM)
GLuint bufferObj;
cudaGraphicsResource *resource;
// based on ripple code, but uses uchar4 which is the type of data
// graphic inter op uses. see screenshot - basic2.png
__global__ void kernel( uchar4 *ptr1)
{
// map from threadIdx/BlockIdx to pixel position
int x = threadIdx.x + blockIdx.x * blockDim.x;
int y = threadIdx.y + blockIdx.y * blockDim.y;
int offset = x + y * blockDim.x * gridDim.x ;
// now calculate the value at that position
float fx = x/(float)DIM - 0.5f;
float fy = y/(float)DIM - 0.5f;
unsigned char green = 128 + 127 * tan( abs(fx*100) - abs(fy*100) );
// accessing uchar4 vs unsigned char*
ptr1[offset].x = 0;
ptr1[offset].y = green;
ptr1[offset].z = 0;
ptr1[offset].w = 255;
}
__global__ void kernel2( uchar4 *ptr2)
{
// map from threadIdx/BlockIdx to pixel position
int x = threadIdx.x + blockIdx.x * blockDim.x;
int y = threadIdx.y + blockIdx.y * blockDim.y;
int offset = x + y * blockDim.x * gridDim.x ;
// now calculate the value at that position
float fx = x/(float)DIM - 0.5f;
float fy = y/(float)DIM - 0.5f;
unsigned char green = 128 + 127 * tan( abs(fx*100) - abs(fy*100) );
unsigned char orange = 1000;
// accessing uchar4 vs unsigned char*
ptr2[offset].x = orange;
ptr2[offset].y = green;
ptr2[offset].z = 0;
ptr2[offset].w = 255;
}
__global__ void copy ( uchar4 *pBuffer, uchar4 *Ptr )
{
int x = threadIdx.x + blockIdx.x * blockDim.x;
int y = threadIdx.y + blockIdx.y * blockDim.y;
int idx = x + y * blockDim.x * gridDim.x ;
while ( idx != DIM*DIM)
{
pBuffer[idx] = Ptr[idx] ;
__syncthreads();
}
}
__global__ void copy2 ( uchar4 *pBuffer, uchar4 *Ptr2 )
{
int x = threadIdx.x + blockIdx.x * blockDim.x;
int y = threadIdx.y + blockIdx.y * blockDim.y;
int idx = x + y * blockDim.x * gridDim.x ;
int bdx = idx;
while ( (idx < DIM*DIM) && (bdx < DIM*DIM) )
{
uchar4 temp = Ptr2[bdx];
__syncthreads();
pBuffer[idx+4] = temp;
__syncthreads();
if ((idx==DIM*DIM) && (bdx==DIM*DIM))
{
break;
}
}
}
void key_func( unsigned char key, int x, int y ) {
switch (key) {
case 27:
// clean up OpenGL and CUDA
( cudaGraphicsUnregisterResource( resource ) );
glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, 0 );
glDeleteBuffers( 1, &bufferObj );
exit(0);
}
}
void draw_func( void ) {
// we pass zero as the last parameter, because out bufferObj is now
// the source, and the field switches from being a pointer to a
// bitmap to now mean an offset into a bitmap object
glDrawPixels( DIM, DIM, GL_RGBA, GL_UNSIGNED_BYTE, 0 );
glutSwapBuffers();
}
int main( int argc, char **argv ) {
cudaDeviceProp prop;
int dev;
(memset( &prop, 0, sizeof( cudaDeviceProp ) ));
prop.major = 1;
prop.minor = 0;
HANDLE_ERROR( cudaChooseDevice( &dev, &prop ) );
// tell CUDA which dev we will be using for graphic interop
// from the programming guide: Interoperability with OpenGL
// requires that the CUDA device be specified by
// cudaGLSetGLDevice() before any other runtime calls.
HANDLE_ERROR( cudaGLSetGLDevice( dev ) );
// these GLUT calls need to be made before the other OpenGL
// calls, else we get a seg fault
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA );
glutInitWindowSize( DIM, DIM );
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");
// the first three are standard OpenGL, the 4th is the CUDA reg
// of the bitmap these calls exist starting in OpenGL 1.5
glGenBuffers( 1, &bufferObj );
glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, bufferObj );
glBufferData( GL_PIXEL_UNPACK_BUFFER_ARB, DIM * DIM * 4 ,
NULL, GL_DYNAMIC_DRAW_ARB );
// REGISTER THE GL BufferObj and CUDA Resource
HANDLE_ERROR(( cudaGraphicsGLRegisterBuffer( &resource,
bufferObj,
cudaGraphicsMapFlagsNone ) ));
// do work with the memory dst being on the GPU, gotten via mapping
HANDLE_ERROR( cudaGraphicsMapResources( 1, &resource, NULL ) );
uchar4* devPtr;
size_t size = DIM*DIM;
size_t sizet = 2*DIM*DIM;
gpuErrchk(cudaMalloc ( (uchar4 **)&devPtr, size));
uchar4 *devPtr2;
gpuErrchk(cudaMalloc ( (uchar4 **)&devPtr2, size));
uchar4 *pBuffer;
gpuErrchk(cudaMalloc ( (uchar4 **)&pBuffer, size));
uchar4 *pBufferCurrent;
gpuErrchk(cudaMalloc ( (uchar4 **)&pBuffer, size));
uchar4 *pBufferImage;
gpuErrchk(cudaMalloc ( (uchar4 **)&pBufferImage, sizet));
// REGISTER THE C BUFFER and CUDA Resource
HANDLE_ERROR( cudaGraphicsResourceGetMappedPointer( (void**)&pBufferImage,
&size,
resource) );
dim3 grids(DIM/16,DIM/16);
dim3 threads(16,16);
kernel<<<grids,threads>>>( devPtr );
gpuErrchk( cudaPeekAtLastError() );
gpuErrchk( cudaDeviceSynchronize() );
kernel2<<<grids,threads>>>(devPtr2);
gpuErrchk( cudaPeekAtLastError() );
gpuErrchk( cudaDeviceSynchronize() );
int a = 1;
do
{
if (a==1)
{
copy<<< 512, 512>>>(pBufferImage, devPtr);
gpuErrchk( cudaPeekAtLastError() );
gpuErrchk( cudaDeviceSynchronize() );
}
if(a==2)
{
copy2<<< 512, 512>>>(pBufferImage, devPtr2);
gpuErrchk( cudaPeekAtLastError() );
gpuErrchk( cudaDeviceSynchronize() );
}
a++;
} while (a<=2);
HANDLE_ERROR ( cudaGraphicsUnmapResources( 1, &resource, NULL ) );
// set up GLUT and kick off main loop
glutKeyboardFunc( key_func );
glutDisplayFunc( draw_func );
glutMainLoop();
}
Here's some code I wrote that is a modification of the CUDA by examples code contained here which I believe is effectively what you started with. I used two kernels, just as you have, to generate either a green or an orange image. It will initially start with the green image displayed, but you can toggle between green and orange images using the space bar. ESC key will exit the app.
#include "book.h"
#include "cpu_bitmap.h"
//#include "cuda.h"
#include <cuda_gl_interop.h>
int which_image;
PFNGLBINDBUFFERARBPROC glBindBuffer = NULL;
PFNGLDELETEBUFFERSARBPROC glDeleteBuffers = NULL;
PFNGLGENBUFFERSARBPROC glGenBuffers = NULL;
PFNGLBUFFERDATAARBPROC glBufferData = NULL;
#define DIM 512
GLuint bufferObj;
cudaGraphicsResource *resource;
dim3 mgrids(DIM/16,DIM/16);
dim3 mthreads(16,16);
// based on ripple code, but uses uchar4 which is the type of data
// graphic inter op uses. see screenshot - basic2.png
__global__ void kernel_gr( uchar4 *ptr ) {
// map from threadIdx/BlockIdx to pixel position
int x = threadIdx.x + blockIdx.x * blockDim.x;
int y = threadIdx.y + blockIdx.y * blockDim.y;
int offset = x + y * blockDim.x * gridDim.x;
// now calculate the value at that position
float fx = x/(float)DIM - 0.5f;
float fy = y/(float)DIM - 0.5f;
unsigned char green = 128 + 127 *
sin( abs(fx*100) - abs(fy*100) );
// accessing uchar4 vs unsigned char*
ptr[offset].x = 0;
ptr[offset].y = green;
ptr[offset].z = 0;
ptr[offset].w = 255;
}
__global__ void kernel_or( uchar4 *ptr ) {
// map from threadIdx/BlockIdx to pixel position
int x = threadIdx.x + blockIdx.x * blockDim.x;
int y = threadIdx.y + blockIdx.y * blockDim.y;
int offset = x + y * blockDim.x * gridDim.x;
// now calculate the value at that position
float fx = x/(float)DIM - 0.5f;
float fy = y/(float)DIM - 0.5f;
unsigned char orange = 128 + 127 *
sin( abs(fx*100) - abs(fy*100) );
// accessing uchar4 vs unsigned char*
ptr[offset].x = orange;
ptr[offset].y = orange/2;
ptr[offset].z = 0;
ptr[offset].w = 255;
}
static void draw_func( void ) {
// we pass zero as the last parameter, because out bufferObj is now
// the source, and the field switches from being a pointer to a
// bitmap to now mean an offset into a bitmap object
glDrawPixels( DIM, DIM, GL_RGBA, GL_UNSIGNED_BYTE, 0 );
glutSwapBuffers();
}
static void key_func( unsigned char key, int x, int y ) {
switch (key) {
case 32:
// do work with the memory dst being on the GPU, gotten via mapping
HANDLE_ERROR( cudaGraphicsMapResources( 1, &resource, NULL ) );
uchar4* devPtr;
size_t size;
HANDLE_ERROR(
cudaGraphicsResourceGetMappedPointer( (void**)&devPtr,
&size,
resource) );
if (which_image == 1){
kernel_or<<<mgrids,mthreads>>>( devPtr );
HANDLE_ERROR(cudaPeekAtLastError());
HANDLE_ERROR(cudaDeviceSynchronize());
printf("orange\n");
which_image = 2;
}
else {
kernel_gr<<<mgrids,mthreads>>>( devPtr );
HANDLE_ERROR(cudaPeekAtLastError());
HANDLE_ERROR(cudaDeviceSynchronize());
printf("green\n");
which_image = 1;
}
HANDLE_ERROR( cudaGraphicsUnmapResources( 1, &resource, NULL ) );
draw_func();
break;
case 27:
// clean up OpenGL and CUDA
HANDLE_ERROR( cudaGraphicsUnregisterResource( resource ) );
glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, 0 );
glDeleteBuffers( 1, &bufferObj );
exit(0);
}
}
int main( int argc, char **argv ) {
cudaDeviceProp prop;
int dev;
memset( &prop, 0, sizeof( cudaDeviceProp ) );
prop.major = 1;
prop.minor = 0;
HANDLE_ERROR( cudaChooseDevice( &dev, &prop ) );
// tell CUDA which dev we will be using for graphic interop
// from the programming guide: Interoperability with OpenGL
// requires that the CUDA device be specified by
// cudaGLSetGLDevice() before any other runtime calls.
HANDLE_ERROR( cudaGLSetGLDevice( dev ) );
// these GLUT calls need to be made before the other OpenGL
// calls, else we get a seg fault
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA );
glutInitWindowSize( DIM, DIM );
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");
// the first three are standard OpenGL, the 4th is the CUDA reg
// of the bitmap these calls exist starting in OpenGL 1.5
glGenBuffers( 1, &bufferObj );
glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, bufferObj );
glBufferData( GL_PIXEL_UNPACK_BUFFER_ARB, DIM * DIM * 4,
NULL, GL_DYNAMIC_DRAW_ARB );
HANDLE_ERROR(
cudaGraphicsGLRegisterBuffer( &resource,
bufferObj,
cudaGraphicsMapFlagsNone ) );
// do work with the memory dst being on the GPU, gotten via mapping
HANDLE_ERROR( cudaGraphicsMapResources( 1, &resource, NULL ) );
uchar4* devPtr;
size_t size;
HANDLE_ERROR(
cudaGraphicsResourceGetMappedPointer( (void**)&devPtr,
&size,
resource) );
dim3 grids(DIM/16,DIM/16);
dim3 threads(16,16);
kernel_gr<<<grids,threads>>>( devPtr );
HANDLE_ERROR( cudaGraphicsUnmapResources( 1, &resource, NULL ) );
which_image = 1;
// set up GLUT and kick off main loop
glutKeyboardFunc( key_func );
glutDisplayFunc( draw_func );
glutMainLoop();
}
Not sure if it will be useful, I'm still not understanding what you want to accomplish entirely. I don't really know what this means:
I just want to store both those images in a buffer and then render the buffer containing those two images in OpenGL.
You want to be able to see one image at a time, and switch images? Or you want to be able to see both images at the same time? If the latter, please explain. Do you want one at the top of the window and one at the bottom of the window? Both of them blended together?
EDIT: It seems to me that you may be wanting some sort of 3D visualization of multiple images, since the question and answer with you about what you want hasn't been productive (at least I still can't get a handle on what you want to see VISUALLY, ignoring what goes on under the hood.) You haven't tagged this question with OpenGL, so no OpenGL experts are looking at it. Furthermore, you've made statements like: "I will use OpenGL functions to rotate and translate the buffer. " If what you're trying to do is create a 3D visualization of a set of images that a user can interact with, this is not the sample code you want to start with. This is a basic 2D image display code. Trying to expand the buffer to hold multiple images is the least of your difficulties in creating some sort of 3D visualization in OpenGL. And you will not get to some kind of 3D multi-image display using this sample code.
I suspect that the CUDA-OpenGL interop portion of what you're trying to do is not difficult. I've shown with the example program how you can get 2 different images, generated by 2 different kernels, displayed under user control. So the problem of how to take an image from CUDA and display it, or get it into a buffer that can be displayed, I think is pretty well illustrated.
My suggestion is this: Leave the CUDA-OpenGL interop portion aside. Write an OpenGL program that does what you want, with arbitrary images (generate them however you like, no need to use CUDA.) If you need help with that, pose questions on SO, and tag them with OpenGL so that people who will know how to do it can help you. Then, when you have a prototype of what you want to display visually, you can inject the CUDA portion. And I suspect that part will be pretty simple at that point.

The intruder detector - OpenCv - C

I'm doing "photo" processing application using C with OpenCV.
My computer will constantly survey its surroundings using its
webcam. When it detects movement, it will write the current
webcam image to a file (notify).
This is how I have written the code , but it is very slow, I'm not good in doing the notify....could you give me please some good advice concerning the code?
#include <stdio.h>
#include "cv.h"
#include "highgui.h"
#include <dos.h>
#include <conio.h>
CvCapture *capture;
int key;
IplImage *oldFrame;
IplImage *currentFrame;
IplImage *res;
int f=0,x,y,idx,idy;
int currentFrame_width, currentFrame_height;
int oldFrame_width, oldFrame_height;
int res_width, res_height;
int main( int argc, char** argv )
{
while( key != 'q' )
{
capture = cvCaptureFromCAM( 0 );
if( !capture ) return 1;
key = cvWaitKey( 1 );
cvNamedWindow( "FrameController", CV_WINDOW_AUTOSIZE );
if(f==0){
oldFrame = cvQueryFrame( capture );
cvShowImage( "FrameController", oldFrame );
f=1;
}
else{
CvMemStorage* storage = cvCreateMemStorage(0);
currentFrame = cvQueryFrame( capture );
for ( x=0;x<currentFrame->width;x++)
{
for ( y=0;y<currentFrame->height;y++)
{
idx = y*currentFrame->widthStep + currentFrame->nChannels*x;
idy = y*oldFrame->widthStep + oldFrame->nChannels*x;
}
}
uchar R= (uchar) currentFrame->imageData [idx+0];
uchar G = (uchar) currentFrame->imageData [idx+1];
uchar B = (uchar) currentFrame->imageData [idx+2];
uchar R1= (uchar) oldFrame->imageData [idy+0];
uchar G1 = (uchar) oldFrame->imageData [idy+1];
uchar B1 = (uchar) oldFrame->imageData [idy+2];
if(!((R==R1) || (G==G1) || (B==B1))){
f=0;
CvHaarClassifierCascade* cascade = (CvHaarClassifierCascade*)cvLoad( "C:\\OpenCV2.1\\data\\haarcascades\\haarcascade_frontalface_alt2.xml" );
double scale = 1.3;
static CvScalar colors[] = {CV_RGB(rand()&255, rand()&255, rand()&255 )};
cvClearMemStorage( storage );
CvSeq* objects = cvHaarDetectObjects( currentFrame, cascade, storage, 1.1, 4, 0, cvSize( 40, 50 ));
CvRect* r;
for( int i = 0; i < (objects ? objects->total : 0 ); i++ ){
r = ( CvRect* )cvGetSeqElem( objects, i );
cvRectangle( currentFrame, cvPoint( r->x, r->y ), cvPoint( r->x + r->width, r->y + r->height ),
colors[i%8]);
cvShowImage( "FrameController", currentFrame );
}
}
}
}
cvDestroyWindow( "FrameController" );
cvReleaseCapture( &capture );
cvReleaseImage( &currentFrame);
cvReleaseImage( &oldFrame);
return 0;
}
Seems like you're doing unnecessary initializations inside your while loop. For example
capture = cvCaptureFromCAM( 0 ); << is that necessary to be in the loop?
cvNamedWindow( "FrameController", CV_WINDOW_AUTOSIZE ); << this?
for ( x=0;x<currentFrame->width;x++)
{
for ( y=0;y<currentFrame->height;y++)
{
idx = y*currentFrame->widthStep + currentFrame->nChannels*x;
idy = y*oldFrame->widthStep + oldFrame->nChannels*x;
}
} << that's a loop over the WHOLE IMAGE that does NOTHING.
What is the storage used for?
Hope that helped you clean up your code a bit.

OpenCV Eye Tracking looses out for some frames

I am pasting code below to track both eyes and overlay an image when both eyes are tracked.
I am using haar xml files to track both eyes of faces and overlaying an image over it.
My problem is I get result like this.
Img 1
Img 2
Img 3
Img 4
Img 5
Img 6
Img 7
Img 8
Img 9
Img 10
I am posting my code below
#include "cv.h"
#include "highgui.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <stdio.h>
#include <sys/stat.h>
#include <ctype.h>
#include <string>
using namespace cv;
using namespace std;
void detectAndDisplay( Mat frame );
String face_cascade_name = "/root/opencv/newtutorial/haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "/root/opencv/newtutorial/haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
RNG rng(12345);
IplImage *disp,*neg_img,*cpy_img,*imga;
IplImage *pic;
IplImage *image_n = 0;
int make=0;
CvMat* warp_matrix = cvCreateMat(3,3,CV_32FC1);
namespace {
void makevdo(){
bool flag = false;
fstream fin;
char filename_new[200],filename_new_after[200];
int n=0;
Mat frame;
double frameRate = 25.0;
CvVideoWriter *vdowriter = cvCreateVideoWriter( "/root/opencv/newtutorial/test_converted_next.mov", CV_FOURCC('j','p','e','g'), frameRate, Size(640,480) );
VideoWriter(outputFile,CV_FOURCC('j','p','e','g'),frameRate,Size(640,480));
while(flag==false){
sprintf(filename_new,"/root/opencv/newtutorial/mydirnext/filename%.3d.jpg",n);
sprintf(filename_new_after,"/root/opencv/newtutorial/framesaftertrack/filename%.3d.jpg",n);
fin.open(filename_new,ios::in);
if( fin.is_open() )
{
frame = imread(filename_new);
pic = cvLoadImage("/root/opencv/newtutorial/pic.png");
image_n = cvLoadImage(filename_new,1);
disp = cvCreateImage( cvGetSize(image_n), 8, 3 );
cpy_img = cvCreateImage( cvGetSize(image_n), 8, 3 );
neg_img = cvCreateImage( cvGetSize(image_n), 8, 3 );
std::vector<Rect> faces;
Mat frame_gray;
cvtColor( frame, frame_gray, CV_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );
face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
CvPoint2D32f q[4];
q[0].x= (float) pic->width * 0;
q[0].y= (float) pic->height * 0;
q[1].x= (float) pic->width;
q[1].y= (float) pic->height * 0;
q[2].x= (float) pic->width;
q[2].y= (float) pic->height;
q[3].x= (float) pic->width * 0;
q[3].y= (float) pic->height;
for( int i = 0; i < (int)faces.size(); i++ )
{
faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 2, 8, 0 );
Mat faceROI = frame_gray( faces[i] );
std::vector<Rect> eyes;
eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );
if((int)eyes.size()==2 && (((eyes[0].x + eyes[0].width*0.5 ) < eyes[1].x) || ((eyes[1].x + eyes[1].width*0.5 ) < eyes[0].x))){
CvPoint2D32f p[4];
IplImage* blank = cvCreateImage( cvGetSize(pic), 8, 3);
cvZero(blank);
cvNot(blank,blank);
if(eyes[0].x<eyes[1].x){
Point center0( faces[i].x + eyes[0].x + eyes[0].width*0.5, faces[i].y + eyes[0].y + eyes[0].height*0.5 );
float radius0 = (eyes[0].width + eyes[0].height)*0.25 ;
Point center1( faces[i].x + eyes[1].x + eyes[1].width*0.5, faces[i].y + eyes[1].y + eyes[1].height*0.5 );
float radius1 = (eyes[1].width + eyes[1].height)*0.25 ;
p[2].x= (float) center0.x - radius0;
p[2].y= (float) center0.y + radius0;
p[1].x= (float) center0.x - radius0;
p[1].y= (float) center0.y - radius0;
p[3].x= (float) center1.x + radius1;
p[3].y= (float) center1.y + radius1;
p[0].x= (float) center1.x + radius1;
p[0].y= (float) center1.y - radius1;
}
else{
Point center0( faces[i].x + eyes[1].x + eyes[1].width*0.5, faces[i].y + eyes[1].y + eyes[1].height*0.5 );
float radius0 = (eyes[1].width + eyes[1].height)*0.25 ;
Point center1( faces[i].x + eyes[0].x + eyes[0].width*0.5, faces[i].y + eyes[0].y + eyes[0].height*0.5 );
float radius1 = (eyes[0].width + eyes[0].height)*0.25 ;
p[2].x= (float) center0.x - radius0;
p[2].y= (float) center0.y + radius0;
p[1].x= (float) center0.x - radius0;
p[1].y= (float) center0.y - radius0;
p[3].x= (float) center1.x + radius1;
p[3].y= (float) center1.y + radius1;
p[0].x= (float) center1.x + radius1;
p[0].y= (float) center1.y - radius1;
}
cvGetPerspectiveTransform(q,p,warp_matrix);
cvZero(neg_img);
cvZero(cpy_img);
cvWarpPerspective( pic, neg_img, warp_matrix);
cvWarpPerspective( blank, cpy_img, warp_matrix);
cvNot(cpy_img,cpy_img);
cvAnd(cpy_img,image_n,cpy_img);
cvOr(cpy_img,neg_img,image_n);
}
}
cvSaveImage(filename_new_after,image_n);
cvWriteFrame(vdowriter,image_n);
cout<<"Read file filename"<< n <<endl;
}
else{
flag=true;
}
fin.close();
n++;
}
cvReleaseVideoWriter(&vdowriter);
cvReleaseImage(&pic);
cvReleaseImage(&imga);
cvReleaseImage(&disp);
cvReleaseImage(&neg_img);
cvReleaseImage(&cpy_img);
cvReleaseImage(&image_n);
}
int process(VideoCapture& capture) {
char strFrame[]="/root/opencv/newtutorial/mydirnext";
if(mkdir(strFrame,0777)==-1)
{
cout<<"Error Trying to delete"<<endl;
if(system("rm -r /root/opencv/newtutorial/mydirnext")){
cout << "Directory successfully deleted"<<endl;
}
if(mkdir(strFrame,0777)==-1){
cout << "Error Again creating directory" << endl;
}
}
int n = 0;
char filename[200];
string window_name = "video | q or esc to quit";
cout << "press space to save a picture. q or esc to quit" << endl;
namedWindow(window_name, CV_WINDOW_KEEPRATIO);
Mat frame;
for (;;) {
capture >> frame;
if (frame.empty())
continue;
imshow(window_name, frame);
sprintf(filename,"/root/opencv/newtutorial/mydirnext/filename%.3d.jpg",n++);
imwrite(filename,frame);
char key = (char)waitKey(5);
switch (key) {
case 27:
if(make==0){
make=1;
makevdo();
}
return 0;
default:
break;
}
}
return 0;
}
}
int main(int ac, char** av) {
if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };
if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };
if (ac != 2) {
return 1;
}
std::string arg = av[1];
VideoCapture capture(arg);
if (!capture.isOpened())
capture.open(atoi(arg.c_str()));
if (!capture.isOpened()) {
cerr << "Failed to open a video device or video file!\n" << endl;
return 1;
}
return process(capture);
}
Sorry for posting so many Images SO, but No other way to explain my problem.
Start by extracting everything to methods/functions that you can be given a sensible name. We don't want to see methods longer than 10 lines. The images are not helpful at all. Remove the overlay and show what the algorithm claims to recognize, when it does so.
After you've done that, feed it sequences of the same images. First the ones where it recognizes, then those where it doesn't. That way you can get a better guess where the problem is. But first clean up the messy code.

Tracking objects using histogram data in OpenCV

I am trying to track objects inside an image using histogram data from the object. I pass in a reference image to get the histogram data and store it in a Mat. From there I load in an image and try and use the histogram data to detect the object. The problem I am coming with is not only is it not tracking the object, but it's not updating the detection. If I load image "1.jpg" the detection will claim that the object is in the top right corner when it's in the bottom left. When I pass in the second image the detection field does not move at all. This continues for the next batch of images as well. Below is a code snippet of my application.
This is being done in a Windows 7 32-bit environment using OpenCV2.3 in VS2010. Thanks in advance for any help
int main( int argc, char** argv )
{
vector<string> szFileNames;
IplImage* Image;
Mat img, hist, backproj;
Rect trackWindow;
// Load histogram data
hist = ImageHistogram("C:/Users/seb/Documents/redbox1.jpg", backproj);
Image = cvLoadImage("C:/Users/seb/Documents/1.jpg");
img = Mat(Image);
trackWindow = Rect(0, 0, Image->width, Image->height);
imshow("Histogram", hist);
while(true)
{
Detection(img, backproj, trackWindow);
imshow("Image", img);
char c = cvWaitKey(1);
switch(c)
{
case 32:
{
cvReleaseImage(&Image);
Image = cvLoadImage("C:/Users/seb/Documents/redbox2.jpg");
img = Mat(Image);
break;
}
}
}
cvReleaseImage(&Image);
// Destroy all windows
cvDestroyWindow("Histogram");
cvDestroyWindow("Image");
return 0;
}
Mat ImageHistogram(string szFilename, Mat& backproj)
{
// Create histogram values
int vmin = 10;
int vmax = 256;
int smin = 30;
int hsize = 16;
float hranges[] = {0,180};
const float* phranges = hranges;
// Load the image
IplImage* Image = cvLoadImage(szFilename.c_str());
Rect rect = Rect(0, 0, Image->width, Image->height);
// Convert Image to a matrix
Mat ImageMat = Mat(Image);
// Create and initialize the Histogram
Mat hsv, mask, hue, hist, histimg = Mat::zeros(200, 320, CV_8UC3);
cvtColor(ImageMat, hsv, CV_BGR2HSV);
// Create and adjust the histogram values
inRange(hsv, Scalar(0, smin, vmin), Scalar(180, 256, vmax), mask);
int ch[] = {0, 0};
hue.create(hsv.size(), hsv.depth());
mixChannels(&hsv, 1, &hue, 1, ch, 1);
Mat roi(hue, rect), maskroi(mask, rect);
calcHist(&roi, 1, 0, maskroi, hist, 1, &hsize, &phranges);
normalize(hist, hist, 0, 255, CV_MINMAX);
histimg = Scalar::all(0);
int binW = histimg.cols / hsize;
Mat buf(1, hsize, CV_8UC3);
for( int i = 0; i < hsize; i++ )
buf.at<Vec3b>(i) = Vec3b(saturate_cast<uchar>(i*180./hsize), 255, 255);
cvtColor(buf, buf, CV_HSV2BGR);
for( int i = 0; i < hsize; i++ )
{
int val = saturate_cast<int>(hist.at<float>(i)*histimg.rows/255);
rectangle( histimg, Point(i*binW,histimg.rows),
Point((i+1)*binW,histimg.rows - val),
Scalar(buf.at<Vec3b>(i)), -1, 8 );
}
calcBackProject(&hue, 1, 0, hist, backproj, &phranges);
backproj &= mask;
cvReleaseImage(&Image);
return histimg;
}
void Detection(Mat& image, Mat& backproj, Rect& trackWindow)
{
RotatedRect trackBox = CamShift(backproj, trackWindow, TermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 ));
int test2 = trackWindow.area();
if(trackBox.size.height > 0 && trackBox.size.width > 0)
{
if( trackWindow.area() <= 1 )
{
int cols = backproj.cols, rows = backproj.rows, r = (MIN(cols, rows) + 5)/6;
trackWindow = Rect(trackWindow.x - r, trackWindow.y - r,
trackWindow.x + r, trackWindow.y + r) &
Rect(0, 0, cols, rows);
}
int test = trackBox.size.area();
if(test >= 1)
{
rectangle(image, trackBox.boundingRect(), Scalar(255,0,0), 3, CV_AA);
ellipse( image, trackBox, Scalar(0,0,255), 3, CV_AA );
}
}
}
I've figured out the issue. It had to deal with me not converting the image that I'm checking upon. I had to get histogram data from my colored box and then I had to get the histogram from the image I was using to search.

Resources