国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

python 圖片傾斜校正

這篇具有很好參考價(jià)值的文章主要介紹了python 圖片傾斜校正。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

0. 前言

進(jìn)行圖片校正是將拍照傾斜的圖片恢復(fù)水平狀態(tài),大致思路為:

  1. 用canny算子檢測出圖像中的邊緣輪廓線;
  2. 用霍夫線變換檢測出圖像中的所有直線;
  3. 篩選出接近水平方向上的直線,求出他們偏移角度的平均值;
  4. 根據(jù)傾斜角旋轉(zhuǎn)矯正;
  5. 輸出圖片。

這里設(shè)計(jì)到幾個(gè)知識點(diǎn):
canny算子
原理:數(shù)字圖像處理(20): 邊緣檢測算子(Canny算子)
cv2.Canny函數(shù):OpenCV-Python教程(8、Canny邊緣檢測)
edge = cv2.Canny(image, threshold1, threshold2[, edges[, apertureSize[, L2gradient ]]])

變量 內(nèi)容
image 要檢測的圖像

threshold1 和 threshold2 的值較小時(shí),能夠捕獲更多的邊緣信息,下文中canny_threshold(self, img_path)函數(shù)即可可視化不同threshold的效果。

霍夫變換
原理:霍夫變換——神奇的特征提取算法
cv2.HoughLines函數(shù):每天一練P9-Python和OpenCV做圖像處理(HoughLines)

其他
Python2 math.degrees() 函數(shù)
Python scipy.ndimage.rotate用法及代碼示例(該函數(shù)是按逆時(shí)針旋轉(zhuǎn))
利用向量推導(dǎo)坐標(biāo)旋轉(zhuǎn)公式(方案一)
atctan

1. 代碼

在使用代碼前,canny的閾值一定要根據(jù)實(shí)際情況修改!

import cv2
import math
import numpy as np
from scipy import ndimage


class HorizontalCorrection:
    def __init__(self):
        self.rotate_vector = np.array([0, 1])  # 圖片中地面的法線向量
        self.rotate_theta = 0  # 旋轉(zhuǎn)的角度

    def process(self, img):
        img = cv2.imread(img)  # 讀取圖片
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # 二值化
        # cv2.imshow('gray', gray)
        # cv2.waitKey()
        edges = cv2.Canny(gray, 350, 400, apertureSize=3)  # canny算子
        cv2.imwrite('./test result/edges.png', edges)
        # cv2.imshow('edges', edges)
        # cv2.waitKey()

        # 霍夫變換
        lines = cv2.HoughLines(edges, 1, np.pi / 180, 120)
        sum = 0
        count = 0
        for i in range(len(lines)):
            for rho, theta in lines[i]:
                a = np.cos(theta)
                b = np.sin(theta)
                x0 = a * rho
                y0 = b * rho
                x1 = int(x0 + 1000 * (-b))
                y1 = int(y0 + 1000 * (a))
                x2 = int(x0 - 1000 * (-b))
                y2 = int(y0 - 1000 * (a))

                if x2 != x1:
                    t = float(y2 - y1) / (x2 - x1)
                    if t <= np.pi / 5 and t >= - np.pi / 5:
                        rotate_angle = math.degrees(math.atan(t))
                        sum += rotate_angle
                        count += 1
                        cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)

        if count == 0:
            avg_rotate_angle = 0
        else:
            avg_rotate_angle = sum / count
        self.rotate_img = ndimage.rotate(img, avg_rotate_angle)  # 逆時(shí)針旋轉(zhuǎn)
        # cv2.imwrite('./test result/1.png', self.rotate_img)
        # cv2.imshow('rotate', rotate_img)
        # cv2.waitKey()

        self.rotate_theta = avg_rotate_angle  # 向量順時(shí)針旋轉(zhuǎn)公式
        self.count_rotate_vector()

    def count_rotate_vector(self):
        v1_new = (self.rotate_vector[0] * np.cos(self.rotate_theta / 180)) - \
                 (self.rotate_vector[1] * np.sin(self.rotate_theta / 180))
        v2_new = (self.rotate_vector[1] * np.cos(self.rotate_theta / 180)) + \
                 (self.rotate_vector[0] * np.sin(self.rotate_theta / 180))
        self.rotate_vector = np.array([v1_new, v2_new])

    def manual_set_rotate_vector(self, rotate_theta):
        self.rotate_theta = rotate_theta
        self.count_rotate_vector()

    def canny_threshold(self, img_path):
        img_original = cv2.imread(img_path)
        #設(shè)置窗口
        cv2.namedWindow('Canny')
        #定義回調(diào)函數(shù)
        def nothing(x):
            pass
        #創(chuàng)建兩個(gè)滑動(dòng)條,分別控制threshold1,threshold2
        cv2.createTrackbar('threshold1','Canny',50,400,nothing)
        cv2.createTrackbar('threshold2','Canny',100,400,nothing)
        while(1):
            #返回滑動(dòng)條所在位置的值
            threshold1=cv2.getTrackbarPos('threshold1','Canny')
            threshold2=cv2.getTrackbarPos('threshold2','Canny')
            #Canny邊緣檢測
            img_edges=cv2.Canny(img_original,threshold1,threshold2)
            #顯示圖片
            cv2.imshow('original',img_original)
            cv2.imshow('Canny',img_edges)
            if cv2.waitKey(1)==ord('q'):
                break
        cv2.destroyAllWindows()


