subtraction frames opencv - opencv

hey i tried to do subtraction between current frame to previous, (the code attached ) the code running but i get errors and gray window without result the errors i got on command prompt:
Compiler did not align stack variables. Libavcodec has been miscompiled and may be very slow or crash. This is not a bug in libavcodec, but in the compiler. You may try recompiling using gcc >= 4.2. Do not report crashes to FFmpeg developers. OpenCV Error: Assertion failed (src1.size() == dst.size() && src1.type() == dst. type()) in unknown function, file ........\ocv\opencv\src\cxcore\cxarithm.cpp , line 1563.
someone have an idea? please your help!! thank you
int main()
{
int key = 0;
CvCapture* capture = cvCaptureFromAVI( "macroblock.mpg" );
IplImage* frame = cvQueryFrame( capture );
IplImage* currframe = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
IplImage* destframe = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
if ( !capture )
{
fprintf( stderr, "Cannot open AVI!\n" );
return 1;
}
int fps = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FPS );
cvNamedWindow( "dest", CV_WINDOW_AUTOSIZE );
while( key != 'x' )
{
frame = cvQueryFrame( capture );
currframe = cvCloneImage( frame );// copy frame to current
frame = cvQueryFrame( capture );// grab frame
cvSub(frame,currframe,destframe);// subtraction between the last frame to cur
if(key==27 )break;
cvShowImage( "dest",destframe);
key = cvWaitKey( 1000 / fps );
}
cvDestroyWindow( "dest" );
cvReleaseCapture( &capture );
return 0;
}

The problem is here
IplImage* currframe = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
IplImage* destframe = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
What you are doing is that you are reading off an mpeg that has 3 channels per frame. Now when you do the subtraction, you subtract a 3 channel frame from a 1 channel frame. This WILL cause problems. Try setting the number of channels to 3. And see if it works
IplImage* currframe = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,3);
IplImage* destframe = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,3);
To be sure, check the number of channels for the queried image, the cloned image. And since you are pushing the final image into a destination image of 1 channel. There you are corrupting the data. If no exception is thrown/caught at any place.
OpenCV Error: Assertion failed (src1.size() == dst.size() && src1.type() == dst. type())
Assertion failed seems to be a clear indicator of what I have explained.

Related

Unhandled exception at 0x52f9e470 in project1.exe : 0xC000001D : Illegal instruction

i am trying to detect an object using opencv in c++ but i am getting an error :
Unhandled exception at 0x52f9e470 in project1.exe : 0xC000001D : Illegal instruction.
using windows 7 32 bit,opencv 2.4.3,visual studio (c++) 2010 and my code is :
#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>
// Create a string that contains the exact cascade name
// Contains the trained classifer for detecting hand
const char *cascade_name="D:/31dec12/hand.xml";
//The function detects the hand from input frame and draws a rectangle around the detected portion of the frame
void detect_and_draw( IplImage* img )
{
// Create memory for calculations
static CvMemStorage* storage = 0;
// Create a new Haar classifier
static CvHaarClassifierCascade* cascade = 0;
// Sets the scale with which the rectangle is drawn with
int scale = 1;
// Create two points to represent the hand locations
CvPoint pt1, pt2;
// Looping variable
int i;
// Load the HaarClassifierCascade
cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
// Check whether the cascade has loaded successfully. Else report and error and quit
if( !cascade )
{
fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
return;
}
// Allocate the memory storage
storage = cvCreateMemStorage(0);
// Create a new named window with title: result
cvNamedWindow( "result", 1 );
// Clear the memory storage which was used before
cvClearMemStorage( storage );
// Find whether the cascade is loaded, to find the hands. If yes, then:
if( cascade )
{
// There can be more than one hand in an image. So create a growable sequence of hands.
// Detect the objects and store them in the sequence
CvSeq* hands = cvHaarDetectObjects( img, cascade, storage,
1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
cvSize(40, 40) );
// Loop the number of hands found.
for( i = 0; i < (hands ? hands->total : 0); i++ )
{
// Create a new rectangle for drawing the hand
CvRect* r = (CvRect*)cvGetSeqElem( hands, i );
// Find the dimensions of the hand,and scale it if necessary
pt1.x = r->x*scale;
pt2.x = (r->x+r->width)*scale;
pt1.y = r->y*scale;
pt2.y = (r->y+r->height)*scale;
// Draw the rectangle in the input image
cvRectangle( img, pt1, pt2, CV_RGB(230,20,232), 3, 8, 0 );
}
}
// Show the image in the window named "result"
cvShowImage( "result", img );
}
// A Simple Camera Capture Framework
int main()
{
// Gets the input video stream from camera
CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
// Checks if the input stream is obtained
if( !capture )
{
fprintf( stderr, "ERROR: capture is NULL \n" );
getchar();
return -1;
}
// Show the image captured from the camera in the window and repeat
while( 1 )
{
// Get one frame
IplImage* frame = cvQueryFrame( capture );
// Cecks if a frame is obtained
if( !frame )
{
fprintf( stderr, "ERROR: frame is null...\n" );
getchar();
break;
}
// Flips the frame into mirror image
cvFlip(frame,frame,1);
// Call the function to detect and draw the hand positions
detect_and_draw(frame);
//If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
//remove higher bits using AND operator
if( (cvWaitKey(10) & 255) == 27 )
break;
}
// Release the capture device housekeeping
cvReleaseCapture( &capture );
return 0;
}
What kind of cpu are you using? Last time I had the error: 0xC000001D : Illegal instruction was related to the SSE instruction used in the code. Some new SSE instruction are not implemented at AMD processors e.g. So you can fix this by rebuilding opencv without SSE support.
I also have this problem when using cv::Mat(...)
The same exception throw at
size_t esz = CV_ELEM_SIZE(_type), esz1 = CV_ELEM_SIZE1(_type);
Not so sure why but after changing Visual C++ Project floating point model from precise to fast, the problem solve.

