梯度求法和sobel之類的算子雷同,甚至更簡單,就是一個離散差分,不清楚的童鞋可以百度,一大堆資料呢,從源碼也可清晰的看出原理。 -
-
-
-
-
-
-
-
-
- void Grad(SIZE sz, LPBYTE pGray,int *pGradX, int *pGradY, int *pMag)
- {
- LONG y,x;
-
-
- double dSqt1;
- double dSqt2;
-
-
- for(y=1;y<sz.cy-1;y++)
- {
- for(x=1;x<sz.cx-1;x++)
- {
- pGradX[y*sz.cx +x] = (int)( pGray[y*sz.cx+x+1]-pGray[y*sz.cx+ x-1] );
- }
- }
-
-
- for(x=1;x<sz.cx-1;x++)
- {
- for(y=1;y<sz.cy-1;y++)
- {
- pGradY[y*sz.cx +x] = (int)(pGray[(y+1)*sz.cx +x] - pGray[(y-1)*sz.cx +x]);
- }
- }
-
-
- for(y=0; y<sz.cy; y++)
- {
- for(x=0; x<sz.cx; x++)
- {
-
- dSqt1 = pGradX[y*sz.cx + x]*pGradX[y*sz.cx + x];
- dSqt2 = pGradY[y*sz.cx + x]*pGradY[y*sz.cx + x];
- pMag[y*sz.cx+x] = (int)(sqrt(dSqt1+dSqt2)+0.5);
- }
- }
- }
用到的類型: - typedef struct {
- int cy;
- int cx;
- } SIZE;
-
- typedef unsigned char *LPBYTE;
-
- typedef long LONG;
|