if __name__ == '__main__':
    horizontal_correction = HorizontalCorrection()
    # horizontal_correction.canny_threshold(r'./test image/IMG_6386.JPG')
    horizontal_correction.process(r'./test image/IMG_6386.JPG')
    print(horizontal_correction.rotate_theta)
    cv2.imwrite('./test result/1.png', horizontal_correction.rotate_img)
    cv2.imshow('rotate', horizontal_correction.rotate_img)
    cv2.waitKey()

2. 效果圖

python 圖片傾斜校正

python 圖片傾斜校正

從圖中可以看出霍夫變換根據(jù)欄桿的水平線進(jìn)行校正。
彩蛋:乾元的朋友,讓我看見你們的雙手。

補(bǔ)充——用滑動(dòng)條調(diào)整canny閾值

之前在一個(gè)博客看到的,但是現(xiàn)在找不到了,先把代碼放上。

    def canny_threshold(self, img_path):
        img_original = cv2.imread(img_path)
        # 設(shè)置窗口
        cv2.namedWindow('Canny')
        # 定義回調(diào)函數(shù)
        def nothing(x):
            pass
        # 創(chuàng)建兩個(gè)滑動(dòng)條,分別控制threshold1,threshold2
        cv2.createTrackbar('threshold1', 'Canny', 50, 400, nothing)
        cv2.createTrackbar('threshold2', 'Canny', 100, 400, nothing)
        while True:
            # 返回滑動(dòng)條所在位置的值
            threshold1 = cv2.getTrackbarPos('threshold1', 'Canny')
            threshold2 = cv2.getTrackbarPos('threshold2', 'Canny')
            # Canny邊緣檢測
            img_edges = cv2.Canny(img_original, threshold1, threshold2)
            # 顯示圖片
            cv2.imshow('original', img_original)
            cv2.imshow('Canny', img_edges)
            if cv2.waitKey(1) == ord('q'):
                break
        cv2.destroyAllWindows()

其他

cv2.HoughLines返回值的處理方式進(jìn)行了修改。文章來源地址http://www.zghlxwxcb.cn/news/detail-443992.html

import cv2
import math
import numpy as np
from scipy import ndimage


class HorizontalCorrection:
    def __init__(self):
        self.rotate_vector = np.array([0, 1])  # 圖片中地面的法線向量
        self.rotate_theta = 0  # 旋轉(zhuǎn)的角度

    def process(self, img):
        img = cv2.imread(img)  # 讀取圖片
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # 二值化
        # cv2.imshow('gray', gray)
        # cv2.waitKey()
        edges = cv2.Canny(gray, 350, 400, apertureSize=3)  # canny算子
        cv2.imwrite('./test result/edges.png', edges)
        # cv2.imshow('edges', edges)
        # cv2.waitKey()

        # 霍夫變換
        lines = cv2.HoughLines(edges, 1, np.pi / 180, 120)
        sum = 0
        count = 0
        for r_theta in lines:
            arr = np.array(r_theta[0], dtype=np.float64)
            rho, theta = arr
            a = np.cos(theta)
            b = np.sin(theta)
            x0 = a * rho
            y0 = b * rho
            x1 = int(x0 + 1000 * (-b))
            y1 = int(y0 + 1000 * (a))
            x2 = int(x0 - 1000 * (-b))
            y2 = int(y0 - 1000 * (a))

            if x2 != x1:
                t = float(y2 - y1) / (x2 - x1)
                if t <= np.pi / 5 and t >= - np.pi / 5:
                    rotate_angle = math.degrees(math.atan(t))
                    sum += rotate_angle
                    count += 1
                    cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)

        if count == 0:
            avg_rotate_angle = 0
        else:
            avg_rotate_angle = sum / count
        self.rotate_img = ndimage.rotate(img, avg_rotate_angle)  # 逆時(shí)針旋轉(zhuǎn)
        # cv2.imwrite('./test result/1.png', self.rotate_img)
        # cv2.imshow('rotate', rotate_img)
        # cv2.waitKey()

        self.rotate_theta = avg_rotate_angle  # 向量順時(shí)針旋轉(zhuǎn)公式
        self.count_rotate_vector()

    def count_rotate_vector(self):
        v1_new = (self.rotate_vector[0] * np.cos(self.rotate_theta / 180)) - \
                 (self.rotate_vector[1] * np.sin(self.rotate_theta / 180))
        v2_new = (self.rotate_vector[1] * np.cos(self.rotate_theta / 180)) + \
                 (self.rotate_vector[0] * np.sin(self.rotate_theta / 180))
        self.rotate_vector = np.array([v1_new, v2_new])

    def manual_set_rotate_vector(self, rotate_theta):
        self.rotate_theta = rotate_theta
        self.count_rotate_vector()

    def canny_threshold(self, img_path):
        img_original = cv2.imread(img_path)
        #設(shè)置窗口
        cv2.namedWindow('Canny')
        #定義回調(diào)函數(shù)
        def nothing(x):
            pass
        #創(chuàng)建兩個(gè)滑動(dòng)條,分別控制threshold1,threshold2
        cv2.createTrackbar('threshold1','Canny',50,400,nothing)
        cv2.createTrackbar('threshold2','Canny',100,400,nothing)
        while(1):
            #返回滑動(dòng)條所在位置的值
            threshold1=cv2.getTrackbarPos('threshold1','Canny')
            threshold2=cv2.getTrackbarPos('threshold2','Canny')
            #Canny邊緣檢測
            img_edges=cv2.Canny(img_original,threshold1,threshold2)
            #顯示圖片
            cv2.imshow('original',img_original)
            cv2.imshow('Canny',img_edges)
            if cv2.waitKey(1)==ord('q'):
                break
        cv2.destroyAllWindows()


