I actually got an output from the custom kernel that you have created. I used Python OpenCV to do this, but the way of calling the functions in OpenCV are pretty much the same. To be self-contained, this is the Python code I called for your image using the Sobel and your custom kernel:
import cv2
import numpy as np
im = cv2.imread('Nj9fM.png'); #// Save image to computer first
#// Call using built-in Sobel
out1 = cv2.Sobel(im, cv2.CV_16S, 0, 1, 3)
out1 = cv2.convertScaleAbs(out1.copy())
#// Create custom kernel
xVals = np.array([0.125,0,-0.125,0.25,0,-0.25,0.125,0,-0.125]).reshape(3,3)
#// Call filter2D
out2 = cv2.filter2D(im, cv2.CV_32F, xVals, None, (-1,-1), 0, cv2.BORDER_DEFAULT)
out2 = cv2.convertScaleAbs(out2.copy())
cv2.imshow('Output 1', out1)
cv2.imshow('Output 2', out2)
cv2.waitKey(0)
cv2.destroyAllWindows()
In terms of C++:
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char* argv[])
{
Mat im = imread("Nj9fM.png", CV_LOAD_IMAGE_COLOR); // Save image to computer first
// Call using built-in Sobel
Mat out1, out2;
Sobel(img, out1, CV_16S, 1, 0, 3);
convertScaleAbs(out1, out2);
// Create custom kernel
Mat xVals = Mat_<float>(3, 3) << 0.125, 0, -0.125, 0.25, 0, -0.25, 0.125, 0, -0.125;
// Call filter2D
filter2D(im, out2, -1, xVals, Point(-1,-1), 0 ,BORDER_DEFAULT);
convertScaleAbs(out2, out2);
imshow("Output 1", out1);
imshow("Output 2", out1);
waitKey(0);
destroyWindow("Output 1");
destroyWindow("Output 2");
}
If you run this code, you'll actually see both images where the first one uses the built-in Sobel, while the other one uses your custom kernel. The main difference I see between the Sobel implementation and your gradient is the fact that you took the Sobel kernel by divided each element by 8. As such, any gradients you detect, you'll get a contrast reduction and that's what I see. Actually, you are basically taking the gradient result and dividing by 8 and so you are reducing the intensities of the output by a factor of 8. Try doing: float xVals2[9] = {1f,0f,-1f,2f,0f,-2f,1f,0f,-1f}; the actual Sobel kernel, then run your code again. You should see a higher boost in contrast. For Python, this would be:
xVals2 = np.array([1.,0.,-1.,2.,0,-2.,1.,0,-1.]).reshape(3,3)
Also in C++:
Mat xVals2 = Mat_<float>(3, 3) << 1f, 0f, -1f, 2f, 0f, -2f, 1f, 0f, -1f;
If you run your code using this kernel, you'll see that there's a higher contrast. You'll see that there is more noise though, as the gradient values are larger.
float xVals[9] = {1f,0f,-1f,2f,0f,-2f,1f,0f,-1f};the actual Sobel kernel, then run your code again. You should see a higher boost in contrast.