計算機視覺—圖像特效(3)

1、灰度處理

(1) imread (src,0)

#imread 
import cv2
img0 = cv2.imread('canton.jpg',0)
img1 = cv2.imread('canton.jpg',1)
print(img0.shape)
print(img1.shape)
cv2.imshow('src',img0)
cv2.imshow('src',img1)
cv2.waitKey(0)
複製代碼

(2)cvtColor ()

將圖像從一個顏色空間轉換爲另外一個顏色空間。python

該功能將輸入圖像從一個顏色空間轉換爲另外一個顏色空間。若是要轉換RGB顏色空間,則應明確指定通道的順序(RGB或BGR)。請注意,OpenCV中的默認顏色格式一般被稱爲RGB,但它其實是BGR(字節相反)。所以,標準(24位)彩色圖像中的第一個字節將是一個8位藍色份量,第二個字節將是綠色,第三個字節將是紅色。第四,第五和第六個字節將成爲第二個像素(藍色,而後是綠色,而後是紅色),依此類推數組

cv::cvtColor()支持多種顏色空間之間的轉換,其支持的轉換類型和轉換碼以下:dom

一、RGB和BGR(opencv默認的彩色圖像的顏色空間是BGR)顏色空間的轉換函數

cv::COLOR_BGR2RGB cv::COLOR_RGB2BGR cv::COLOR_RGBA2BGRA cv::COLOR_BGRA2RGBAui

二、向RGB和BGR圖像中增添alpha通道spa

cv::COLOR_RGB2RGBA cv::COLOR_BGR2BGRA3d

三、從RGB和BGR圖像中去除alpha通道code

cv::COLOR_RGBA2RGB cv::COLOR_BGRA2BGRorm

四、從RBG和BGR顏色空間轉換到灰度空間cdn

cv::COLOR_RGB2GRAY cv::COLOR_BGR2GRAY

cv::COLOR_RGBA2GRAY

cv::COLOR_BGRA2GRAY

五、從灰度空間轉換到RGB和BGR顏色空間

cv::COLOR_GRAY2RGB cv::COLOR_GRAY2BGR

cv::COLOR_GRAY2RGBA cv::COLOR_GRAY2BGRA

六、RGB和BGR顏色空間與BGR565顏色空間之間的轉換

cv::COLOR_RGB2BGR565 cv::COLOR_BGR2BGR565 cv::COLOR_BGR5652RGB cv::COLOR_BGR5652BGR cv::COLOR_RGBA2BGR565 cv::COLOR_BGRA2BGR565 cv::COLOR_BGR5652RGBA cv::COLOR_BGR5652BGRA

七、灰度空間域BGR565之間的轉換

cv::COLOR_GRAY2BGR555 cv::COLOR_BGR5552GRAY

八、RGB和BGR顏色空間與CIE XYZ之間的轉換

cv::COLOR_RGB2XYZ cv::COLOR_BGR2XYZ cv::COLOR_XYZ2RGB cv::COLOR_XYZ2BGR

九、RGB和BGR顏色空間與uma色度(YCrCb空間)之間的轉換

cv::COLOR_RGB2YCrCb cv::COLOR_BGR2YCrCb cv::COLOR_YCrCb2RGB cv::COLOR_YCrCb2BGR

十、RGB和BGR顏色空間與HSV顏色空間之間的相互轉換

cv::COLOR_RGB2HSV

cv::COLOR_BGR2HSV

cv::COLOR_HSV2RGB

cv::COLOR_HSV2BGR

十一、RGB和BGR顏色空間與HLS顏色空間之間的相互轉換

cv::COLOR_RGB2HLS cv::COLOR_BGR2HLS cv::COLOR_HLS2RGB cv::COLOR_HLS2BGR

十二、RGB和BGR顏色空間與CIE Lab顏色空間之間的相互轉換

cv::COLOR_RGB2Lab cv::COLOR_BGR2Lab cv::COLOR_Lab2RGB cv::COLOR_Lab2BGR

1三、RGB和BGR顏色空間與CIE Luv顏色空間之間的相互轉換

cv::COLOR_RGB2Luv cv::COLOR_BGR2Luv cv::COLOR_Luv2RGB cv::COLOR_Luv2BGR

1四、Bayer格式(raw data)向RGB或BGR顏色空間的轉換

cv::COLOR_BayerBG2RGB

cv::COLOR_BayerGB2RGB

cv::COLOR_BayerRG2RGB

cv::COLOR_BayerGR2RGB

cv::COLOR_BayerBG2BGR

cv::COLOR_BayerGB2BGR

cv::COLOR_BayerRG2BGR

cv::COLOR_BayerGR2BGR

cvtColor(

  • InputArray 輸入圖像:8位無符號,16位無符號(CV_16UC ...)或單精度浮點。,
  • OutputArray 輸出與src相同大小和深度的圖像。,
  • INT 顏色空間轉換代碼,
  • INT 目標圖像中的通道數量; 若是參數爲0,則通道的數量自動從src和代碼中導出。 )
