Ask Your Question

Create a structuring element of 3 * 3

asked 2012-11-28 09:14:22 -0500

isaacenrique gravatar image


I'm working Qt and OpenCV. I am currently testing the functions of mathematical morphology.

I'm trying to create a rectangular structuring element of 3 * 3, using the cvCreateStructuringElementEx() function, as follows:

cvCreateStructuringElementEx ee = (3, 3, 1, 1, CV_SHAPE_RECT)

This should create the structuring element I seek not?

Then apply the dilation operation to two previously created images. In the second image using 'ee' and first put so as NULL use default structuring element. so:

cvDilate (entrada_gris, dilatacion1, NULL, iteraciones); cvDilate (entrada_gris, dilatacion2, ee, iteraciones);

Variable 'iteraciones' is the number of iterations that the operation is applied, usually I use 10.

Since the default structuring element is a solid rectangle 3 * 3 (and if I'm creating the structuring element 'ee' correctly), the result should be the same, but it is not because when a comparison between the images:

cvCmp (dilatacion1, dilatacion2, comparison, CV_CMP_EQ);

I get an image that is not completely white (as it should be if the two images are the same right?).

If anyone has an idea of what may be happening or give me some light I would appreciate advance.

Attach program source code in case anyone wants to see.

#include "opencv.hpp"

int main(int argc, char **argv)
    IplImage *entrada, *entrada_gris;
    IplImage *dilatacion1, *dilatacion2, *comparacion;
    IplConvKernel *ee;
    unsigned iteraciones;

    /* cargar imagen de entrada */
    entrada = cvLoadImage( argv[1] );

    /* leer el numero de iteraciones */
    iteraciones = 0;
    for(unsigned i = 0; i < strlen(argv[2]); i++)
        iteraciones = (iteraciones * 10) + (argv[2][i] - '0');

    /* ******************************** */
    FILE *salida = fopen("salida.txt", "w");

    fprintf(salida, "iteraciones = %d\n", iteraciones);
    fclose( salida );
    /* ******************************** */

    /* convertir a escala de gris */
    entrada_gris = cvCreateImage(cvGetSize(entrada), IPL_DEPTH_8U, 1);
    cvCvtColor(entrada, entrada_gris, CV_BGR2GRAY);

    /* asignar la memoria para las imagenes imagenes */
    dilatacion1 = cvCreateImage(cvGetSize(entrada), IPL_DEPTH_8U, 1);
    dilatacion2 = cvCreateImage(cvGetSize(entrada), IPL_DEPTH_8U, 1);
    comparacion = cvCreateImage(cvGetSize(entrada), IPL_DEPTH_8U, 1);

    /* crear EE */
    ee = cvCreateStructuringElementEx(3, 3, 1, 1, CV_SHAPE_RECT);

    /* aplicar dilatacion */
    cvDilate(entrada_gris, dilatacion1, NULL, iteraciones);
    cvDilate(entrada_gris, dilatacion2, ee, iteraciones);

    cvCmp(dilatacion1, dilatacion2, comparacion, CV_CMP_EQ);

    /* crear las ventanas para mostrar las imagenes */
    cvNamedWindow("DILATACION (EE por defecto)", CV_WINDOW_NORMAL);
    cvNamedWindow("DILATACION (EE creado)", CV_WINDOW_NORMAL);

    /* mostrar las imagenes en las ventanas apropiadas */
    cvShowImage("IMAGEN DE ENTRADA", entrada);
    cvShowImage("IMAGEN DE ENTRADA - ESCALA DE GRIS", entrada_gris);
    cvShowImage("DILATACION (EE por defecto)", dilatacion1);
    cvShowImage("DILATACION (EE creado)", dilatacion2);
    cvShowImage("COMPARACION", comparacion);

    /* esperar a que el usuario presione una tecla */

    /* liberar la memoria reservada */
    cvReleaseStructuringElement( &ee );

    cvReleaseImage( &entrada );
    cvReleaseImage( &entrada_gris );
    cvReleaseImage( &dilatacion1 );
    cvReleaseImage( &dilatacion2 );
    cvReleaseImage( &comparacion );

    cvDestroyWindow( "IMAGEN DE ENTRADA" );
    cvDestroyWindow( "IMAGEN DE ENTRADA - ESCALA DE GRIS" );
    cvDestroyWindow( "DILATACION (EE por defecto)" );
    cvDestroyWindow( "DILATACION (EE creado)" );
    cvDestroyWindow( "COMPARACION" );

    return 0;
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2012-11-29 00:56:56 -0500

Michael Burdinov gravatar image

I think the problem is that the anchor is not in center of rectangle, i.e. resulting image is shifted. It should be (3,3,-1,-1,CV_SHAPE_RECT).

edit flag offensive delete link more
Login/Signup to Answer

Question Tools


Asked: 2012-11-28 09:14:22 -0500

Seen: 740 times

Last updated: Nov 29 '12