Opencv 2.4.3 How to Convert IPL_DEPTH_8U to IPL_DEPTH_32F?

char* filename1="1.bmp";
IplImage* greyLeftImg= cvLoadImage(filename1,0);
char* filename2="2.bmp";
IplImage* greyRightImg= cvLoadImage(filename2,0);
IplImage* greyLeftImg32=cvCreateImage(cvSize(width,height),32,greyLeftImg->nChannels);//IPL_DEPTH_32F
IplImage* greyRightImg32=cvCreateImage(cvSize(width,height),32,greyRightImg->nChannels);
Always failed,said " Assertion failed (src.size == dst.size && dst.type() == CV_8UC(src.channels())) in unknown function"
I have searched for many methods , but none of them seems to work?
A simple step to convert any gray scale 8 bit or 16 bit uint images in opencv to 32 bit floating type is like this...
IplImage* img = cvLoadImage( "E:\\Work_DataBase\\earth.jpg",0);
IplImage* out = cvCreateImage( cvGetSize(img), IPL_DEPTH_32F, img->nChannels);
double min,max;
cvMinMaxLoc(img,&min,&max);
// Remember values of the floating point image are in the range of 0 to 1, which u
// can't visualize by cvShowImage().......
cvCvtScale(img,out,1/ max,0);
Hope it is easy way...
Here is a simple function to convert any IplImage to 32 bit float.
IplImage* convert_to_float32(IplImage* img)
{
IplImage* img32f = cvCreateImage(cvGetSize(img),IPL_DEPTH_32F,img->nChannels);
for(int i=0; i<img->height; i++)
{
for(int j=0; j<img->width; j++)
{
cvSet2D(img32f,i,j,cvGet2D(img,i,j));
}
}
return img32f;
}
An important consideration is that for floating point images in OpenCV, only those can be visualized whose pixel values are from 0.0 and 1.0.
To visualize the floating point image, you have to scale the values from 0.0 to 1.0.
Here is an example for how to do this:
IplImage* img8u = cvLoadImage(filename1,0);
IplImage* img32f = convert_to_float32(img8u);
cvShowImage("float image",img32f); //Image will not be shown correctly
cvWaitKey(0);
cvScale(img32f, img32f, 1.0/255.0);
cvShowImage("float image normalized",img32f); //Image will be shown correctly now
cvWaitKey(0);

cvCvtColor faild

I try to run the following program according to what is written on this site
http://www.lirtex.com/robotics/fast-object-tracking-robot-computer-vision/
Initialization parameters
CvCapture *capture = cvCaptureFromCAM(0);
int width = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH );
int height = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT );
IplImage *frame;
CvSize size = cvSize(640,480);
cvNamedWindow( "Camera", CV_WINDOW_NORMAL );
cvNamedWindow( "HSV", CV_WINDOW_NORMAL );
cvNamedWindow( "EdgeDetection", CV_WINDOW_NORMAL );
cvResizeWindow("Camera",640,480);
cvResizeWindow("HSV",640,480);
cvResizeWindow("EdgeDetection",640,480);
IplImage * hsv_frame = cvCreateImage(size, IPL_DEPTH_8U, 3);
IplImage* thresholded = cvCreateImage(size, IPL_DEPTH_8U, 1);
when im calling the function cvCvtColor like this:
cvCvtColor(frame, frame , CV_BGR2HSV);
with the 2nd paramter is equal to the first its work
but when im calling the function like this
cvCvtColor(frame, hsv_frame , CV_BGR2HSV);
its fail and throws an exception
Does anyone have any idea why this happens?
The Array frame is the size of the captured image whereas, *hsv_frame* has been hardcoded to 640x480. The colorspace conversion operation(cvCvtColor) requires that both the source and destination be of the same size and so the exception.
Solution:
a. Use the same size as the input by setting
CvSize size = cvSize(frame->width, frame->height);
-OR-
b. If you want the final o/p to be 640x480, resize the input to the required size using: cvResize
One final note: The C++ interface is much more intuitive, much more stable and has added features as compared to the C interface. I recommend moving to the C++ interface of OpenCV

