Python + OpenCV achieve image edge detection operator SOBEL, ROBERT

 A Python image edge detection operator SOBEL, ROBERT, the process to achieve image processing out certain functions may be a bit rough on Jupyter Notebook. Install on opencv libraries can refer to: Python installation under opencv libraries and some of the issues summarized .

 

1. Implement the code

import cv2
import numpy as np
import matplotlib.pyplot as plt

# Implement anti-color picture function 
DEF PointInvert (img):
    height, width, _ = img.shape
    for i in range(height):
        for j in range(width):
            pi = img[i, j]
            img[i, j] = 255 - pi
return img

# Read the original gray-scale images 
src_s = cv2.imread ( " dip_switch_02.bmp " )
cv2.imshow ( " src_s " , src_s) # original picture named "src_s" displayed 
# picture anti-color 
src = PointInvert (src_s)
cv2.imshow ( " src " , src) # picture src_s after being color-inverted named "src" and displayed

#SOBEL算子
sobel = cv2.Sobel(src,cv2.CV_64F, 1, 1, ksize=7)
cv2.imshow ( " the Sobel " , the Sobel) # after the SOBEL operator processing image src named "sobel" and displayed 
# picture anti-color 
sobel2 = PointInvert (the Sobel)
cv2.imshow ( " sobel2 " , sobel2) # after the anti-color process sobel picture named "sobel2" and displayed

# ROBERT operator 
DEF Robert (img):
    h,w,_ = img.shape
    rob = [[-1,-1],[1,1]]
    for x in range(h):
        for y in range(w):
            if (y + 2 <= w) and (x + 2 <= h):
                imgChild = img[x:x+2, y:y+2, 1]
                list_robert = rob*imgChild
                IMG [X, Y] = ABS (list_robert.sum ()) # sum plus the absolute value of 
    return IMG
robert = Robert(src)
cv2.imshow ( " Robert " , Robert) # after the operator with pictures ROBERT src named "robert" and displayed

# Pictures reverse color 
robert2 = PointInvert (Robert)
cv2.imshow ( " robert2 " , robert2) # named after the anti-color pictures robert treated as "robert2" and displayed 
cv2.waitKey (0)

 

2. Run results

 

 

 

 

 

 

 

 

 

 

 

3. The problems and solutions encountered

(1) run-time: ValueError: too many values ​​to unpack (expect 2)

Error Code: H, W = img.shape

Error reason:

First understand img.shape [0], [1], [2].

img.shape [0]: the vertical size of the image (height)

img.shape [1]: image horizontal size (width)

img.shape [2]: number of channels of the image

In the matrix, [0] indicates the number of lines on, [1] indicates the number of columns.

由此可知,img.shape默认返回值个数为3个,而我没有指明缺失的参数。

解决方法:

此处未用到的参数为图像的通道数,使用_代表用不到的参数,将代码h,w = img.shape改为h,w,_ = img.shape。

 

(2) 运行时出现:ValueError: operands could not be broadcast together with shapes (2,2) (2,2,3)

出错代码:list_robert = rob*imgChild

出错原因:

经过网上搜索解决方法,发现是违反了ufunc的广播机制。广播机制如下:

当使用ufunc函数对两个数组进行计算时,ufunc函数会对这两个数组的对应元素进行计算,因此它要求这两个数组有相同的大小(shape相同)。如果两个数组的shape不同的话,会进行如下的广播(broadcasting)处理:

1)让所有输入数组都向其中shape最长的数组看齐,shape中不足的部分都通过在前面加1补齐。

2)输出数组的shape是输入数组shape的各个轴上的最大值。

3)如果输入数组的某个轴和输出数组的对应轴的长度相同或者其长度为1时,这个数组能够用来计算,否则出错。

4)当输入数组的某个轴的长度为1时,沿着此轴运算时都用此轴上的第一组值。

我们图片的shape是(h,w,c),而列表的shape是(x:x+2, y:y+2),根据规则1可知列表的shape拓展应为(x:x+2, y:y+2,1);由规则2可知输出数组的shape应为(h,w,c),列表的shape不满足规则3,所以出错。

解决方法:

将代码imgChild = img[x:x+2, y:y+2]改为imgChild = img[x:x+2, y:y+2, 1]。

 

Guess you like

Origin www.cnblogs.com/BIXIABUMO/p/12639418.html