opencv error: opencv fail to allocate memory

asked 2013-05-12 03:00:32 -0500

Nermeen gravatar image

hi i wrote the following code in opencv c++ with visual studio 2005 this code will show a scroll bar that allow the user to define an area of an image then it will send the new image of the chosen area to hsv_seg function that will segment a part of the image using predefined hue, saturation, & value values. i sometimes used iplimage and other used cvmat is that cause such problem? please help ,,,,any help will be heavily appreciated

##

#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
#define PIXEL(img,x,y) (img)->imageData[(x)*img->widthStep+(y)]
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;




int high_switch_h = 0;
int highInth = 0;
int low_switch_h = 0;
int lowInth = 0;

void switch_h_callback_h( int position ){
    highInth = position;


}
void switch_h_callback_l( int position ){
    lowInth = position;
}


int high_switch_s = 0;
int highInts = 0;
int low_switch_s = 0;
int lowInts = 0;

void switch_s_callback_h( int position ){
    highInts = position;


}
void switch_s_callback_l( int position ){
    lowInts = position;
}


int high_switch_v = 0;
int highIntv = 0;
int low_switch_v = 0;
int lowIntv = 0;

void switch_v_callback_h( int position ){
    highIntv = position;


}
void switch_v_callback_l( int position ){
    lowIntv = position;
}

void hsvSeg(const cv::Mat& input_image,const cv::Mat& hsv ,cv::Mat &output1, cv::Mat &output2,int low_h,int low_s, int low_v,int high_h,int high_s, int high_v) {
    // modify output1
    hsv.copyTo(output1);

    Mat bw;
    inRange(output1, Scalar(low_h, low_s, low_v), Scalar(high_h, high_s, high_v), bw);// segmentaion based on hsv min & max values

    vector<vector<Point> > contours;
    findContours(bw.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

    Mat dst = Mat::zeros(input_image.size(), input_image.type());
    drawContours(dst, contours, -1, Scalar::all(255), CV_FILLED);
    dst.copyTo(output2);
    output2 &= input_image ;

}



int main()

{
IplImage *RgbImg,*imHSV,*outImgh , *h_plane , *s_plane, *v_plane ,*alpha,*outImgs,*outImgv,*outImg ,*outH,*outS,*outV;
int height,width,channels;

cv::Mat input_image = imread("m3.jpg");
       if (input_image.empty())
        return -1;

    Mat filter_img;
    medianBlur(input_image,filter_img,3);

    IplImage rgb=filter_img; //converting from cvmat to iplimage
    RgbImg =&rgb;



/*

RgbImg=cvLoadImage("m3.jpg",1);



if(!RgbImg){
printf("Could not load image file: %s\n");
cvWaitKey(0);
exit(0);
}
*/
//
//cvNamedWindow("Oimg", CV_WINDOW_AUTOSIZE); 
//cvShowImage("Oimg", RgbImg);
//cvWaitKey(0);
outImg  = cvCreateImage(cvGetSize(RgbImg), IPL_DEPTH_8U, 1);
outImgh  = cvCreateImage(cvGetSize(RgbImg), IPL_DEPTH_8U, 1);
outImgs  = cvCreateImage(cvGetSize(RgbImg), IPL_DEPTH_8U, 1);
outImgv  = cvCreateImage(cvGetSize(RgbImg), IPL_DEPTH_8U, 1);
outH  = cvCreateImage(cvGetSize(RgbImg), IPL_DEPTH_8U, 1);
outS  = cvCreateImage(cvGetSize(RgbImg), IPL_DEPTH_8U, 1);
outV  = cvCreateImage(cvGetSize(RgbImg), IPL_DEPTH_8U, 1);

//cvCvtColor(RgbImg,GrayImg,CV_RGB2GRAY);
height = RgbImg->height;
width = RgbImg->width;
channels = RgbImg->nChannels;
imHSV = cvCreateImage(cvGetSize(RgbImg), IPL_DEPTH_8U, 3);


h_plane  = cvCreateImage(cvGetSize(RgbImg), 8, 1);
s_plane  = cvCreateImage(cvGetSize(RgbImg), 8, 1);
v_plane  = cvCreateImage(cvGetSize(RgbImg), 8, 1);
//
alpha  = cvCreateImage(cvGetSize(RgbImg), IPL_DEPTH_8U, 3);

//cvSplit(RgbImg,red,green,blue,alpha);
cvCvtColor(RgbImg,imHSV,CV_RGB2HSV);


cvCvtPixToPlane(imHSV, h_plane, s_plane, v_plane, 0 );

// Create trackbars
cvNamedWindow("H", CV_WINDOW_AUTOSIZE); 
cvShowImage("H", h_plane);
    cvCreateTrackbar( "High_h", "H", &high_switch_h, 255, switch_h_callback_h );
    cvCreateTrackbar( "Low_h", "H", &low_switch_h, 255, switch_h_callback_l );
    cvCreateTrackbar( "High_s", "H", &high_switch_s, 255, switch_s_callback_h );
    cvCreateTrackbar( "Low_s", "H", &low_switch_s, 255, switch_s_callback_l );
    cvCreateTrackbar( "High_v", "H", &high_switch_v, 255, switch_v_callback_h );
    cvCreateTrackbar( "Low_v", "H", &low_switch_v, 255, switch_v_callback_l );


    while( 1 ) {    
        cvThreshold(h_plane,outImgh, low_switch_h ...
(more)
edit retag flag offensive close merge delete

Comments

it has been solved by moving " CvMat *mat_hsv = cvCreateMat(RgbImg->height,RgbImg->width,CV_32FC3 );// creat mat image" out of while loop

Nermeen gravatar imageNermeen ( 2013-05-12 16:49:22 -0500 )edit