histogram on opencv

hey i tried to made a histogram that shows frames substraction, the code is running but i got gray window without result.
the message on the command window is:
Compiler did not align stack variables. Libavcodec has been miscompiled
and may be very slow or crash. This is not a bug in libavcodec,
but in the compiler. You may try recompiling using gcc >= 4.2.
Do not report crashes to FFmpeg developers.
OpenCV Error: Assertion failed (images[j].channels() == 1) in unknown function,
file ........\ocv\opencv\src\cv\cvhistogram.cpp, line 137
here is the code someone have an idea?thanks for help.....
int main()
{
int key = 0;
CvCapture* capture = cvCaptureFromAVI( "macroblock.mpg" );
IplImage* frame = cvQueryFrame( capture );
IplImage* currframe = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,3);
IplImage* destframe = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,3);
IplImage* imgHistogram = 0;
CvHistogram* hist;
if ( !capture )
{
fprintf( stderr, "Cannot open AVI!\n" );
return 1;
}
int fps = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FPS );
cvNamedWindow( "dest", CV_WINDOW_AUTOSIZE );
cvNamedWindow( "imgHistogram", CV_WINDOW_AUTOSIZE );
while( key != 'x' )
{
frame = cvQueryFrame( capture );
currframe = cvCloneImage( frame );
frame = cvQueryFrame( capture );
cvSub(frame,currframe,destframe);
int bins = 256;
int hsize[] = {bins};
float max_value = 0, min_value = 0;
float value;
int normalized;
float xranges[] = {0, 256};
float* ranges[] = {xranges};
IplImage* planes[] = {destframe};
hist = cvCreateHist(1, hsize, CV_HIST_ARRAY, ranges,1);
cvCalcHist(planes, hist, 0, NULL);
cvGetMinMaxHistValue(hist, &min_value, &max_value);
// printf("Minimum Histogram Value: %f, Maximum Histogram Value: %f\n", min_value, max_value);
imgHistogram = cvCreateImage(cvSize(bins, 50),IPL_DEPTH_8U,3);
cvRectangle(imgHistogram, cvPoint(0,0), cvPoint(256,50), CV_RGB(255,255,255),-1);
for(int i=0; i < bins; i++){
value = cvQueryHistValue_1D(hist, i);
normalized = cvRound(value*50/max_value);
cvLine(imgHistogram,cvPoint(i,50), cvPoint(i,50-normalized), CV_RGB(0,0,0));
}
if(key==27 )break;
cvShowImage( "dest",destframe);
cvShowImage( "imgHistogram",imgHistogram);
key = cvWaitKey( 1000 / 10 );
}
cvDestroyWindow( "dest" );
cvReleaseCapture( &capture );
return 0;
}
Since you are trying to show a 1D histogram, the histogram plane needs to be in grayscale. So, you need to convert the resulting image from cvSub() to grayscale first. Try
IplImage *gray = NULL;
gray = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);
while(key != 'x') {
...
cvSub(frame, currframe, destframe);
cvCvtColor(destframe, gray, CV_BGR2GRAY);
...
IplImage* planes[] = {gray};
..
}
Let me know if it works for you.

OpenCV cannot find video device

I cannot use webcam as input device for OpenCV 2.3.1 in Ubuntu 11.04, this code works fine on windows:
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
// A Simple Camera Capture Framework
int main() {
CvCapture* capture = cvCaptureFromCAM(-1);
if( !capture ) {
fprintf( stderr, "ERROR: capture is NULL \n" );
getchar();
return -1;
}
// Create a window in which the captured images will be presented
cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
// Show the image captured from the camera in the window and repeat
while( 1 ) {
// Get one frame
IplImage* frame = cvQueryFrame( capture );
if( !frame ) {
fprintf( stderr, "ERROR: frame is null...\n" );
getchar();
break;
}
cvShowImage( "mywindow", frame );
// Do not release the frame!
//If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
//remove higher bits using AND operator
if( (cvWaitKey(10) & 255) == 27 ) break;
}
// Release the capture device housekeeping
cvReleaseCapture( &capture );
cvDestroyWindow( "mywindow" );
return 0;
}
It returns "ERROR: capture is NULL "
I found the solution, I need to install libv4l-0 and libv4l-dev then compile OpenCV with USE_V4L=ON.

Resources