2 qq 34614890 qq_34614890 于 2016.05.11 16:37 提问

Sobel边缘检测,为何输出不对呢

//Sobel边缘检测

//卷积
// H 模板高
// W 模板宽
// C模板系数
// CX 中心元素横坐标
// CY 中心元素纵坐标
//fpArray 指向模板数组的指针
//C 模板系数

void Templat( unsigned char *dst,int H, int W, int CX,int CY,int C ,int *Template)

{
IplImage*image;
image = cvLoadImage("1.jpg", 0);
cvNamedWindow("image");
cvShowImage("image", image);
int width = image->width;
int height = image->height;
unsigned char *ptr;

int i, j, k, l;

for (j = CY;j < height - H + CY + 1;j++) 
{
    for (i = CX;i < width - W + CX + 1;i++)
    {

        for (k = 0;k < H;k++)  
        {
            for (l = 0;l < W;l++)
            {
                ptr = (unsigned char*)image->imageData + j*image->widthStep + i;
                ptr = ptr + *((unsigned char *)image->imageData + (j - CY + k)*width + (i - CX + l))*Template[k*W + l];
                dst = (unsigned char*)image->imageData + j*image->widthStep + i;
                *ptr*= C;

                *ptr = abs(*ptr);
                if (*ptr > 255)

                {
                    *dst = 255;
                }
                else 
                {
                    *dst = *ptr;
                }
            }
        }
    }
}

}

//sobel水平与垂直边缘检测

void Sobel(IplImage*image, IplImage*image5)
{
unsigned char*dst,*dst1,*dst2;
int width = image->width;
int height = image->height;
int i, j;
int H=3;//模板高
int W=3;//模板宽
int C=1;//模板系数
int CX=1;//中心元素横坐标
int CY=1;//中心元素纵坐标
int Template[9];

for (j = 0;j < height;j++)
{
    for (i = 0;i < width;i++)
    {

        dst1 = (unsigned char*)image->imageData + j*image->widthStep + i;
        dst2 = (unsigned char*)image->imageData + j*image->widthStep + i;

        }
    }

Template[0] = -1;
Template[1] = -2;
Template[2] = -1;
Template[3] = 0;
Template[4] = 0;
Template[5] = 0;
Template[6] = 1;
Template[7] = 2;
Template[8] = 1;

Templat(dst1,H,W,CX,CY,C,Template);

Template[0] = -1;
Template[1] = 0;
Template[2] = 1;
Template[3] = -2;
Template[4] = 0;
Template[5] = 2;
Template[6] = -1;
Template[7] = 0;
Template[8] = 1;

Templat( dst2,H, W, CX, CY, C,Template);

for (j = 0;j < height;j++)
{
    for(i = 0;i < width;i++)
    {
        dst= (unsigned char*)image5->imageData + j*image5->widthStep + i;

        if (*dst1 < *dst2)
        {
            *dst= *dst2;
        }
        else

        {
            *dst = *dst1;
        }

    }
}

}

void main()
{
IplImage*image, *image1,*image2,*image3,*image4,*image5;
image = cvLoadImage("1.jpg", 0);
cvNamedWindow("image");
cvShowImage("image", image);

image1 = cvCreateImage(cvGetSize(image), image->depth, 1);
image2 = cvCreateImage(cvGetSize(image), image->depth, 1);
image3 = cvCreateImage(cvGetSize(image), image->depth, 1);
image4 = cvCreateImage(cvGetSize(image), image->depth, 1);
image5 = cvCreateImage(cvGetSize(image), image->depth, 1);

Zongxiang(image, image1);
Hengxiang(image, image2);
Shuangxiang(image, image3);
Robert(image, image4);
Sobel(image, image5);


//cvNamedWindow("zongxiang");
//cvShowImage("zongxiang", image1);
//cvNamedWindow("hengxiang");
//cvShowImage("hengxiang", image2);
//cvNamedWindow("shuangxiang");
//cvShowImage("shuangxiang", image3);
//cvNamedWindow("Robert");
//cvShowImage("Robert", image4);
cvNamedWindow("Sobel");
cvShowImage("Sobel", image5);


cvWaitKey();

}

1个回答

qq_34614890
qq_34614890   2016.05.11 16:38

高人在哪里,求助啊

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!