这是图像: -
我希望看起来像这样。:-
请看,在图像的第二张图片按钮被拉了。我用一个软件来做这个,他们称这个选项为20分段。我不知道如何在python中做任何人都可以帮助我吗?我正在使用python 2.7,我也是python的新手,所以请用代码示例描述清楚。我已经尝试过Opencv矢量间距来做到这一点,但是所有的时间都给我错误的图像。
答案 0 :(得分:5)
首先,您需要在图像中找到对象(数字)并将其保存在/objects
文件夹中。您可以使用cv2.findContours()
来查找轮廓,然后使用cv2.boundingRect()
方法找到边界矩形的坐标。
import numpy as np
import cv2
im = cv2.imread('old_image.png')
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
thresh = cv2.adaptiveThreshold(blur,255,1,1,11,2)
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
i=0
for cnt in contours:
[x,y,w,h] = cv2.boundingRect(cnt)
if h>60:
cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),1)
im3=im[y:y+h,x:x+w]
cv2.imwrite('objects/pix%i.png'%i,im3)
i+=1
cv2.imshow('norm',im)
cv2.imwrite('objects/shhh.jpg',im)
key = cv2.waitKey(0)
最后,您只需要连接保存的对象whit new align:
import numpy as np
import cv2
im0 = cv2.imread('objects/pix0.png',0)
im1 = cv2.imread('objects/pix1.png',0)
im2 = cv2.imread('objects/pix2.png',0)
im3 = cv2.imread('objects/pix3.png',0)
im4 = cv2.imread('objects/pix4.png',0)
im5 = cv2.imread('objects/pix5.png',0)
h0, w0 = im0.shape[:2]
h1, w1 = im1.shape[:2]
h2, w2 = im2.shape[:2]
h3, w3 = im3.shape[:2]
h4, w4 = im4.shape[:2]
h5, w5 = im5.shape[:2]
maxh=max(h0,h1,h2,h3,h4,h5)
#add 50 for space between the objects
new = np.zeros((maxh, w0+w1+w2+w3+w4+w5+50),np.uint8)
new=(255-new)
new[maxh-h0:, :w0] = im0
new[maxh-h1:, w0+10:w0+w1+10] = im1
new[maxh-h2:, w0+w1+20:w0+w1+w2+20] = im2
new[maxh-h3:, w0+w1+w2+30:w0+w1+w2+w3+30] = im3
new[maxh-h4:, w0+w1+w2+w3+40:w0+w1+w2+w3+w4+40] = im4
new[maxh-h5:, w0+w1+w2+w3+w4+50:] = im5
gray = cv2.cvtColor(new, cv2.COLOR_GRAY2BGR)
cv2.imshow('norm',gray)
cv2.imwrite('objects/new_image.jpg',gray)
key = cv2.waitKey(0)
要删除黑线,您可以对该行cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),1)
所以将其更改为#cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),1)