Skip to content

Instantly share code, notes, and snippets.

@yeban
Created October 21, 2009 15:49
Show Gist options
  • Save yeban/215204 to your computer and use it in GitHub Desktop.
Save yeban/215204 to your computer and use it in GitHub Desktop.
IplImage* dilate( const IplImage* src, const IplImage* kernel){
//create a destination image with the same parametrs as the source image
IplImage* dst = cvCreateImage(cvSize(src->height, src->width), IPL_DEPTH_8U, 1);
vector<uchar> pix_values; //to store all the pixels that are covered
int i, j;
//loop over each row
for( int row = 0; row < src->height; row++ ){
//intialise pointers to the start of the row of destination image
uchar* dst_row_ptr = (uchar*)(dst->imageData + row*dst->widthStep);
//loop over each column in the row
for( int column = 0; column < src->width; column++ ){
/* for every pixel in the image at (row, column)
* collect the intensities of all the pixels
* swept over by the kernel, in the vector pix_values
*/
pix_values.clear();
for( int krow = 0; krow < kernel->height; krow++){
/* i marks the rows of the source image that are swept by the kernel
* so initialise a temp pointer to the ith row of the source
* and krow row of the kernel
*/
i = row + krow - kernel->height/2;
uchar* src_row_ptr = (uchar*)(src->imageData + i*src->widthStep);
uchar* kernel_row_ptr = (uchar*)(kernel->imageData + krow*kernel->widthStep);
for( int kcolumn = 0; kcolumn < kernel->width; kcolumn++){
// j marks the columns of the source image that are swept by the kernel
j = column + kcolumn - kernel->width/2;
/* the value at the (i, j) pixel of the source is to be collected
* if (i, j) lie on the image and
* the kernel's pixel at the same location is foreground
*/
if(i >= 0 && i < src->height && j >= 0 && j < src->width)
if( kernel_row_ptr[kcolumn] == 255 )
pix_values.push_back(src_row_ptr[j]);
}
}
/* max or min value of all the collected pixel goes in the desitnaiton
* for dilation or erosion respectively
*/
dst_row_ptr[column] = *max_element(pix_values.begin(), pix_values.end()); //the max goes in the destination
}
}
return dst;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment