Why is my sobel filter code only returning half of the image?

52 Views Asked by At

Im trying to do edge detection on my image without the already existing functions in opencv. I copied this code from somewhere on GitHub, but for some reason it only does half the image, I thints because of the image type but im not sure.

enter image description here

Mat Picture::sobelFilter()
{
    //Sobel X Filter
    double x1[] = { -1.0, 0, 1.0 };
    double x2[] = { -2.0, 0, 2.0 };
    double x3[] = { -1.0, 0, 1.0 };

    vector<vector<double>> xFilter(3);

    xFilter[0].assign(x1, x1 + 3);
    xFilter[1].assign(x2, x2 + 3);
    xFilter[2].assign(x3, x3 + 3);

    //Sobel Y Filter
    double y1[] = { 1.0, 2.0, 1.0 };
    double y2[] = { 0, 0, 0 };
    double y3[] = { -1.0, -2.0, -1.0 };

    vector<vector<double>> yFilter(3);
    yFilter[0].assign(y1, y1 + 3);
    yFilter[1].assign(y2, y2 + 3);
    yFilter[2].assign(y3, y3  + 3);

    //Limit Size
    int size = (int)xFilter.size() / 2;

    Mat filteredImg = Mat(_image.rows - 2 * size, _image.cols - 2 * size, CV_16UC1);

    angleMap = Mat((_image.rows - 2) * size, (_image.cols - 2) * size, CV_32FC1); //AngleMap

    for (int i = size;i < _image.rows - size; i++)
    {
        for (int j = size; j < _image.cols - size; j++)
        {
            double sumx = 0;
            double sumy = 0;

            for (int x = 0; x < xFilter.size(); x++)
                for (int y = 0; y < yFilter.size(); y++)
                {
                    sumx += xFilter[x][y] * (double)(_image.at<uchar>(i + x - size, j + y - size)); //Sobel_X Filter Value
                    sumy += yFilter[x][y] * (double)(_image.at<uchar>(i + x - size, j + y - size)); //Sobel_Y Filter Value
                }
            double sumxsq = sumx * sumx;
            double sumysq = sumy * sumy;

            double sq2 = sqrt(sumxsq + sumysq);

            if (sq2 > 255) //Unsigned Char Fix
                sq2 = 255;
            filteredImg.at<uchar>(i - size, j - size) = sq2;

            if (sumx == 0) //Arctan Fix
                angleMap.at<float>(i - size, j - size) = 90;
            else
                angleMap.at<float>(i - size, j - size) = atan(sumy / sumx);

        }
    }


    return filteredImg;
}

I tried chaging the type of the original image but that just made a mess...Im really stuck please help

0

There are 0 best solutions below