Posts Tagged ‘basic’

OpenCv; Small Code using Erode and Dilate

February 16, 2009 Leave a comment

Small Code that reads pbm image, convert it into 1 channel image. Erodes it, dilates it and shows them

#include <float.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>
#include <iostream>
#include "BlobResult.h"
#include "blob.h"

#ifdef _EiC

#define WIN32


using namespace std;

void main(void){

IplImage *inputImage = cvLoadImage( "0584.pbm", 1 );

IplImage *gray = cvCreateImage( cvSize(inputImage->width,inputImage->height), 8, 1 );

cvCvtColor( inputImage, gray, CV_BGR2GRAY );

IplImage*img = inputImage;

inputImage = gray;


cvNamedWindow( "orig", 1 );

cvShowImage( "orig", inputImage );

IplImage *outputImage;

IplImage *outputImage2;

outputImage = cvCreateImage( cvSize( inputImage->width, inputImage->height ), IPL_DEPTH_8U, 1 );

outputImage2 = cvCreateImage( cvSize( inputImage->width, inputImage->height ), IPL_DEPTH_8U, 1 );

cvErode(inputImage, outputImage, 0, 1);

cvNamedWindow( "erode", 1 );

cvShowImage( "erode", outputImage );

cvDilate(outputImage, outputImage2, 0, 2);

cvNamedWindow( "dilate", 1 );

cvShowImage( "dilate", outputImage2 );






connecting OpenCv with Matlab; Basic

November 13, 2008 24 comments

It took much of the time, but I was able to connect OpenCv with matlab.

Much thanks to the

I took the introductory program given in the

Also help from

Writing C Functions in Matlab form Jason Laska

And modified it to run through matlab.

I named it showImageMt.cpp

So the Matlab code for compiling it was

mex showImageMt.cpp

But for that you have setup the file ‘mexopts.bat‘ as described in the above pdf . You might not able to find it by searching the matlab folder. You can find the path by running the following command in the Matlab


The contents of the file could be see by type(fullfile(prefdir,’mexopts.bat’))

The C++ opencv code is following

// This is a simple, introductory OpenCV program. The program reads an
// image from a file, inverts it, and displays the result.
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>

#include "mex.h"

void justWork(IplImage *img){
int height,width,step,channels;
uchar *data;
int i,j,k;

// get the image data
height    = img->height;
width     = img->width;
step      = img->widthStep;
channels  = img->nChannels;
data      = (uchar *)img->imageData;
printf("Processing a %dx%d image with %d channels\n",height,width,channels);

// create a window
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
cvMoveWindow("mainWin", 100, 100);

// invert the image
for(i=0;i<height;i++) for(j=0;j<width;j++) for(k=0;k<channels;k++)

// show the image
cvShowImage("mainWin", img );

// wait for a key


void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]){

if (nrhs != 0)
mexErrMsgTxt("One input required.");
else if (nlhs > 0)
mexErrMsgTxt("Too many output arguments");

IplImage* img = 0;

printf("Usage: main <image-file-name>\n\7");

char *name = "lena.jpg";
// load an image
printf("Could not load image file: %s\n",name);
// release the image
cvReleaseImage(&img );

Read more…

Categories: basic, matlab, OpenCv Tags: , , ,