Image processing robert, prewitt, sobel operators

robert operator is actually a 22 array, 1, - 1 diagonal, which is a convolution kernel. The purpose is to find the boundary by the difference method, which is rough.
prewitt operator is a 33 convolution kernel, with 1, - 1 on both sides symmetric along the middle or diagonal
sobel operators consider the distance weight, not all of them are 1 or - 1, but on the basis of prewitt, the middle number is 2 or - 2, which is more advanced than prewitt.

The robert code is as follows:

import numpy as np
import cv2

path = r'../test.jpg'
image = cv2.imread(path,0)
robert = [[[1,0],[0,-1]],[[0,-1],[1,0]]]
robert = np.asarray(robert)
hight,width = image.shape

#img_map = np.zeros((hight+2,width+2))
#img_map[1:hight+1,1:width+1] = image

result_map = np.zeros((hight,width))

for i in range(1,hight):
	for j in range(1,width):
		for rob in robert:
			result_map[i-1,j-1] = result_map[i-1,j-1]+np.sum(image[i-1:i+1,j-1:j+1]*rob)

print(result_map)
cv2.imshow('test',result_map.astype(np.uint8))
cv2.imwrite('rob.jpg',result_map.astype(np.uint8))
cv2.waitKey(0)
cv2.destroyWindows()


sobel directly calls the api. If the self-made convolution kernel is similar to the above code, the code is as follows:

import numpy as np
import cv2

path = r'../test.jpg'
image = cv2.imread(path,0)
sobel = cv2.Sobel(image,cv2.CV_64F,1,1,ksize=3)
# robert = [[[1,0],[0,-1]],[[0,-1],[1,0]]]
# robert = np.asarray(robert)
# hight,width = image.shape

#img_map = np.zeros((hight+2,width+2))
#img_map[1:hight+1,1:width+1] = image

# result_map = np.zeros((hight,width))

# for i in range(1,hight):
# 	for j in range(1,width):
# 		for rob in robert:
# 			result_map[i-1,j-1] = result_map[i-1,j-1]+np.sum(image[i-1:i+1,j-1:j+1]*rob)

# print(result_map)
# cv2.imshow('test',result_map.astype(np.uint8))
# cv2.imwrite('rob.jpg',result_map.astype(np.uint8))
cv2.imshow('test',sobel.astype(np.uint8))
cv2.imwrite('sobel.jpg',sobel.astype(np.uint8))
cv2.waitKey(0)
cv2.destroyWindows()

The effect is as follows:
It's worth mentioning that sobel can be detected in either x direction or y direction. This picture is detected by xy. Just adjust the parameter to change 1 to 0, and the corresponding detection direction will change.

Posted by Amit Rathi on Mon, 21 Oct 2019 14:27:19 -0700