import cv2
img = cv2.imread('canton.jpg',1)
dst = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow('dst',dst)
cv2.waitKey(0)
複製代碼

(3)np.uint8()

import cv2
import numpy as np
img = cv2.imread('canton.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
dst = np.zeros((height,width,3),np.uint8)
for i in range(0,height):
    for j in range(0,width):
        (b,g,r) = img[i,j]
        b = int(b)
        g = int(g)
        r = int(r)
        gray = r*0.2+g*0.5+b*0.2
        dst[i,j] = np.uint8(gray)
cv2.imshow('dst',dst)
cv2.waitKey(0)
複製代碼

結果:

2、顏色翻轉

(1)灰色圖片顏色翻轉

import cv2
import numpy as np
img = cv2.imread('canton.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
dst = np.zeros((height,width,1),np.uint8)
for i in range(0,height):
    for j in range(0,width):
        grayPixel = gray[i,j]
        dst[i,j] = 255-grayPixel
cv2.imshow('dst',dst)
cv2.waitKey(0)
複製代碼

結果:

(2)彩色圖片顏色翻轉

import cv2
import numpy as np
img = cv2.imread('canton.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
dst = np.zeros((height,width,3),np.uint8)
for i in range(0,height):
    for j in range(0,width):
        (b,g,r) = img[i,j]
        dst[i,j] = (255-b,255-g,255-r)
cv2.imshow('dst',dst)
cv2.waitKey(0)
複製代碼

結果:

3、馬賽克效果

import cv2
import numpy as np
img = cv2.imread('cantontower.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
for m in range(0,600):
    for n in range(300,600):
        # pixel ->10*10
        if m%10 == 0 and n%10==0:
            for i in range(0,10):
                for j in range(0,10):
                    (b,g,r) = img[m,n]
                    img[i+m,j+n] = (b,g,r)
cv2.imshow('dst',img)
cv2.waitKey(0)
複製代碼

結果:

4、毛玻璃效果

import cv2
import numpy as np
import random
img = cv2.imread('cantontower.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
dst = np.zeros((height,width,3),np.uint8)
mm = 8
for m in range(0,height-mm):
    for n in range(0,width-mm):
        index = int(random.random()*8)#0-8
        (b,g,r) = img[m+index,n+index]
        dst[m,n] = (b,g,r)
cv2.imshow('dst',dst)
cv2.waitKey(0)
複製代碼

結果:

5、圖片融合

(1)addWeighted()

cv2.addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]])

  • 參數
  • src1 圖片1連接
  • alpha 是src1透明度
  • src2 圖片2連接
  • beta 是src2透明度
  • gamma 一個加到權重總和上的標量值,dst = src1 * alpha + src2 * beta + gamma;
  • dtype 輸出陣列的可選深度,有默認值-1。;當兩個輸入數組具備相同的深度時,這個參數設置爲-1(默認值),即等同於src1.depth()
import cv2
import numpy as np
img0 = cv2.imread('cantontower.jpg',1)
img1 = cv2.imread('qilou.jpg',1)
imgInfo = img0.shape
height = imgInfo[0]
width = imgInfo[1]

roiH = int(height/2)
roiW = int(width/2)
img0ROI = img0[0:roiH,0:roiW]
img1ROI = img1[0:roiH,0:roiW]

dst = np.zeros((roiH,roiW,3),np.uint8)
dst = cv2.addWeighted(img0ROI,0.5,img1ROI,0.5,0)
# dst = src1 * alpha + src2 * beta + gamma;

cv2.imshow('dst',dst)
cv2.waitKey(0)
複製代碼

結果:

6、邊緣檢測

(1)GaussianBlur()

GaussianBlur(InputArray src, OutputArray dst, Size ksize, double sigmaX, double sigmaY=0, int borderType=BORDER_DEFAULT)

參數

  • src,輸入圖像,即源圖像,填Mat類的對象便可。它能夠是單獨的任意通道數的圖片,但須要注意,圖片深度應該爲CV_8U,CV_16U, CV_16S, CV_32F 以及 CV_64F之一。
  • dst,即目標圖像,須要和源圖片有同樣的尺寸和類型。好比能夠用Mat::Clone,以源圖片爲模板,來初始化獲得如假包換的目標圖。
  • ksize,高斯內核的大小。其中ksize.width和ksize.height能夠不一樣,但他們都必須爲正數和奇數(並不能理解)。或者,它們能夠是零的,它們都是由sigma計算而來。
  • sigmaX,表示高斯核函數在X方向的的標準誤差。
  • sigmaY,表示高斯核函數在Y方向的的標準誤差。若sigmaY爲零,就將它設爲sigmaX,若是sigmaX和sigmaY都是0,那麼就由ksize.width和ksize.height計算出來。爲告終果的正確性着想,最好是把第三個參數Size,第四個參數sigmaX和第五個參數sigmaY所有指定到。
  • borderType,用於推斷圖像外部像素的某種邊界模式。注意它有默認值BORDER_DEFAULT。

(2)Canny()

Canny(InputArray image,OutputArray edges,double threshold1,double threshold2,int apertureSize = 3,bool L2gradient = false )

參數

  • image 輸入8位圖像.
  • edges 輸出邊緣圖; 單通道8位圖像,其大小與圖像相同。
  • threshold1 滯後程序的第一閾值。
  • threshold2 滯後程序的第二閾值。
  • apertureSize Sobel算子的光圈大小。
  • L2gradient 一個標誌,代表是否有更準確的 L2 norm =(dI/dx)2+(dI/dy)2,仍是默認的 L1 norm =|dI/dx|+|dI/dy| 就行 ( L2gradient=false )
import cv2
import numpy as np
import random
img = cv2.imread('cantontower.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
cv2.imshow('src',img)

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
imgG = cv2.GaussianBlur(gray,(3,3),0)
dst = cv2.Canny(img,50,50)
cv2.imshow('dst',dst)
cv2.waitKey(0)
複製代碼

結果:

(3)邊緣檢測原理

邊緣是圖像中灰度發生急劇變化的區域邊界。圖像灰度的變化狀況能夠用圖像灰度分佈的梯度來表示,數字圖像中求導是利用差分近似微分來進行的,實際上經常使用空域微分算子經過卷積來完成

Sobel算子是高斯平滑與微分操做的結合體。因此其抗噪能力很是強,用途較多。通常的sobel算子包含x與y兩個方向,算子模板爲:

在opencv函數中,還可以設置卷積核(ksize)的大小,假設ksize=-1,就演變爲3*3的Scharr算子,模板無非變了個數字:

import cv2
import numpy as np
import random
import math
img = cv2.imread('cantontower.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
cv2.imshow('src',img)

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
dst = np.zeros((height,width,1),np.uint8)
for i in range(0,height-2):
    for j in range(0,width-2):
        gy = -gray[i,j]*1-gray[i,j+1]*2-gray[i,j+2]*1+gray[i+2,j]*1+gray[i+2,j+1]*2+gray[i+2,j+2]*1
        gx = -gray[i,j]*1+gray[i+2,j]*1-gray[i,j+1]*2+gray[i+2,j+1]*2-gray[i,j+2]*1+gray[i+2,j+2]*1
        grad = math.sqrt(gx*gx+gy*gy)
        if grad>50:
            dst[i,j] = 255
        else:
            dst[i,j] = 0
cv2.imshow('dst',dst)
cv2.waitKey(0)
複製代碼

7、浮雕功能

import cv2
import numpy as np
img = cv2.imread('image0.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# newP = gray0-gray1+150
dst = np.zeros((height,width,1),np.uint8)
for i in range(0,height):
    for j in range(0,width-1):
        grayP0 = int(gray[i,j])
        grayP1 = int(gray[i,j+1])
        newP = grayP0-grayP1+150
        if newP > 255:
            newP = 255
        if newP < 0:
            newP = 0
        dst[i,j] = newP
cv2.imshow('dst',dst)
cv2.waitKey(0)
複製代碼

結果:

7、顏色風格

import cv2
import numpy as np
img = cv2.imread('cantontower.jpg',1)
cv2.imshow('src',img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]

dst = np.zeros((height,width,3),np.uint8)
for i in range(0,height):
    for j in range(0,width):
        (b,g,r) = img[i,j]
        b = b*1.5
        g = g*1.3
        r = r
        if b>255:
            b = 255
        if g>255:
            g = 255
        if r>255:
            r = 255
        dst[i,j]=(b,g,r)
cv2.imshow('dst',dst)
cv2.waitKey(0)
複製代碼

結果:

8、油畫特效

import cv2
import numpy as np
img = cv2.imread('image00.jpg',1)
cv2.imshow('src',img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
dst = np.zeros((height,width,3),np.uint8)
for i in range(4,height-4):
    for j in range(4,width-4):
        array1 = np.zeros(8,np.uint8)
        for m in range(-4,4):
            for n in range(-4,4):
                p1 = int(gray[i+m,j+n]/32)
                array1[p1] = array1[p1]+1
        currentMax = array1[0]
        l = 0
        for k in range(0,8):
            if currentMax<array1[k]:
                currentMax = array1[k]
                l = k
        for m in range(-4,4):
            for n in range(-4,4):
                if gray[i+m,j+n]>=(l*32) and gray[i+m,j+n]<=((l+1)*32):
                    (b,g,r) = img[i+m,j+n]
        dst[i,j] = (b,g,r)
cv2.imshow('dst',dst)
cv2.waitKey(0)
複製代碼

結果:

相關文章
相關標籤/搜索