开发者

Converting pbm files into IplImage

Apparen开发者_如何学Gotly, this code gives me error at the last line cvCvtColor(), what is wrong? Is there anything that can be improved in this function?

+(IplImage*) LoadPbmAsIplImage: (NSString*) fileName{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"pbm"];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSString *content = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSArray *lines = [content componentsSeparatedByString:@"\n"];
    NSString *secondLine = [lines objectAtIndex:1]; //because first line (index 0) contains meta data
    int width = [secondLine length];
    int height = [lines count]-1;
    Mat *mat = new Mat(width, height, CV_8UC4);
    MatIterator_<uint> it = mat->begin<uint>();
    for (int i=0; i<[lines count]; i++){
        for (int j=0; j<[[lines objectAtIndex:i] length]; j++){
            int pixelValue = 0;
            if ([[lines objectAtIndex:i] characterAtIndex:j] == '1'){
                pixelValue = 255;
            }
            *it = pixelValue;
        }
    }
    IplImage iplImage = *mat;
    IplImage* rv = cvCreateImage(cvSize(iplImage.width, iplImage.height), IPL_DEPTH_8U, 3);
    cvCvtColor(mat, rv, CV_RGBA2BGR);
    return rv;
}


You mix the C++ interface (cv::Mat) with the C interface (IplImage), why do you do that?

Mat *mat = new Mat(width, height, CV_8UC4);

This is a memory leak, you never delete that map, also there's almost no reason to create a Mat with new - just Mat mat(width, height, CV_8UC4) does the job fine, and you don't need to delete it.

cvCvtColor(mat, rv, CV_RGBA2BGR);

cvCvtColor expects two IplImage*s as arguments, you give it a cv::Mat* and an IplImage*, this can't work. Replace the first mat with &iplImage, or just consistently use the c++ interface.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