(CODE)计算机视觉引论及数字成像系统
生活随笔
收集整理的這篇文章主要介紹了
(CODE)计算机视觉引论及数字成像系统
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
“Hello, world! ”程序
import cv2 as cvimg = cv.imread(r'G:\test\images\lena.jpg')#lena圖像cv.imshow('Hello World!!', img)#顯示圖像cv.waitKey(4000)#等待 cv.destroyAllWindows()#關(guān)閉 ''' imread()讀入一幅圖像 imshow()顯示圖像 '''圖像改變大小、平滑、閾值化
'''高斯模糊,尺寸,灰度,閾值化''' import cv2 as cvfilename = r'G:\test\images\lena.jpg' img = cv.imread(filename) #高斯模糊 imgGauss = cv.GaussianBlur(img, (1, 1), 0) imgGauss1 = cv.GaussianBlur(img, (5, 5), 0) imgGauss2 = cv.GaussianBlur(img, (5, 5), 1000) imgGauss3 = cv.GaussianBlur(img, (9, 9), 0) #圖像大小 image1 = cv.resize(img, (int(img.shape[1]/2), int(img.shape[0]/2))) image2 = cv.pyrDown(image1) image3 = cv.pyrUp(img) #灰度,閾值化,二值化 gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) _ ,gray1 = cv.threshold(gray, 120, 0xff, cv.THRESH_BINARY) _ ,gray2 = cv.threshold(gray, 60, 0xff, cv.THRESH_BINARY) _ ,gray3 = cv.threshold(gray, 180, 0xff, cv.THRESH_BINARY)cv.imshow("source image", img) ''' cv.imshow("Gaussian filtered image1,1,0", imgGauss) cv.imshow("Gaussian filtered image5,5,0", imgGauss1) cv.imshow("Gaussian filtered image5,5,10", imgGauss2) cv.imshow("Gaussian filtered image9,9,0", imgGauss3)cv.imshow("half size", image1) cv.imshow("quarter size", image2) cv.imshow("double size", image3) ''' cv.imshow("gray", gray) cv.imshow("threshold_gray1", gray1) cv.imshow("threshold_gray2", gray2) cv.imshow("threshold_gray3", gray3)cv.waitKey() cv.destroyAllWindows()高斯平滑
dst = cv.GaussianBlur(src, ksize, sigmaX[, dst[, sigmaY[, borderType]]])dst = 輸出與圖像大小和類型相同的圖像src
ksize = 高斯核大小
sigmaX = X方向上的高斯核標準偏差
sigmaY = Y方向上的高斯核標準差
如果 sigmaY 為零,則將其設置為等于 sigmaX
如果兩個西格瑪均為零,則分別根據(jù)ksize.width 和 ksize.height 進行計算
完全控制的結(jié)果,無論這一切的語義未來可能的修改,建議指定所有的ksize,sigmaX和sigmaY
borderType = 像素外推方法(borderInterpolate()有關(guān)詳細信息,請參見link )
(blur1,blur2)= 高斯核的大小,blur1和blur2的選取一般是奇數(shù),blur1和blur2的值可以不同
參數(shù)0表示標準差取0
當blur1=blur2=1時,相當于不對原始圖像做操作
blur1和blur2越大,圖像的模糊程度越大
但不是blur1和blur2越大越好,blur1和blur2太大,不僅會濾除噪音,還會平滑掉圖像中有用的信息。所以blur的選取要進行測試
如果要進行濾波的圖像的長寬比大致為1:1,那么選取blur時,一般設置blur1=blur2
如果要進行濾波的圖像的長寬比大致為m:n,那么選取blur時,blur1:blur2=m:n
大小
cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])–>dstdst = 目標圖像。當參數(shù)dsize不為0時,dst的大小為size;否則,它的大小需要根據(jù)src的大小,參數(shù)fx和fy決定。dst的type和src圖像相同
dsize = 目標圖像大小
fx = 水平軸上的比例因子
fy = 垂直軸上的比例因子
interpolation = 插值方法
1)INTER_NEAREST - 最近鄰插值法
2)INTER_LINEAR - 雙線性插值法(默認)
3)INTER_AREA - 基于局部像素的重采樣。對于圖像抽取來說,這可能是一個更好的方法。但如果是放大圖像時,它和最近鄰法的效果類似。
4)INTER_CUBIC - 基于4x4像素鄰域的3次插值法
5)INTER_LANCZOS4 - 基于8x8像素鄰域的Lanczos插值
img.shape #尺寸
img.shape[0] #圖片寬度
img.shape[1] #圖片高度
img.shape[2] #圖片通道數(shù)
img.size #總像素個數(shù)
img.max() #最大像素值
img.min() #最小像素值
img.mean() #像素平均值
對圖像向上采樣:pyrUp函數(shù)(放大圖像)
對圖像向下采樣:pyrDown函數(shù)(縮小圖像)
cvtColor():用于實現(xiàn)圖像的色彩空間轉(zhuǎn)換
閾值化
cv2.threshold(src,thresh,maxval,type[,dst])maxval:在二元閾值THRESH_BINARY和逆二元閾值THRESH_BINARY_INV中使用的最大值
type:使用的閾值類型
將原始的圖像通過制定閾值轉(zhuǎn)換為目標的黑白圖像
該函數(shù)通常用于從灰度圖像中獲取二進制圖像或用于消除噪聲,即濾除太小或太小的像素,很大的價值
函數(shù)支持幾種類型的閾值處理。 它們由類型參數(shù)確定。
此外,特殊值cv :: THRESH_OTSU或cv :: THRESH_TRIANGLE可以與上述值之一組合。
在這些情況下,函數(shù)使用Otsu或Triangle算法確定最佳閾值,并使用它而不是指定的閾值。 該函數(shù)返回計算的閾值。
目前,Otsu和Triangle方法僅適用于8位圖像
灰度,閾值化,HSV顏色模型,通道分離
'''灰度,閾值化,HSV顏色模型,通道分離''' import cv2 as cv import numpy as npfilename = r'G:\test\images\lena.jpg' filename_logo = r'G:\test\images\opencv-logo.png'img = cv.imread(filename)b, g, r = cv.split(img) #分離出圖片的B,R,G顏色通道 cv.imshow('blue', b) #顯示三通道的值都為R值時d圖片 cv.imshow('green', g) cv.imshow('red', r) cv.waitKey(8000)img_logo = cv.imread(filename_logo)gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) gray_logo = cv.cvtColor(img_logo, cv.COLOR_BGR2GRAY)cv.imshow("source image", img) cv.imshow("source image_logo", img_logo) cv.imshow("gray", gray) cv.imshow("gray_logo", gray_logo) cv.waitKey(4000) cv.destroyAllWindows()hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV) hsv_logo = cv.cvtColor(img_logo, cv.COLOR_BGR2HSV)cv.imshow("hsv_logo", hsv_logo) cv.imshow("Hue", hsv[:, :, 0]) cv.imshow("Saturation", hsv[:, :, 1]) cv.imshow("Value", hsv[:, :, 2]) cv.imshow("Hue_logo", hsv_logo[:, :, 0]) cv.imshow("Saturation_logo", hsv_logo[:, :, 1]) cv.imshow("Value_logo", hsv_logo[:, :, 2]) cv.waitKey(4000) cv.destroyAllWindows()cv.imshow("Blue", img[:, :, 0]) cv.imshow("Green", img[:, :, 1]) cv.imshow("Red", img[:, :, 2]) cv.imshow("Blue_logo", img_logo[:, :, 0]) cv.imshow("Green_logo", img_logo[:, :, 1]) cv.imshow("Red_logo", img_logo[:, :, 2])cv.waitKey(7000) cv.destroyAllWindows()[:, :, 1]
同理 imshow(G)和imshow(B)所顯示d圖像的顏色通道也依次為(G,G,G)和(B,B,B)。
而當三個通道d值相同時,則為灰度圖。
總結(jié)
以上是生活随笔為你收集整理的(CODE)计算机视觉引论及数字成像系统的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenCV滤波器 龙门石窟篇【Pyth
- 下一篇: 中值滤波器及均值滤波器