python opencv的簡單課程設計

2021-10-05 12:36:30 字數 2507 閱讀 2192

從網上找了很多關於處理的資料,整合了下,就有了這個簡單的課程設計。

**直接附上

# @author:linjiaxiaozhu

import cv2

from matplotlib import pyplot as plt

import numpy as np

from math import *

path = input(

"請輸入你要上傳的:"

)while true:

print(

'操作提示【1:均值模糊;2:描邊;3:旋轉90度;4:雙邊濾波;5:裁剪;6:提亮】'

) num = int(input(

"請輸入你想要的操作(輸入數字):"

)) str =

['均值模糊', '描邊', '旋轉90度', '雙邊濾波', '裁剪','提亮'

]while num not in range(1, 7):

num = int(input(

"不存在該操作,請重新輸入想要的操作:"

)) print(

'你要對進行的操作是:' + str[num - 1]

) cv_img = cv2.imdecode(np.fromfile(path,dtype=np.uint8),-1)

#解決中文路徑的問題

img = cv2.cvtcolor(cv_img, cv2.color_rgb2bgr)

#原圖 y, x = img.shape[0:2]

#求出上傳的長x寬y

if num==1:

img1=cv2.blur(img, (1, 15))

#(1, 15)是垂直方向模糊

titles =

["yuantu", "mohu"

]elif num==2:

img1= cv2.copymakeborder(img,30,30,30,30,cv2.border_constant,value=

[252,157,154]

)#40是描的4條邊的粗細,value是顏色值

titles =

["yuantu", "miaobian"

]elif num == 3:

heightnew = int(x * fabs(sin(radians(90))

) + y * fabs(cos(radians(90))

)) widthnew = int(y * fabs(sin(radians(90))

) + x * fabs(cos(radians(90))

)) matrotation = cv2.getrotationmatrix2d((x / 2, y / 2), 90, 1)

#(x/2,y/2)是旋轉點的座標,旋轉90度

matrotation[0, 2] +=

(widthnew - x) / 2 # 重點在這步

matrotation[1, 2] +=

(heightnew - y) / 2 # 重點在這步

img1 = cv2.warpaffine(img, matrotation, (widthnew, heightnew), bordervalue=

(255, 255, 255))

titles =

["yuantu", "xuanzhuan"

]elif num == 4:

img1 = cv2.bilateralfilter(img, 0, 100, 15)

# 雙邊濾波

titles =

["yuantu", "lvbo"

]elif num == 5:

img1= img[50:int(x),50:int(y)

]# 裁剪座標為[x0:x1, y0:y1]

titles =

["yuantu", "caijian"

]elif num == 6:

img1 = cv2.addweighted(img, 0.7, img, 0.4, 3)

# 括號內引數分別為:圖1,圖1的權重,圖2,圖2的權重,權重和新增的值為3

titles =

["yuantu", "tiliang"

] images =

[img, img1]

for i in range(2):

plt.subplot(1, 2, i + 1), plt.imshow(images[i]

), plt.title(titles[i]

) plt.xticks(

), plt.yticks(

) plt.show(

)

記得先安裝相應的包哦

(因為是用了封裝好的函式,具體的引數代表什麼意思還不懂,等研究透了再來一篇教程吧)

python opencv 簡單閾值演算法

本文先了解乙個簡單閾值函式,以了解乙個閾值演算法的具體引數。然後比較不同閾值函式的區別。同樣的,先用一副圖說明本文重要大綱 先將影象矩陣進行二值化 也可以直接將影象用灰度值讀入,其中0就表示用灰度讀圖 cv2.imshow img img img1 cv2.threshold img,100,250...

python opencv攝像頭的簡單應用

1 安裝 安裝包 pip install opencv python 2.4.12 cp27 none win amd64.whl 2 codiwww.cppcns.comng utf 8 import cv2 import time cap cv2.videocapture 0 讀取攝像頭,0表示...

Python OpenCV實現簡單的人臉檢測

匯入opencv庫 import cv2 載入特徵分類器 opencv自帶 face cascade cv2.cascadeclassifier haarcascade frontalface default.xml 開啟電腦攝像頭 capture cv2.videocapture 0 獲得攝像頭捕...