2018.12.5 —2018.12.6计算机视觉加强之图像特效
- 01 灰度处理1
- 02灰度处理2
- 03 算法优化
- 04 图片颜色反转
- 05 马赛克
- 06 毛玻璃
- 07 图片融合
- 08边缘检侧1
- 09边缘检测2
- 10 浮雕效果
- 11 颜色映射
- 总结
01 灰度处理1
'''
# imrade
# 方法1 imread
import cv2
img0 = cv2.imread('image0.jpg',0)
img1 = cv2.imread('image0.jpg',1)
print(img0.shape)
print(img1.shape)
cv2.imshow('src',img0)
cv2.waitKey(0)
'''
import cv2
img = cv2.imread('image0.jpg',1)
dst = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow('bray',dst)
cv2.waitKey(0)
02灰度处理2
'''
# 方法3 gray (R+G+B)/3
import cv2
import numpy as np
img = cv2.imread('image0.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
# RGB R=G=B =gray
dst = np.zeros((height,width),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('dst',dst)
cv2.waitKey(0)
'''
import cv2
import numpy as np
img = cv2.imread('image0.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
dst = np.zeros((height,width),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('dst',dst)
cv2.waitKey(0)
03 算法优化
import cv2
import numpy as np
img = cv2.imread('image0.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
dst = np.zeros((height,width),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+(g<<1)+b)>>2
dst[i,j] = np.uint8(gray)
cv2.imshow('dst',dst)
cv2.waitKey(0)
04 图片颜色反转
'''
# 灰度图片的颜色翻转
# 0-255 255-当前
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)#颜色空间转换 1:data 2:BGR ->BRAY
dst = np.zeros((height,width),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)
'''
import cv2
import numpy as np
img = cv2.imread('image0.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)
05 马赛克
import cv2
import numpy as np
img = cv2.imread('image0.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
for m in range(100,300):
for n in range(100,200):
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)
06 毛玻璃
import cv2
import numpy as np
import random
img = cv2.imread('image0.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)
(b,g,r) = img[m+index,n+index]
dst[m,n] = (b,g,r)
cv2.imshow('dst',dst)
cv2.waitKey(0)
07 图片融合
import cv2
import numpy as np
img0 = cv2.imread('image0.jpg',1)
img1 = cv2.imread('image1.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],np.uint8)
dst = cv2.addWeighted(img0ROI,0.5,img1ROI,0.5,0)
cv2.imshow('dst',dst)
cv2.waitKey(1000)
08边缘检侧1
import cv2
import numpy as np
import random
img = cv2.imread('image0.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
cv2.imshow('src',img)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2BGRA)
imgG = cv2.GaussianBlur(gray,(3,3),0)
dst = cv2.Canny(gray,50,50)
cv2.imshow('dst',dst)
cv2.waitKey(0)
09边缘检测2
import cv2
import numpy as np
import random
import math
img = cv2.imread('image0.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]+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(gx*gx+gy*gy)
if grad>50:
dst[i,j] = 255
else:
dst[i,j] = 0
cv2.imshow('dst',dst)
cv2.waitKey(0)
10 浮雕效果
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)
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)
11 颜色映射
import cv2
import numpy as np
img = cv2.imread('image0.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
if b>255:
b=255
if g>255:
g = 255
dst[i,j]=(b,g,r)
cv2.imshow('dst',dst)
cv2.waitKey(0)
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)
总结
哼哼 今天就没总结了