How realign segments of image in python? -
this image like:-
i want this.:-
please look, in second picture button of image laved up. use software called option segment 20. don't have idea ho in python can please me ? using python 2.7 , new python please describe clear code example. have tried opencv vector spacing time give me wrong image.
first off, need find objects (numbers) within image , save them in /objects
folder. can using cv2.findcontours()
in order find contours , bounding rectangle's coordinates cv2.boundingrect()
method.
import numpy 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 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)
result:
finally, need concatenate saved objects whit new align:
import numpy 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 space between 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)
result :
for remove black lines can comment line cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),1)
so change #cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),1)
Comments
Post a Comment