import cv2 
img = cv2.imread("D:/photo/123456.jpg")
cv2.imshow("img",img)
cv2.waitKey(0)
#灰度图
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow("gray",gray)
cv2.waitKey(0)
#二值化
ret,im_fixed=cv2.threshold(gray,50,100,cv2.THRESH_BINARY)
cv2.imshow("im_fixed",im_fixed)
cv2.waitKey(0)
#反色
img2 = gray.copy()
cv2.threshold(gray,80,255,0,gray)
for i in range(0,gray.shape[0]):
    for j in range(0,gray.shape[1]):
        img2[i,j] = 255-gray[i,j]
cv2.imshow("img2",img2)
cv2.waitKey(0)
#水平投影
img3 = img2.copy() 
#返回图像的高和宽
(h,w)=img3.shape
#初始化一个跟图像高一样长度的数组,用于记录每一行的黑点个数
a=[0 for z in range(0,h)]
for i in range(0,h):          #遍历每一行
    for j in range(0,w):      #遍历每一列
        if img3[i,j]==0:      #判断该点是否为黑点,0代表黑点
            a[i]+=1           #该行的计数器加一
            img3[i,j]=255     #将其改为白点,即等于255
for i in range(0,h):          #遍历每一行
    for j in range(0,a[i]):   #从该行应该变黑的最左边的点开始向最右边的点设置黑点
        img3[i,j]=0           #设置黑点
cv2.imshow("shuiping",img3)
cv2.waitKey(0)
img4 = img2.copy()
#垂直投影
#返回图像的高和宽
(h,w)=img4.shape
#初始化一个跟图像宽一样长度的数组,用于记录每一列的黑点个数
a =[0 for z in range(0,w)]
for i in range(0,w):           #遍历每一列  
    for j in range(0,h):       #遍历每一行
        if img4[j,i]==0:       #判断该点是否为黑点,0代表是黑点
            a[i]+=1            #该列的计数器加1
            img4[j,i]=255      #记录完后将其变为白色,即等于255
for i in range(0,w):           #遍历每一列
    for j in range(h-a[i],h):  #从该列应该变黑的最顶部的开始向最底部设为黑点
        img4[j,i]=0            #设为黑点
cv2.imshow("chuizhi",img4)
cv2.waitKey(0)
#画矩形框
image = img2.copy()

cv2.rectangle(image, (10,10), (65,120),(0,0,255),1)#设置矩形的各参数依次是:图片,添加的文字,左上角坐标(整数),字体,字体大小,颜色,字体粗细
cv2.rectangle(image, (75,10), (135,120),(0,0,255), 1)
cv2.rectangle(image, (160,10), (220,120),(0,0,255), 1)
cv2.rectangle(image, (225,10), (290,120),(0,0,255), 1)
cv2.rectangle(image, (300,10), (355,120),(0,0,255), 1)
cv2.rectangle(image, (360,10), (415,120),(0,0,255), 1)
cv2.rectangle(image, (420,10), (480,120),(0,0,255), 1)
cv2.imshow('image',image)
cv2.waitKey(0)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

import cv2
 
# 1、读取图像,并把图像转换为灰度图像并显示
img = cv2.imread("D:/photo/123456.jpg")  # 读取图片
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)   # 转换了灰度化
cv2.imshow('gray', img_gray)  # 显示图片
cv2.waitKey(0)
 
# 2、将灰度图像二值化,设定阈值是100
img_thre = img_gray
cv2.threshold(img_gray, 100, 255, cv2.THRESH_BINARY_INV, img_thre)
cv2.imshow('threshold', img_thre)
cv2.waitKey(0)
 
# 3、保存黑白图片
cv2.imwrite('thre_res.png', img_thre)
 
# 4、分割字符
white = []  # 记录每一列的白色像素总和
black = []  # ..........黑色.......
height = img_thre.shape[0]
width = img_thre.shape[1]
white_max = 0
black_max = 0
# 计算每一列的黑白色像素总和
for i in range(width):
    s = 0  # 这一列白色总数
    t = 0  # 这一列黑色总数
    for j in range(height):
        if img_thre[j][i] == 255:
            s += 1
        if img_thre[j][i] == 0:
            t += 1
    white_max = max(white_max, s)
    black_max = max(black_max, t)
    white.append(s)
    black.append(t)
    print(s)
    print(t)
 
arg = False  # False表示白底黑字;True表示黑底白字
if black_max > white_max:
    arg = True
 
# 分割图像
def find_end(start_):
    end_ = start_+1
    for m in range(start_+1, width-1):
        if (black[m] if arg else white[m]) > (0.95 * black_max if arg else 0.95 * white_max):  # 0.95这个参数请多调整,对应下面的0.05
            end_ = m
            break
    return end_
 
n = 1
start = 1
end = 2
while n < width-2:
    n += 1
    if (white[n] if arg else black[n]) > (0.05 * white_max if arg else 0.05 * black_max):
        # 上面这些判断用来辨别是白底黑字还是黑底白字
        # 0.05这个参数请多调整,对应上面的0.95
        start = n
        end = find_end(start)
        n = end
        if end-start > 5:
            cj = img_thre[1:height, start:end]
            cv2.imshow('caijian', cj)
            cv2.waitKey(0)

结果截图:

 

       

 

 posted on 2020-03-22 13:29  ❤sndd❤  阅读(351)  评论(0编辑  收藏  举报