Python image processing seven: image spatial sharpening

1. Image edge detection

Differential operators, which can be used for edge detection and feature extraction. The filtering operation is performed through the filters module in the skimage library
.

1. Roberts operator

Roberts operator is used to detect edges,

Calling format:
edges=filters.roberts(image)
can also use Roberts' cross kernel to filter to achieve the purpose of detecting cross edges.
The function corresponding to the positive diagonal difference operator is: roberts_pos_diag(image)
The function corresponding to the negative diagonal difference operator is: roberts_neg_diag(image)

Example 1: Read in a digital image and use the Roberts operator to detect the edge of the image. The code example is as follows:

from skimage import data,filters,io 
import matplotlib.pyplot as plt 
from matplotlib.font_manager import FontProperties 
font_set=FontProperties(fname=r"c:\windows\fonts\simsun.ttc",size=12)

img=data.camera()

dst_neg=filters.roberts_neg_diag(img) 
dst_pos=filters.roberts_pos_diag(img) 
dst=filters.roberts(img)

plt.figure('filters',figsize=(8,8))

plt.subplot(221)
plt.title('原图像',fontproperties=font_set) 
plt.imshow(img,plt.cm.gray)

plt.subplot(222) 
plt.title('负对角线算子',fontproperties=font_set) 
plt.imshow(dst_neg,plt.cm.gray)

plt.subplot(223) 
plt.title('正对角线算子',fontproperties=font_set) 
plt.imshow(dst_pos,plt.cm.gray)

plt.subplot(224) 
plt.title('Robers 算子',fontproperties=font_set) 
plt.imshow(dst,plt.cm.gray)
plt.show()

output:
accc

2. Sobel operator

The calling format of the sobel operator function is:

edges=filters.sobel(image)
The function corresponding to the horizontal edge detection operator is: sobel_h(image)
The function corresponding to the vertical edge detection operator is: sobel_v(image)

Example 2: Read in a digital image, use the Sobel operator to detect the edge of the image, the code example is as follows:

from skimage import data,filters,io 
import matplotlib.pyplot as plt 
from matplotlib.font_manager import FontProperties 
font_set=FontProperties(fname=r"c:\windows\fonts\simsun.ttc",size=12)

img=data.camera()

img_h=filters.sobel_h(img) 
img_v=filters.sobel_v(img) 
img_sobel=filters.sobel(img)

plt.figure('filters',figsize=(8,8))

plt.subplot(221) 
plt.title('原图像',fontproperties=font_set) 
plt.imshow(img,plt.cm.gray)

plt.subplot(222) 
plt.title('水平边缘',fontproperties=font_set) 
plt.imshow(img_h,plt.cm.gray)

plt.subplot(223) 
plt.title('垂直边缘',fontproperties=font_set) 
plt.imshow(img_v,plt.cm.gray)

plt.subplot(224) 
plt.title('Sobel 算子',fontproperties=font_set) 
plt.imshow(img_sobel,plt.cm.gray)
plt.show()

output:
adv

3. Prewitt operator

The function is the same as sobel,

Call format:
edges=filters.prewitt(image)
The function corresponding to the horizontal edge detection operator is: prewitt_h(image)
The function corresponding to the vertical edge detection operator is: prewitt_v(image)

Example 3: Read in a digital image and use the Prewitt operator to detect the edge of the image. The code example is as follows:

from skimage import data,filters,io 
import matplotlib.pyplot as plt 
from matplotlib.font_manager import FontProperties 
font_set=FontProperties(fname=r"c:\windows\fonts\simsun.ttc",size=12)

img=data.camera()

img_h=filters.prewitt_h(img) 
img_v=filters.prewitt_v(img) 
img_prewitt=filters.prewitt(img)

plt.figure('filters',figsize=(8,8))

plt.subplot(221) 
plt.title('原图像',fontproperties=font_set) 
plt.imshow(img,plt.cm.gray)

plt.subplot(222) 
plt.title('水平边缘',fontproperties=font_set) 
plt.imshow(img_h,plt.cm.gray)

plt.subplot(223) 
plt.title('垂直边缘',fontproperties=font_set)
plt.imshow(img_v,plt.cm.gray)

plt.subplot(224) 
plt.title('Prewitt 算子',fontproperties=font_set) 
plt.imshow(img_prewitt,plt.cm.gray)
plt.show()

output:
accc

4. Laplacian operator

Function call format:
edges=filters.laplace(image)

Example 4: Read in a digital image and use the Laplacian operator to detect the edge of the image. The code example is as follows:

