Opencv and code blocks troubleshooting

Asked By 0 points N/A Posted on -
qa-featured

I'm getting an undefined reference to cv::Mat::Mat error when i compile this program

 
#include <opencv/cv.h>
#include <highgui.h>
#include <stdio.h>
#include <limits>
#include <stdlib.h>
 
using namespace cv;
using namespace std;
 
IplImage* g_image = NULL;
IplImage* g_gray = NULL;
int g_thresh = 100;
CvMemStorage* g_storage = NULL;
 
void on_trackbar(int)
{
if( g_storage == NULL )
{
g_gray = cvCreateImage( cvGetSize( g_image ), 8, 1 );
g_storage = cvCreateMemStorage(0);
}
else
{
cvClearMemStorage( g_storage );
}
 
CvSeq* contours = 0;
cvCvtColor( g_image, g_gray, CV_BGR2GRAY );
cvThreshold( g_gray, g_gray, g_thresh, 255, CV_THRESH_BINARY );
cvFindContours( g_gray, g_storage, &contours );
cvZero( g_gray );
if( contours )
{
cvDrawContours(
g_gray,
contours,
cvScalarAll(255),
cvScalarAll(255),
100 );
}
cvShowImage( "Contours", g_gray );
}
 
Rect boundingRect ( const CvSeq* contour )
{
// Points initialization
Point min ( std::numeric_limits<int>::max(),std::numeric_limits<int>::max() );
Point max ( std::numeric_limits<int>::min(),std::numeric_limits<int>::min() );
 
// Getting bounding box coordinates
for( int i=0; i<contour->total; ++i )
{
CvPoint* p = CV_GET_SEQ_ELEM( CvPoint, contour, i );
printf(" (%d,%d)n", p->x, p->y );
if ( p->x > max.x ) max.x=p->x;
if ( p->x < min.x ) min.x=p->x;
 
if ( p->y > max.y ) max.y=p->y;
if ( p->y < min.y ) min.y=p->y;
}
 
 
return Rect ( min.x,min.y,max.x-min.x+1,max.y-min.y+1 );
}
 
int main(int argc, char * argv[])
{
vector<vector<Point> > v;
g_image = cvLoadImage( "sample1.jpg" );
cvNamedWindow( "Contours", 1 );
//cvCreateTrackbar( "Threshold", "Contours", &g_thresh, 255, on_trackbar );
//on_trackbar(0);
if( g_storage == NULL )
{
g_gray = cvCreateImage( cvGetSize( g_image ), 8, 1 );
g_storage = cvCreateMemStorage(0);
}
else
{
cvClearMemStorage( g_storage );
}
 
CvSeq* contours = 0;
cvCvtColor( g_image, g_gray, CV_BGR2GRAY );
cvThreshold( g_gray, g_gray, 160, 255, CV_THRESH_BINARY );
cvFindContours( g_gray, g_storage, &contours);
cvZero( g_gray );
if( contours)
{
cvDrawContours(
g_gray,
contours,
cvScalarAll(255),
cvScalarAll(255),
100 );
}
cvShowImage( "Contours", g_gray );
int size = 0;
int i=0;
CvSeq* max;
for(CvSeq* c=contours; c!=NULL; c=c->h_next )
{
printf("Size of %d contour : %dn",i,c->total);
if((c->total)>size)
{
size=c->total;
max=c;
}
i++;
}
printf("Size of %d contour : %dn",i,max->total);
Rect rect = boundingRect(max);
Point pt1, pt2;
pt1.x = rect.x;
pt1.y = rect.y;
pt2.x = rect.x + rect.width;
pt2.y = rect.y + rect.height;
// Draws the rect in the original image and show it
 
cvRectangle(g_image, pt1, pt2, CV_RGB(255,0,0), 1);
 
cvNamedWindow( "Ejemplo", CV_WINDOW_AUTOSIZE );
imshow("Ejemplo", g_image );
cvWaitKey();
 
return 0;
}
 
 
Please help. I have included all libraries and set the path for search directories
SHARE
Answered By 55 points N/A #127721

Opencv and code blocks troubleshooting

qa-featured

I think that you are trying to code in linux. Actually the programming in linux is always a bit tricky because the dependencies are always different from the one used in windows. but if you are on windows then this will work too.

As you are getting the cv mat error, it seems that there is something wrong with the declaration type of the cv that you are using in the program.

Also the parameters that you are using must be the exact that you are passing it to. These must be the same because it is a crucial thing that must be followed every time.

you can also use the code given below inside your code and see if the error is eliminated.

v::Mat image1, image2;
cv::Rect rect1, rect2;
...
cv::Mat extractedImage2 = image1(rect2);
image1(rect1).copyTo(extractedImage2);

 

Related Questions