首页 > 编程语言 >OpenCV—python 基于傅里叶变换的图像矫正

OpenCV—python 基于傅里叶变换的图像矫正

时间:2023-02-09 16:12:06浏览次数:40  
标签:angle python print cv2 OpenCV thresh np theta 傅里叶

  1. 基于傅里叶变换的图像矫正

  2. import cv2
    import numpy as np
    import math
    
    def fourier_demo():
        #1、灰度化读取文件,
        img = cv2.imread('english_rotation.jpg',0)
    
        #2、图像延扩
        h, w = img.shape[:2]
        new_h = cv2.getOptimalDFTSize(h)
        new_w = cv2.getOptimalDFTSize(w)
        right = new_w - w
        bottom = new_h - h
        nimg = cv2.copyMakeBorder(img, 0, bottom, 0, right, borderType=cv2.BORDER_CONSTANT, value=0)
        cv2.imshow('new image', nimg)
    
        #3、执行傅里叶变换,并过得频域图像
        f = np.fft.fft2(nimg)
        fshift = np.fft.fftshift(f)
        magnitude = np.log(np.abs(fshift))
    
    
        #二值化
        magnitude_uint = magnitude.astype(np.uint8)
        ret, thresh = cv2.threshold(magnitude_uint, 11, 255, cv2.THRESH_BINARY)
        print(ret)
    
        cv2.imshow('thresh', thresh)
        print(thresh.dtype)
        #霍夫直线变换
        lines = cv2.HoughLinesP(thresh, 2, np.pi/180, 30, minLineLength=40, maxLineGap=100)
        print(len(lines))
    
        #创建一个新图像,标注直线
        lineimg = np.ones(nimg.shape,dtype=np.uint8)
        lineimg = lineimg * 255
    
        piThresh = np.pi/180
        pi2 = np.pi/2
        print(piThresh)
    
        for line in lines:
            x1, y1, x2, y2 = line[0]
            cv2.line(lineimg, (x1, y1), (x2, y2), (0, 255, 0), 2)
            if x2 - x1 == 0:
                continue
            else:
                theta = (y2 - y1) / (x2 - x1)
            if abs(theta) < piThresh or abs(theta - pi2) < piThresh:
                continue
            else:
                print(theta)
    
        angle = math.atan(theta)
        print(angle)
        angle = angle * (180 / np.pi)
        print(angle)
        angle = (angle - 90)/(w/h)
        print(angle)
    
        center = (w//2, h//2)
        M = cv2.getRotationMatrix2D(center, angle, 1.0)
        rotated = cv2.warpAffine(img, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
        cv2.imshow('line image', lineimg)
        cv2.imshow('rotated', rotated)
    
    fourier_demo()
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

     

  3. 结果图像

     

标签:angle,python,print,cv2,OpenCV,thresh,np,theta,傅里叶
From: https://www.cnblogs.com/zhml/p/17105627.html

相关文章

  • Centos7 Python2.7升级3.6.9之靠谱教程
    Python2.7现在已经正式被放弃了,运行会报错,所以得玩Python3版本了;这里分享Centos7系统下将Python2.7升级3.6.9的方法,非常靠谱本人刚刚测试通过,看了老外的教程;简单三......
  • 用c编译 php/python拓展(swig生成PHP扩展)
    https://blog.csdn.net/cuxiong8996/article/details/107153840 swig生成了PHP的扩展:https://blog.csdn.net/weixin_39622419/article/details/1160659781、安装swig......
  • 初学opencv
    #opencv学习importcv2importmatplotlib#matplotlib.use('agg')importmatplotlib.pyplotaspltimportnumpyasnpimg=cv2.imread('hblgos.jpg',cv2.IMREAD_GRAYS......
  • opencv读取视频文件
    #opencv读取视频文件importcv2importmatplotlib.pyplotaspltvc=cv2.VideoCapture('test.mp4')#检测视频打开是否正确ifvc.isOpened():oepn,frame=vc.read()......
  • Python面试_可迭代对象&迭代器&生成器
    #可迭代对象&迭代器>**可迭代对象**:使用内置的iter()函数可以获取迭代器的对象。如果对象实现了能返回迭代器的__iter__方法,那么对象就是是可迭代的。序列都可以迭代;实......
  • CentOS安装python3
    一、安转相关依赖使用yuminstallgccpatchlibffi-develpython-develzlib-develbzip2-developenssl-develncurses-develsqlite-develreadline-develtk-devel......
  • python之路67 drf从入门到成神 8 接口文档、jwt介绍和原理、drf-jwt快速使用、定制返
    接口文档前后端分离我们做后端,写接口前端做前端,根据接口写app、pc、小程序作为后端来讲、我们很清楚,比如登录接口/api/v1/login/----》post-------》us......
  • 凑个小热闹:python采集《狂飙》评论
    2023年首部爆款剧集《狂飙》一度冲上热搜第一,害的我两倍速熬夜看完了。  “是非面前稍不留神,就会步入万丈深渊,唯有坚守信仰,才能守得初心”  面对这么多广大网......
  • python数据抓取,抓点星星网的内容
    代码:#coding=utf-8importos,sys,reimportrequestsfromwebob.excimportstrip_tagsfromxpinyinimportPinyindefstr2dict(str):dict={}groups1......
  • Python-celery介绍与快速上手
    1.celery介绍:  celery是一个基于Python开发的模块,可以帮助我们在开发过程中,对任务进行分发和处理。            详细介绍取自:Python之celery的简介与使......