Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

opencv error: opencv fail to allocate memory

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, high_switch_h, CV_THRESH_BINARY );
        cvThreshold(s_plane,outImgs, low_switch_s, high_switch_s, CV_THRESH_BINARY );
        cvThreshold(v_plane,outImgv, low_switch_v, high_switch_v, CV_THRESH_BINARY );
        for(int i=0; i<height;i++)
            for(int j=0;j<width;j++)
            {
                PIXEL(outImg,i,j) =  PIXEL(outImgs,i,j) * PIXEL(outImgh,i,j)* PIXEL(outImgv,i,j);
                PIXEL(outH,i,j) = PIXEL(outImg,i,j) * PIXEL(h_plane,i,j);
                PIXEL(outS,i,j) = PIXEL(outImg,i,j) * PIXEL(s_plane,i,j);
                PIXEL(outV,i,j) = PIXEL(outImg,i,j) * PIXEL(v_plane,i,j);
            }
            cvMerge(outH,outS,outV,NULL,imHSV);
            cvCvtColor(imHSV,RgbImg,CV_HSV2RGB);
            cvShowImage("RGB image", RgbImg);
            cvShowImage("HSV IMAGE", imHSV);

            CvMat *mat_hsv = cvCreateMat(RgbImg->height,RgbImg->width,CV_32FC3 );// creat mat image 
            cvConvert( imHSV, mat_hsv );

            Mat hsv,hsv_mat;
            cvtColor(mat_hsv, hsv_mat, CV_BGR2HSV);



            imshow("mat-hsv",hsv_mat);







        if( cvWaitKey( 15 ) == 27 ) 

            break;
    }


//cvNamedWindow("HIMG1", CV_WINDOW_AUTOSIZE); 
//cvShowImage("HIMG1", h_plane);
//cvWaitKey(0);


//cvThreshold(RgbImg,RgbImg, g_thresh, 255, CV_THRESH_BINARY );
//  cvFindContours( g_gray, g_storage, &contours );
//imHSV = convertRGBtoHSV(RgbImg);
        // Only frees one of the new images, not both!
//cvReleaseImage(&imHSV);
printf("Processing a %dx%d image with %d channels \n",height,width,channels); 



cvWaitKey(0);



/* Free memory and get out */

cvDestroyWindow("Objects detection");

cvReleaseImage(&imHSV);
cvReleaseImage(&RgbImg);


return 0;

}