TensorFlow+OpenCV图像处理( 三 图片特效——边缘检测、浮雕效果、颜色风格、油画特效)

文章目录

    • 3.10Canny边缘检测(API)
    • 3.11 Sobel算子边缘检测
    • 3.12浮雕效果
    • 3.13颜色效果
    • 3.14油画特效

3.10Canny边缘检测(API)

Canny算子应用详解

import cv2
import numpy as np
import random
img = cv2.imread('image0.jpg',1)
info = img.shape
height = info[0]
width = info[1]
cv2.imshow('img',img)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
imgG = cv2.GaussianBlur(gray,(3,3),0)
dst = cv2.Canny(img,100,100)
cv2.imshow('dst',dst)
cv2.waitKey()
cv2.destroyAllWindows()

3.11 Sobel算子边缘检测

Sobel算子应用详解

import cv2
import numpy as np
import random
import math
img = cv2.imread('image0.jpg',1)
info = img.shape
height = info[0]
width = info[1]
cv2.imshow('img',img)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
dst = np.zeros((height,width,1),np.uint8)
for i in range(height-2):
    for j in range(width-2):
        gx =(gray[i-1,j+1]-gray[i-1,j-1])+2*(gray[i,j+1]-gray[i,j-1])+(gray[i+1,j+1]-gray[i+1,j-1])
        gy = (gray[i+1,j-1]-gray[i-1,j-1])+2*(gray[i+1,j]-gray[i-1,j])+(gray[i+1,j+1]-gray[i-1,j+1])
        grad = math.sqrt(gy*gy + gx*gx)
        if grad>800:
            dst[i,j] = 255
        else:
            dst[i,j] = 0
cv2.imshow('dst',dst)
cv2.waitKey()
cv2.destroyAllWindows()

3.12浮雕效果

import cv2
import numpy as np
img = cv2.imread('image0.jpg',1)
info = img.shape
height = info[0]
width = info[1]
cv2.imshow('img',img)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
dst = np.zeros((height,width,1),np.uint8)
for i in range(height):
    for j in range(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()
cv2.destroyAllWindows()

TensorFlow+OpenCV图像处理( 三 图片特效——边缘检测、浮雕效果、颜色风格、油画特效)_第1张图片

3.13颜色效果

import cv2
import numpy as np
img = cv2.imread('image0.jpg',1)
info = img.shape
height = info[0]
width = info[1]
cv2.imshow('img',img)
dst = np.zeros(img.shape,np.uint8)
#蓝色效果
for i in range(height):
    for j in range(width):
        (b,g,r) = img[i,j]
        b = b*1.5
        g = g*1.3
        if b>255:
            b = 255
        if g>255:
            g = 255
        dst[i,j] = (b,g,r)
cv2.imshow('dst',dst)
cv2.waitKey()
cv2.destroyAllWindows()

TensorFlow+OpenCV图像处理( 三 图片特效——边缘检测、浮雕效果、颜色风格、油画特效)_第2张图片TensorFlow+OpenCV图像处理( 三 图片特效——边缘检测、浮雕效果、颜色风格、油画特效)_第3张图片

3.14油画特效

import cv2
import numpy as np
img = cv2.imread('image0.jpg',1)
info = img.shape
height = info[0]
width = info[1]
cv2.imshow('img',img)
dst = np.zeros((height,width,3),np.uint8)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
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]:
                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()
cv2.destroyAllWindows()

TensorFlow+OpenCV图像处理( 三 图片特效——边缘检测、浮雕效果、颜色风格、油画特效)_第4张图片

你可能感兴趣的:(opencv,计算机视觉)