天天看點

基于opencv答題卡識别基本處理_11.讀取圖檔2.圖檔預處理3. 添加邊框

文章目錄

  • 1.讀取圖檔
  • 2.圖檔預處理
    • 2.1 原圖轉換為灰階圖
    • 2.2 高斯濾波處理,去除噪聲點
    • 2.3 增強亮度
    • 2.4 自适應二值化
    • 2.5 圖檔可視化
  • 3. 添加邊框
    • 3.1 使用copyMakeBorder添加邊框
    • 3.2 可視化圖檔檢視效果
    • 3.3 手動截取答題卡區域

1.讀取圖檔

img = cv2.imread('images/5.png')
           

2.圖檔預處理

2.1 原圖轉換為灰階圖

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # COLOR_BGR2RGB
           

2.2 高斯濾波處理,去除噪聲點

blurred = cv2.GaussianBlur(gray, (3,3), 0)
           

作用:去除圖檔噪聲點,圖像預處理一般常用。

GaussianBlur函數:

void GaussianBlur(InputArray src, OutputArray dst, Size ksize, double sigmaX, double sigmaY=0, int borderType=BORDER_DEFAULT )

InputArray src:輸入的圖像

OutputArray dst:輸出圖像

Size ksize:高斯卷積核的大小,是奇數,可以是(3,3),(5,5),(9,9)

double sigmaX, double sigmaY=0, :表示x和y方向的方差,如果y=0則y方向的方差與x相等

int borderType=BORDER_DEFAULT :邊界的處理方式,一般預設

2.3 增強亮度

#增強亮度
def imgBrightness(img1, c, b): 
    rows, cols= img1.shape
    blank = np.zeros([rows, cols], img1.dtype)
    # addWeighted 實作兩副相同大小的圖像融合相加
    rst = cv2.addWeighted(img1, c, blank, 1-c, b)
    return rst
           
blurred = imgBrightness(blurred, 1.5, 3)
           

2.4 自适應二值化

blurred = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 51, 2)
           

2.5 圖檔可視化

# 顯示原來的和縮放後的圖像
# Create a figure
fig = plt.figure(figsize=(16, 12))

# Subplot for original image
a=fig.add_subplot(2,3,1)
imgplot = plt.imshow(img)
a.set_title('原始圖檔')

# Subplot for resized image
a=fig.add_subplot(2,3,2)
imgplot = plt.imshow(gray, cmap='gray')
a.set_title('灰階圖')

# Subplot for resized image
a=fig.add_subplot(2,3,3)
imgplot = plt.imshow(blurred, cmap='gray')
a.set_title('高斯濾波')

# Subplot for resized image
a=fig.add_subplot(2,3,4)
imgplot = plt.imshow(blurred, cmap='gray')
a.set_title('增強亮度')

# Subplot for resized image
a=fig.add_subplot(2,3,5)
imgplot = plt.imshow(blurred, cmap='gray')
a.set_title('自适應二值化')

plt.show()
           
基于opencv答題卡識别基本處理_11.讀取圖檔2.圖檔預處理3. 添加邊框

3. 添加邊框

3.1 使用copyMakeBorder添加邊框

blurred = cv2.copyMakeBorder(blurred, 5, 5, 5, 5, cv2.BORDER_CONSTANT, value=(255,255,255))
           

“”"

為圖像設定邊框(填充)

如果要在圖像周圍建立邊框(如相框),則可以使用cv.copyMakeBorder()。

但是它在卷積運算,零填充等方面有更多應用。此函數采用以下參數:

cv2.copyMakeBorder()

參數:

img:圖像對象

top,bottom,left,right: 上下左右邊界寬度,機關為像素值

borderType:

cv2.BORDER_CONSTANT, 帶顔色的邊界,需要傳入另外一個顔色值

cv2.BORDER_REFLECT, 邊緣元素的鏡像反射做為邊界

cv2.BORDER_REFLECT_101/cv2.BORDER_DEFAULT

cv2.BORDER_REPLICATE, 邊緣元素的複制做為邊界

CV2.BORDER_WRAP

value: borderType為cv2.BORDER_CONSTANT時,傳入的邊界顔色值,如[0,255,0]

“”"

3.2 可視化圖檔檢視效果

fig = plt.figure(figsize=(16, 12))
plt.imshow(blurred,cmap='gray')
           
基于opencv答題卡識别基本處理_11.讀取圖檔2.圖檔預處理3. 添加邊框

3.3 手動截取答題卡區域

blurred_max = cv2.copyMakeBorder(blurred.copy(), 5, 5, 280, 280, cv2.BORDER_CONSTANT, value=(255,255,255))
fig = plt.figure(figsize=(8, 8))
plt.imshow(blurred_max, cmap='gray')
           
基于opencv答題卡識别基本處理_11.讀取圖檔2.圖檔預處理3. 添加邊框
fig = plt.figure(figsize=(8, 8))
plt.imshow(blurred[160:600, 560:1000], cmap='gray')
           
基于opencv答題卡識别基本處理_11.讀取圖檔2.圖檔預處理3. 添加邊框

繼續閱讀