毕设学习之opecv——图片特效

图片特效

1、灰度图像:
方法1
读取图片是直接利用cv2.imread(),
用于读取图片文件
imread函数有两个参数,第一个参数是图片路径,第二个参数表示读取图片的形式,有三种:
cv2.IMREAD_COLOR:加载彩色图片,这个是默认参数,可以直接写1。
cv2.IMREAD_GRAYSCALE:以灰度模式加载图片,可以直接写0。
cv2.IMREAD_UNCHANGED:包括alpha,可以直接写-1

import cv2
img = cv2.imread("xx.jpg",0)#1彩色图片,0灰色图片
cv2.imshow("Img",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

方法2:
利用颜色空间转换,将BGR转为GRAY

import cv2
img = cv2.imread("xx.jpg",1)#1彩色图片,0灰色图片
cv2.imshow("Img",img)
dst = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow("dst",dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

方法3:
利用矩阵遍历的方法,处理每一个像素点

#灰度图像实现
#灰度图像 R=G=B
import cv2
import numpy as np
img = cv2.imread("xx.jpg",1)#1彩色图片,0灰色图片
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
deep = imgInfo[2]
print(imgInfo)
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]
        gray = (int(b)+int(g)+int(r))/3#数据转换,防止数据越界
        dst[i,j]=np.uint8(gray)
cv2.imshow("img",img)
cv2.imshow("dst",dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

方法4
灰度计算公式 gray = r0.299+g0.587+b*0.114

import cv2
import numpy as np
img = cv2.imread("xx.jpg",1)#1彩色图片,0灰色图片
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
deep = imgInfo[2]
print(imgInfo)
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.299+g*0.587+b*0.114
        dst[i,j]=np.uint8(gray)
cv2.imshow("img",img)
cv2.imshow("dst",dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

运行效果:
毕设学习之opecv——图片特效_第1张图片

2、颜色反转

1、灰度图像的颜色反转:
原理:反转图像的灰度值 = 255-原图像灰度值

import cv2
import numpy as np

img = cv2.imread("xx.jpg",1)#1彩色图片,0灰色图片
cv2.imshow("Img",img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow("Dst1",gray)
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("Dst2",dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

运行效果:毕设学习之opecv——图片特效_第2张图片
2、彩色图像的颜色反转
原理:反转图像的值 = 255-原图像值

import cv2
import numpy as np

img = cv2.imread("xx.jpg",1)#1彩色图片,0灰色图片
cv2.imshow("Img",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]
        dst[i,j]= (255-b,255-g,255-r)
cv2.imshow("Dst",dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

运行效果:
毕设学习之opecv——图片特效_第3张图片
3、马赛克

原理:(待补充)

import cv2

img = cv2.imread("xx.jpg",1)#1彩色图片,0灰色图片
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]

for m in range(50,180):
    for n in range(50,180):
        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("Img",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

运行效果:
毕设学习之opecv——图片特效_第4张图片
4、边缘检测

方法1:opencv——API实现:

import cv2

img = cv2.imread("xx.jpg",1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
cv2.imshow("img",img)

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#gray处理
imaG = cv2.GaussianBlur(gray,(3,3),0)#滤波
dst = cv2.Canny(img,100,100)#50 50阈值
#图片卷积 >th ->边缘点
cv2.imshow("Dst",dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

运行结果:
毕设学习之opecv——图片特效_第5张图片

方法2:opencv——原理实现:

原理:待补充

import cv2
import numpy as np
import math

img = cv2.imread("xx.jpg",1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
cv2.imshow("img",img)

#soble 1算子模板 2图片卷积 3阈值判决
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#gray
cv2.imshow("Dst1",gray)

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]+gray[i,j+1]*2+gray[i,j+2]-gray[i+2,j]-gray[i+2,j+1]*2-gray[i+2,j+2]
        gx = gray[i,j]+gray[i+1,j]*2+gray[i+2,j]-gray[i,j+2]-gray[i+1,j+2]*2-gray[i+2,j+2]
        grad = math.sqrt(gy*gy+gx*gx)
      
        if grad>100:
            dst[i,j]=255
        else:
            dst[i,j]=0

cv2.imshow("Dst2",dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

运行结果:
毕设学习之opecv——图片特效_第6张图片
5、浮雕效果
实现原理:待补充

import cv2
import numpy as np


img = cv2.imread("xx.jpg",1)
cv2.imshow("img",img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow("gray",gray)
dst = np.zeros((height,height,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)
cv2.destroyAllWindows()

6、油画效果
实现原理:待补充

import cv2
import numpy as np

img = cv2.imread("xx.jpg",1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
cv2.imshow("img",img)

#1gray 2 

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 
cv2.imshow("gray",gray)

dst = np.zeros((height,width,3),np.uint8)
#4*4
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]>=32 and gray[i+m,j+n]<=(1+1)*32:
                    (b,g,r) = img[i+m,j+n]
                    
        dst[i,j] = (b,g,r)
                    

cv2.imshow("dst",dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

毕设学习之opecv——图片特效_第7张图片
7、颜色风格

实现原理:
个人理解:实际是增强某种颜色

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

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] #读取bgr
        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(0)
cv2.destroyAllWindows()

运行结果:
毕设学习之opecv——图片特效_第8张图片
6、毛玻璃,图片融合待补充

你可能感兴趣的:(毕业设计系列,opencv,计算机视觉,python,图像识别)