if __name__ == '__main__':
    horizontal_correction = HorizontalCorrection()
    # horizontal_correction.canny_threshold(r'./test image/IMG_6386.JPG')
    horizontal_correction.process(r'./test image/IMG_6386.JPG')
    print(horizontal_correction.rotate_theta)
    cv2.imwrite('./test result/1.png', horizontal_correction.rotate_img)
    cv2.imshow('rotate', horizontal_correction.rotate_img)
    cv2.waitKey()

到了這里,關(guān)于python 圖片傾斜校正的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • MATLAB圖像傾斜校正算法實(shí)現(xiàn):圖像傾斜角檢測及校正

    MATLAB圖像傾斜校正算法實(shí)現(xiàn):圖像傾斜角檢測及校正

    在本文中,隨著多媒體技術(shù)的不斷發(fā)展,數(shù)碼相機(jī),高清拍照手機(jī)等多媒體設(shè)備己經(jīng)在人們的生活中占據(jù)了越來越重要的地位 ( 點(diǎn)擊文末“閱讀原文”獲取完整 代碼數(shù)據(jù) ) 。 通過采用圖像處理技術(shù),可以將數(shù)碼設(shè)備采 集到的文字、圖片等信息轉(zhuǎn)化成其他信息形勢輸出,例

    2024年02月04日
    瀏覽(38)
  • 基于深度學(xué)習(xí)的指針式儀表傾斜校正方法——論文解讀

    基于深度學(xué)習(xí)的指針式儀表傾斜校正方法——論文解讀

    中文論文題目:基于深度學(xué)習(xí)的指針式儀表傾斜校正方法 英文論文題目:Tilt Correction Method of Pointer Meter Based on Deep Learning 周登科、楊穎、朱杰、王庫.基于深度學(xué)習(xí)的指針式儀表傾斜校正方法[J].計(jì)算機(jī)輔助設(shè)計(jì)與圖形學(xué)學(xué)報(bào), 2020, 32(12):9.DOI:10.3724/SP.J.1089.2020.18288. ? ? ? ?針對儀

    2024年02月12日
    瀏覽(26)
  • 計(jì)算機(jī)視覺——基于傅里葉幅度譜文檔傾斜度檢測與校正

    計(jì)算機(jī)視覺——基于傅里葉幅度譜文檔傾斜度檢測與校正

    在計(jì)算機(jī)視覺領(lǐng)域,處理文檔數(shù)據(jù)時(shí),OCR算法的性能往往會(huì)受到文檔的傾斜度影響。如果文檔在輸入到模型之前沒有經(jīng)過恰當(dāng)?shù)男UP途蜔o法期待模型能夠提供準(zhǔn)確的預(yù)測結(jié)果,或者模型預(yù)測的精度會(huì)降低。例如,在信息提取系統(tǒng)中,如果向OCR模型提供了傾斜的圖像,模

    2024年04月10日
    瀏覽(16)
  • opencv進(jìn)行雙目標(biāo)定以及極線校正 python代碼

    opencv進(jìn)行雙目標(biāo)定以及極線校正 python代碼

    參考博客 OpenCV相機(jī)標(biāo)定全過程 [OpenCV實(shí)戰(zhàn)]38 基于OpenCV的相機(jī)標(biāo)定 opencv立體標(biāo)定函數(shù) stereoCalibrate() 將打印的結(jié)果保存到標(biāo)定文件中即可 參考博客 機(jī)器視覺學(xué)習(xí)筆記(8)——基于OpenCV的Bouguet立體校正 小白視角之Bouguet雙目立體校正原理 校正前 左圖 右圖 校正后

    2024年02月11日
    瀏覽(27)
  • 使用Python進(jìn)行立體幾何和立體校正的綜合教程

    使用Python進(jìn)行立體幾何和立體校正的綜合教程

    需要多個(gè)視圖 在相機(jī)的針孔模型中,光線從物體上反射出來,照射到膠片上形成圖像。因此,沿著同一條光線的所有點(diǎn)都將對應(yīng)于圖像中的一個(gè)點(diǎn)。 因此,給定圖像中的一個(gè)點(diǎn),不可能確定其在世界上的準(zhǔn)確位置,也就是說,我們無法從單個(gè)圖像中恢復(fù)深度。 我們也無法從

    2024年02月09日
    瀏覽(12)
  • OpenCV圖片校正

    OpenCV圖片校正

    遇到偏的圖片想要校正成水平或者垂直的。 對于傾斜的圖片通過矯正可以得到水平的圖片。一般有如下幾種基于opencv的組合方式進(jìn)行圖片矯正。 1、傅里葉變換 + 霍夫變換+ 直線 + 角度 + 旋轉(zhuǎn) 2、邊緣檢測 + 霍夫變換 + 直線+角度 + 旋轉(zhuǎn) 3、四點(diǎn)透視 + 角度 + 旋轉(zhuǎn) 4、檢測矩形輪

    2024年02月12日
    瀏覽(12)
  • opencv對相機(jī)進(jìn)行畸變校正,及校正前后的坐標(biāo)對應(yīng)

    opencv對相機(jī)進(jìn)行畸變校正,及校正前后的坐標(biāo)對應(yīng)

    目前有個(gè)項(xiàng)目,需要用到熱成像相機(jī)。但是這個(gè)熱成像相機(jī)它的畸變比較厲害,因此需要用標(biāo)定板進(jìn)行標(biāo)定,從而消除鏡頭畸變。 同時(shí)需要實(shí)現(xiàn)用戶用鼠標(biāo)點(diǎn)擊校正后的畫面后,顯示用戶點(diǎn)擊位置的像素所代表的溫度。 另外熱成像sdk中還有個(gè)功能:選定一個(gè)rect,可以返回這

    2024年02月14日
    瀏覽(20)
  • mysql數(shù)據(jù)庫自動(dòng)生成默認(rèn)時(shí)間不正確進(jìn)行校正

    引言 查看數(shù)據(jù)庫數(shù)據(jù),發(fā)現(xiàn)表中自動(dòng)生成的創(chuàng)建時(shí)間不正確,故先使用 SELECT CURRENT_TIMESTAMP; sql驗(yàn)證自動(dòng)生成的時(shí)間是否是不正確的。經(jīng)驗(yàn)證確定是自動(dòng)生成的時(shí)間不正確,進(jìn)而想到了對時(shí)間進(jìn)行校正。 校正過程 首先查看數(shù)據(jù)庫時(shí)間的時(shí)區(qū)是否是當(dāng)?shù)氐摹?SELECT @@global.time_zo

    2024年02月20日
    瀏覽(28)
  • 使用python對圖片進(jìn)行壓縮

    使用python對圖片進(jìn)行壓縮

    對于圖片,強(qiáng)行被定義高和寬會(huì)變形,我們希望圖片被改變大小后,比例保持不變,完成對圖片的壓縮。 1.pillow : pip install pillow -i https://pypi.douban.com/simple 2. os : 連接文件,并獲取文件夾下的文件名 獲取picture文件下的相關(guān)圖片的路徑,將路徑放到列表里面進(jìn)行存儲 使用Image模

    2024年02月13日
    瀏覽(21)
  • Python 對圖片進(jìn)行顏色識別

    Python 對圖片進(jìn)行顏色識別

    場景:在進(jìn)行壓力測試時(shí),需要判斷圖片的某一塊區(qū)域是否是黑色 這里使用的是OpenCV庫對圖片進(jìn)行顏色的識別,幾乎可以識別所有常見的顏色 直接上代碼 運(yùn)行結(jié)果如下: ? 顏色可以判斷出來了,可以做的事情就方便很多了 比如在盡行壓力測試時(shí),去判斷截圖區(qū)域是否是黑

    2024年02月11日
    瀏覽(19)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包