python图像倾斜校正_对图像中的单个字符进行倾斜校正

# -*- coding:utf-8 -*-importcv2,numpyasnp,time

img_roi=[48,191,980,656]# x1, y1, x2, y2src_img_dir="images/source/9.png"bg_img=cv2.imread("images/background.png",cv2.IMREAD_COLOR)[img_roi[1]:img_roi[3],img_roi[0]:img_roi[2]]# The background of the area is constant. So I have used a reference background image and removed pixels which have a similar H value as the backgroundbg_hsv=cv2.cvtColor(bg_img,cv2.COLOR_BGR2HSV)src_img=cv2.imread(src_img_dir,cv2.IMREAD_COLOR)[img_roi[1]:img_roi[3],img_roi[0]:img_roi[2]]# This image is the image where letters are placed on top of the background imagesrc_hsv=cv2.cvtColor(src_img,cv2.COLOR_BGR2HSV)mask=np.zeros([src_img.shape[0],src_img.shape[1],3],dtype=np.uint8)offset=3start_time=time.time()foryinrange(src_img.shape[0]):forxinrange(src_img.shape[1]):sp=src_hsv[y][x]bp=bg_hsv[y][x]ifbp[0]-offset<=sp[0]<=bp[0]+offset:ifsp[1]>=109:mask[y][x]=src_img[y][x]elifsp[1]<=90:ifsp[0]>=67:mask[y][x]=src_img[y][x]elifsp[2]>=125andsp[1]>=20:mask[y][x]=src_img[y][x]else:mask[y][x]=src_img[y][x]"""if sp[1] >= 60 and sp[2] >= 60:

mask[y][x] = src_img[y][x]

#mask[y][x] = conv"""print("duration",time.time()-start_time)mask=cv2.cvtColor(mask,cv2.COLOR_BGR2HSV)#mask[:,:,2] = 255mask=cv2.cvtColor(mask,cv2.COLOR_HSV2BGR)mask_gray=cv2.cvtColor(mask,cv2.COLOR_BGR2GRAY)ret,thresh=cv2.threshold(mask_gray,0,255,cv2.THRESH_BINARY|cv2.THRESH_OTSU)opened=cv2.morphologyEx(thresh,cv2.MORPH_OPEN,(3,3))opened=cv2.morphologyEx(opened,cv2.MORPH_OPEN,(3,3))opened=cv2.erode(opened,(3,3))opened=cv2.dilate(opened,(3,3))opened=cv2.dilate(opened,(5,5))opened=cv2.morphologyEx(opened,cv2.MORPH_CLOSE,(3,3))opened=cv2.erode(opened,(3,3))opened=cv2.erode(opened,(3,3))final_img=opened#edges = cv2.Canny(final_img, 0, 255)lines=cv2.HoughLinesP(final_img,1,np.pi/180,20,minLineLength=10,maxLineGap=3)forlineinlines:coords=line[0]cv2.line(src_img,(coords[0],coords[1]),(coords[2],coords[3]),[255,255,255],2)#cv2.imshow("can", edges)#cv2.drawContours(src_img, fixed_contours, -1, (0,255,0), 2)cv2.imshow("src",src_img)cv2.imshow("",final_img)cv2.waitKey(0)cv2.destroyAllWindows()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值