Error del operador CANNY al procesar imágenes VC
Además, si hay algún problema con tu operador CANNY, por favor compruébalo.
Además
Proporcione fragmentos de código. ¿Escribió el algoritmo usted mismo?
BOOL CannyEdegeDetectDIB(HDIB hDib)
{
int GX[3][3], GY[3][3];
int sumaX, sumaY, SUMA;
int r, c, i, j;
doble ORIENT
int edgeDirection; p> int umbral alto, umbral bajo
int leftPixel, rightPixel
int P1, P2, P3, P4, P5.P6, P7, P8; long lOffset;
WaitCursorBegin();
// 1. Convertir a escala de grises
ConvertToGrayscale(hDib, MEAN_GRAY, 0, 0, 0); p>
// 1.
// 2. Filtro gaussiano DIB
if (! GausianFilterDIB(hDib))
{
WaitCursorEnd();
return FALSE
}
// 3.proceso.... p>
// Máscara Sobel 3x3 GX. Referencia: www.cee.hw.ac.uk/hipr/html/sobel.html
GX[0][0] = -1; GX[0][1] = 0; ][2] = 1;
GX[1][0] = -2; GX[1][1] = 0; p> GX[2][0] = -1; GX[2][1] = 0; GX[2][2] = 1;
// 3x3 GY Máscara de Sobel.
referencia: www.cee.hw.ac.uk/hipr/html/sobel.html
GY[0][0] = 1; GY[0][1] = 2; [2] = 1;
GY[1][0] = 0; GY[1][1] = 0; GY[2][0] = -1; GY[2][1] = -2; GY[2][2] = -1; // Copiar a un nuevo DIB para almacenar los datos originales
HDIB hNewDib = CopyHandle(hDib);
if (!hNewDib)
{
WaitCursorEnd();
return FALSE;
}
// Nuevas propiedades DIB
WORD wDIBWidth = (WORD)DIBWidth( hNewDib);
WORD wDIBHeight = (WORD)DIBHeight( hNewDib);
WORD wBytesPerLine = (WORD)BytesPerLine( hNewDib
// El DIB original se convierte en la imagen de destino
;LPBYTE lpDestDIB = (LPBYTE)GlobalLock(hDib);
LPBYTE lpDestImage = FindDIBBits(lpDestDIB);
// Obtener la dirección de bit en el DIB
LPBYTE lpDIB = (LPBYTE)GlobalLock(hNewDib);
LPBYTE lpDIBits = FindDIBBits(lpDIB);
for(r=0; rlt; wDIBHeight; r )
{
for(c=0; clt; wDIBWidth; c )
{
sumaX = 0;
sumY = 0;
// Límite de la imagen
if(r==0 || r== wDIBHeight-1)
SUM = 0; p >
else if(c==0 || c==wDIBWidth-1)
SUM = 0
// La convolución comienza aquí
más
{
/********************************* **** **
* Aproximación del gradiente X
**************************** ** *****/
for(i=-1; ilt.=1; i )
{
for(j=-1 ; jlt ; = 1; j )
{
lOffset = PIXEL_OFFSET(r i, c j, wBytesPerLine); lpDIBits lOffset ) * GX[i 1][j 1]);
}
}
/******************************
* Aproximación de gradiente Y p>
*********** ***************/
for(i=-1; ilt; =1 ; i )
{
for(j=-1; jlt;=1; j )
{
lOffset = PIXEL_OFFSET (r i , c j, wBytesPerLine);
sumaY = sumaY (int)(int)(*(lpDIBits lOffset) * GY[i 1][j 1]); p>
}
/************************************ ** ******* *************
* APROXIMACIÓN DE MAGNITUD DEL GRADIENTE (Myler p. 218).
218)
******************************************* ******/
SUMA = abs(sumaX) abs(sumaY);
if(SUMgt; 255)
SUMA=255; /p>
if(SUMlt; 0)
SUM=0
}
/***********; * ************* *****
* Dirección web
************** *** **********/
/* No se puede dividir por cero*/
if(sumX == 0)
{ p>
if(sumY==0)
ORIENT = 0.0
else if (sumYlt; 0)
{ p>
sumaY = -sumY;
ORIENTACIÓN = 90,0
}
más
ORIENTACIÓN = 90,0
p>}
// No se puede tomar el invtan del ángulo en el segundo cuadrilátero
else if(sumXlt; 0 amp; amp; sumYgt; 0)
{
sumaX = -sumX;
ORIENT = 180 - ((atan((flotador)(sumaY)/(flotador)(sumaX))))* (180/ PI ));
}
// No se puede tomar el ángulo invtan en el 4to cuadrilátero
else if(sumXgt; 0 amp; amp; sumYgt; 0 amp ; sumaYgt; 0); {
amp; sumaYlt; 0)
{
sumaY = -sumY; = 180 - ((atan((float)(sumY)/(float)(sumX))))* (180/PI));
}
// De lo contrario, el el ángulo está en el 1er o 3er cuadrilátero
else
ORIENT = (atan((float)(sumY)/(float)(sumX))))* (180 /PI) ;
/****************************************** ************
* Encuentre edgeDirection asignando ORIENT a
* 0, 45, 90 o 135 grados, dependiendo de ORIENT Qué valor es más cercano
*.
* El valor de ORIENT es el más cercano
**************************** * ***********************/
if(ORIENT lt; 22.5)
edgeDirection = 0; /p> p>
else if(ORIENT lt; 67.5)
edgeDirection = 45;
else if(ORIENT lt; 112.5)
edgeDirection = 90;
else if(ORIENT lt; 157.5)
else if(ORIENT lt; 157.5)
else if(ORIENT lt; 157.5) p>
else lt; 157.5)
edgeDirection = 135;
else
edgeDirection = 0; *** ************************************************* *
* Obtiene los valores de 2 píxeles adyacentes en la dirección del borde
* Dirección.
******************************************* *********/
if( edgeDirection == 0)
{
lOffset = PIXEL_OFFSET(r, c-1, wBytesPerLine );
leftPixel = (int)(*(lpDIBits lOffset));
lOffset = PIXEL_OFFSET(r, c 1, wBytesPerLine); (int)(*(lpDIBits lOffset));
}
else if(edgeDirection == 45)
{
lOffset = PIXEL_OFFSET(r 1, c-1, wBytesPerLine);
leftPixel = (int)(*(lpDIBits lOffset));
lOffset = PIXEL_OFFSET(r 1, c 1, wBytesPerLine);
rightPixel = (int)(*(lpDIBits lOffset)
}
else if(edgeDirection == 90)
{
lOffset = PIXEL_OFFSET(r-1, c, wBytesPerLine)
leftPixel = (int)(*(lpDIBits lOffset)); > lOffset = PIXEL_OFFSET(r 1, c, wBytesPerLine);
rightPixel = (int)(*(lpDIBits lOffset));
}
else
{
lOffset = PIXEL_OFFSET(r-1, c-1, wBytesPerLine)
leftPixel = (int)(*(lpDIBits lOffset));
lOffset = PIXEL_OFFSET(r-1, c 1, wBytesPerLine);
rightPixel = (int)(*(lpDIBits lOffset )); p>
/********************************************* **** *
* Compare la amplitud actual con dos valores de píxeles adyacentes
*. Si es menor que
* cualquiera de los dos valores adyacentes, suprímalo y convierta
* en un no borde.
************************************* ****** **/
if(SUM lt; leftPixel || SUM lt; rightPixel)
SUMA = 0
else
{
/************************
* Histéresis
**** ******************/
Umbral alto = 120;
Umbral bajo = 40
lOffset = PIXEL_OFFSET(r, c, wBytesPerLine);
if (SUM gt; = highThreshold)
SUM = *(lpDIBits lOffset); // Borde
else); if(SUM lt; lowThreshold)
SUM = 0; // nononedge
// SUMA está entre T1 y T2
else
{
// Determinar el valor de los píxeles adyacentes
lOffset = PIXEL_OFFSET( r-1, c-1, wBytesPerLine
P1 = ( int)*(lpDIBits lOffset);
lOffset = PIXEL_OFFSET(r-1, c, wBytesPerLine);
P2 = (int)*(lpDIBits lOffset);
lOffset = PIXEL_OFFSET(r-1, c 1, wBytesPerLine);
P3 = (int)*(lpDIBits lOffset);
lOffset = PIXEL_OFFSET(r, c) -1, wBytesPerLine);
P4 = (int)*(lpDIBits lOffset);
lOffset = PIXEL_OFFSET(r, c 1, wBytesPerLine);
P5 = (int)*( lpDIBits lOffset);
P5 = (int)*( lpDIBits lOffset);
lOffset = PIXEL_OFFSET(r 1, c-1, wBytesPerLine);
P6 = (int)*( lpDIBits lOffset);
lOffset = PIXEL_ OFFSET(r 1, c, wBytesPerLine);
P7 = (int ) *(lpDIBits lOffset);
lOffset = PIXEL_OFFSET(r 1, c 1, wBytesPerLine);
P8 = (int)*(lpDIBits lOffset);
P8 = (int)* (lpDIBits lOffset);
// Comprobar píxeles adyacentes
Si el valor es un borde
lOffset = PIXEL_OFFSET(r, c, wBytesPerLine
if( P1 gt; highThreshold || P2 gt; highThreshold ||
P3 gt; umbral alto || P4 gt; umbral alto ||
umbral alto ||| umbral alto ||| SUM = *(lpDIBits lOffset); // crear borde
else
SUM = 0; // crear sin borde
else p>
SUM = 0; // hacer sin borde
else
SUM = 0; // hacer sin borde
} p>
}
lOffset = PIXEL_OFFSET(r, c, wBytesPerLine);
*(lpDestImage lOffset) = (BYTE)(SUM); p>
}
// Limpiar el búfer temporal
GlobalUnlock(hNewDib
GlobalFree(hNewDib
GlobalUnlock(hDib);
WaitCursorEnd();
devuelve VERDADERO;