python opencv圖像處理(素描、懷舊、光照、流年、濾鏡 原理及實現)
圖像素描特效主要經過以下幾個步驟:
調用cv.cvtColor()函數將彩色圖像灰度化處理;通過cv.GaussianBlur()函數實現高斯濾波降噪;邊緣檢測采用Canny算子實現;最后通過cv.threshold()反二進制閾值化處理實現素描特效。
#coding:utf-8import cv2 as cvimport numpy as np#讀取原始圖像img = cv.imread(’d:/paojie.png’)#圖像灰度處理gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)#高斯濾波降噪gaussian = cv.GaussianBlur(gray, (5,5), 0) #Canny算子canny = cv.Canny(gaussian, 50, 150)#閾值化處理ret, result = cv.threshold(canny, 0, 255, cv.THRESH_BINARY_INV+cv.THRESH_OTSU)#顯示圖像#cv.imshow(’src’, img)#cv.imshow(’result’, result)cv.imshow(’result’,np.vstack((gray,result)))cv.waitKey()cv.destroyAllWindows()圖像素描特效展示
圖像懷舊特效
懷舊特效是將圖像的RGB三個分量分別按照一定比例進行處理的結果,其懷舊公式如下所示:
#coding:utf-8import cv2 as cvimport numpy as np#讀取原始圖像img = cv.imread(’d:/paojie.png’)#獲取圖像行和列rows, cols = img.shape[:2]#新建目標圖像dst = np.zeros((rows, cols, 3), dtype='uint8')#圖像懷舊特效for i in range(rows): for j in range(cols): B = 0.272*img[i,j][2] + 0.534*img[i,j][1] + 0.131*img[i,j][0] G = 0.349*img[i,j][2] + 0.686*img[i,j][1] + 0.168*img[i,j][0] R = 0.393*img[i,j][2] + 0.769*img[i,j][1] + 0.189*img[i,j][0] if B>255: B = 255 if G>255: G = 255 if R>255: R = 255 dst[i,j] = np.uint8((B, G, R)) #顯示圖像cv.imshow(’result’,np.vstack((img,dst)))cv.waitKey()cv.destroyAllWindows()圖像懷舊特效展示
圖像光照特效是指圖像存在一個類似于燈光的光暈特效,圖像像素值圍繞光照中心點呈圓形范圍內的增強。python實現代碼主要是通過雙層循環遍歷圖像的各像素點,尋找圖像的中心點,再通過計算當前點到光照中心的距離(平面坐標系中兩點之間的距離),判斷該距離與圖像中心圓半徑的大小關系,中心圓范圍內的圖像灰度值增強,范圍外的圖像灰度值保留,并結合邊界范圍判斷生成最終的光照效果。
#coding:utf-8import cv2 as cvimport mathimport numpy as np#讀取原始圖像img = cv.imread(’d:/paojie.png’)#獲取圖像行和列rows, cols = img.shape[:2]#設置中心點和光照半徑centerX = rows / 2 - 20centerY = cols / 2 + 20radius = min(centerX, centerY)#設置光照強度strength = 100#新建目標圖像dst = np.zeros((rows, cols, 3), dtype='uint8')#圖像光照特效for i in range(rows): for j in range(cols): #計算當前點到光照中心距離(平面坐標系中兩點之間的距離) distance = math.pow((centerY-j), 2) + math.pow((centerX-i), 2) #獲取原始圖像 B = img[i,j][0] G = img[i,j][1] R = img[i,j][2] if (distance < radius * radius): #按照距離大小計算增強的光照值 result = (int)(strength*( 1.0 - math.sqrt(distance) / radius )) B = img[i,j][0] + result G = img[i,j][1] + result R = img[i,j][2] + result #判斷邊界 防止越界 B = min(255, max(0, B)) G = min(255, max(0, G)) R = min(255, max(0, R)) dst[i,j] = np.uint8((B, G, R)) else: dst[i,j] = np.uint8((B, G, R)) #顯示圖像cv.imshow(’result’,np.vstack((img,dst)))cv.waitKey()cv.destroyAllWindows()圖像光照特效展示
流年是用來形容如水般流逝的光陰或年華,圖像處理中特指將原圖像轉換為具有時代感或歲月沉淀的特效。python實現代碼如下,它將原始圖像的藍色(B)通道的像素值開根號,再乘以一個權重參數,產生最終的流年效果。
#coding:utf-8import cv2 as cvimport mathimport numpy as np#讀取原始圖像img = cv.imread(’d:/paojie.png’)#獲取圖像行和列rows, cols = img.shape[:2]#新建目標圖像dst = np.zeros((rows, cols, 3), dtype='uint8')#圖像流年特效for i in range(rows): for j in range(cols): #B通道的數值開平方乘以參數12 B = math.sqrt(img[i,j][0]) * 12 G = img[i,j][1] R = img[i,j][2] if B>255: B = 255 dst[i,j] = np.uint8((B, G, R)) #顯示圖像cv.imshow(’result’,np.vstack((img,dst)))cv.waitKey()cv.destroyAllWindows()圖像流年特效展示
濾鏡主要是用來實現圖像的各種特殊效果,它在Photoshop中具有非常神奇的作用。濾鏡通常需要同通道、圖層等聯合使用,才能取得最佳藝術效果。本小節將講述一種基于顏色查找表(Look up Table)的濾鏡處理方法,它通過將每一個原始顏色進行轉換之后得到新的顏色。比如,原始圖像的某像素點為紅色(R-255, G-0, B-0),進行轉換之后變為綠色(R-0, G-255, B-0),之后所有是紅色的地方都會被自動轉換為綠色,而顏色查找表就是將所有的顏色進行一次(矩陣)轉換,很多的濾鏡功能就是提供了這么一個轉換的矩陣,在原始色彩的基礎上進行顏色的轉換。假設現在存在一張新的濾鏡顏色查找表,如圖所示,它是一張512×512大小,包含各像素顏色分布的圖像。下面這張圖片另存為本地,即可直接用于圖像濾鏡處理。
#coding:utf-8import cv2 as cv import numpy as np#獲取濾鏡顏色def getBGR(img, table, i, j): #獲取圖像顏色 b, g, r = img[i][j] #計算標準顏色表中顏色的位置坐標 x = int(g/4 + int(b/32) * 63) y = int(r/4 + int((b%32) / 4) * 63) #返回濾鏡顏色表中對應的顏色 return lj_map[x][y]#讀取原始圖像img = cv.imread(’d:/paojie.png’)lj_map = cv.imread(’lvjing.png’)#獲取圖像行和列rows, cols = img.shape[:2]#新建目標圖像dst = np.zeros((rows, cols, 3), dtype='uint8')#循環設置濾鏡顏色for i in range(rows): for j in range(cols): dst[i][j] = getBGR(img, lj_map, i, j) #顯示圖像cv.imshow(’result’,np.vstack((img,dst)))cv.waitKey()cv.destroyAllWindows()圖像濾鏡特效展示
以上就是python opencv圖像處理(素描、懷舊、光照、流年、濾鏡 原理及實現)的詳細內容,更多關于python opencv圖像處理的資料請關注好吧啦網其它相關文章!
相關文章: