1. 灰度处理
方法1:img0 = cv2.imread('image0.jpg', 0)
方法2:cvtColor方法
img0 = cv2.imread('image0.jpg', 1)
dst = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#颜色空间转换 1 data 2颜色转换的方式
方法3: 对R,G,B的颜色取平均值
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 (R+G+B)/3
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('dst',dst)
cv2.waitKey(0)
方法4: 心理学系数
gray = r*0.299 + g*0.587 + b*0.114
颜色反转
对于灰度图像(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)
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)
对于彩色图像的颜色反转则稍有不同
#RGB 255-R=newR
#0-255 255-当前
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)
马赛克效果
先来看一下什么是马赛克,在一个小区域内用左上角的值来替代整个区域的值。
#RGB 255-R=newR
#0-255 255-当前
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)
其实我觉得(b,g,r) = img[i,j] 应该加在后两个for循环之前
毛玻璃效果
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)#0-8
(b,g,r) = img[m+index,n+index]
dst[m,n] = (b,g,r)
cv2.imshow('dst',dst)
cv2.waitKey(0)
图片融合
每一种图片直接乘一个比例系数然后在相加的结果
。
。
。
边缘检测
方法1:调用API
1.图像灰度化 2,高斯滤波 3,调用canny方法
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)
#canny 1 gray 2 高斯 3 canny
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
imgG = cv2.GaussianBlur(gray,(3,3),0)
dst = cv2.Canny(img,50,50) #1data 2,门限值(大于门限则是边缘点)
cv2.imshow('dst',dst)
cv2.waitKey(0)
方法2 : 用源码来实现 sobel 1,算子模板 2,图像卷积 3,阈值判决
sobel的模板分为水平和竖直的两种
# [1 2 1 [ 1 0 -1 # 0 0 0 2 0 -2 # -1 -2 -1 ] 1 0 -1 ]
图片卷积:
[1 2 3 4] [a b c d] 的卷积为 a*1+b*2+c*3+d*4 (结果就是梯度)
梯度的方向有两种,水平和竖直,可以使用arctan来确定梯度的方向
根号下A方加B方来计算梯度的
sqrt(a*a+b*b)结果就是幅值,然后和阈(yu)值比较 a是第一个算子也就是竖直方向上的算子,他的卷积结果是a; 同理可求得b。
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)
浮雕效果
———计算梯度的过程, newP = gray0 - gray1 + 150
新的像素值 = 相邻的两个像素值之差(突出边缘特征)再加上一个恒定值(加强灰度等级)
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)
颜色映射:
。
。
。
油画效果
。
。
。
TO BE CONTINUE 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
图形的绘制(形状,文字,图片)