from skimage import data,filters,io 
import matplotlib.pyplot as plt 
from matplotlib.font_manager import FontProperties 
font_set=FontProperties(fname=r"c:\windows\fonts\simsun.ttc",size=12)

img=data.camera()

img_laplace=filters.laplace(img)
plt.figure('filters',figsize=(8,8))

plt.subplot(121) 
plt.title('原图像',fontproperties=font_set) 
plt.imshow(img,plt.cm.gray)

plt.subplot(122) 
plt.title('Laplacian 算子',fontproperties=font_set) 
plt.imshow(img_laplace,plt.cm.gray)
plt.show()

output:
afa

5. LoG operator

Example 5: Read in a digital image and use the LoG operator to detect the edge of the image. The code example is as follows:

from skimage import data,filters,io 
import matplotlib.pyplot as plt 
from matplotlib.font_manager import FontProperties 
font_set=FontProperties(fname=r"c:\windows\fonts\simsun.ttc",size=12)

img=data.camera()

img_gauss=filters.gaussian(img,sigma=2) 
img_laplace=filters.laplace(img) 
img_log=filters.laplace(img_gauss)

plt.figure('filters',figsize=(8,8))
plt.subplot(221) 
plt.title('原图像',fontproperties=font_set) 
plt.imshow(img,plt.cm.gray)

plt.subplot(222) 
plt.title('Laplacian 算子',fontproperties=font_set) 
plt.imshow(img_laplace,plt.cm.gray)

plt.subplot(223)
plt.title('高斯平滑图像',fontproperties=font_set) 
plt.imshow(img_gauss,plt.cm.gray)

plt.subplot(224) 
plt.title('LoG 算子',fontproperties=font_set) 
plt.imshow(img_log,plt.cm.gray)
plt.show()

output:
agg

6. Contrast

from skimage import data,filters,io 
import matplotlib.pyplot as plt 
from matplotlib.font_manager import FontProperties 
font_set=FontProperties(fname=r"c:\windows\fonts\simsun.ttc",size=12)

img=data.camera()

img_gauss=filters.gaussian(img,sigma=2) 
img_laplace=filters.laplace(img) 
img_log=filters.laplace(img_gauss)
img_sobel=filters.sobel(img)
img_prewitt=filters.prewitt(img)
img_Robers=filters.roberts(img)


plt.figure('filters',figsize=(14,14))
plt.subplot(321) 
plt.title('原图像',fontproperties=font_set) 
plt.imshow(img,plt.cm.gray)

plt.subplot(322) 
plt.title('Laplacian 算子',fontproperties=font_set) 
plt.imshow(img_laplace,plt.cm.gray)

plt.subplot(323)
plt.title('Prewitt 算子',fontproperties=font_set) 
plt.imshow(img_prewitt,plt.cm.gray)


plt.subplot(324) 
plt.title('LoG 算子',fontproperties=font_set) 
plt.imshow(img_log,plt.cm.gray)

plt.subplot(325) 
plt.title('Robers 算子',fontproperties=font_set) 
plt.imshow(img_Robers,plt.cm.gray)

plt.subplot(326) 
plt.title('Sobel 算子',fontproperties=font_set) 
plt.imshow(img_sobel,plt.cm.gray)


plt.show()

output:
avvv

2. Image spatial sharpening

Since the sharpened image is equal to the original image plus the accentuated edge, it is only necessary to add the original image to the detected edge image.

Example 6: Read in a digital image and use the Laplacian operator to sharpen the image. The code example is as follows:

from skimage import data,filters,io 
import matplotlib.pyplot as plt 
from matplotlib.font_manager import FontProperties 
font_set=FontProperties(fname=r"c:\windows\fonts\simsun.ttc",size=12)

img=data.camera()

img_laplace=filters.laplace(img)
img_1=img+2*img_laplace
plt.figure('filters',figsize=(8,8))
plt.subplot(131) 
plt.title('原图像',fontproperties=font_set)
plt.imshow(img,plt.cm.gray)

plt.subplot(132) 
plt.title('Laplacian 算子',fontproperties=font_set) 
plt.imshow(img_laplace,plt.cm.gray)

plt.subplot(133) 
plt.title('锐化图像',fontproperties=font_set) 
plt.imshow(img_1,plt.cm.gray)
plt.show()

output:
insert image description here

Daily Pie:
Keep your face to the sun so you don't see shadows

Guess you like

Origin blog.csdn.net/weixin_52051554/article/details/127958519