一、灰度处理
(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 ()
将图像从一个颜色空间转换为另一个颜色空间。
该功能将输入图像从一个颜色空间转换为另一个颜色空间。如果要转换RGB颜色空间,则应明确指定通道的顺序(RGB或BGR)。请注意,OpenCV中的默认颜色格式通常被称为RGB,但它实际上是BGR(字节相反)。因此,标准(24位)彩色图像中的第一个字节将是一个8位蓝色分量,第二个字节将是绿色,第三个字节将是红色。第四,第五和第六个字节将成为第二个像素(蓝色,然后是绿色,然后是红色),依此类推
cv::cvtColor()支持多种颜色空间之间的转换,其支持的转换类型和转换码如下:
1、RGB和BGR(opencv默认的彩色图像的颜色空间是BGR)颜色空间的转换
cv::COLOR_BGR2RGB cv::COLOR_RGB2BGR cv::COLOR_RGBA2BGRA cv::COLOR_BGRA2RGBA
2、向RGB和BGR图像中增添alpha通道
cv::COLOR_RGB2RGBA cv::COLOR_BGR2BGRA
3、从RGB和BGR图像中去除alpha通道
cv::COLOR_RGBA2RGB cv::COLOR_BGRA2BGR
4、从RBG和BGR颜色空间转换到灰度空间
cv::COLOR_RGB2GRAY cv::COLOR_BGR2GRAY
cv::COLOR_RGBA2GRAY
cv::COLOR_BGRA2GRAY
5、从灰度空间转换到RGB和BGR颜色空间
cv::COLOR_GRAY2RGB cv::COLOR_GRAY2BGR
cv::COLOR_GRAY2RGBA cv::COLOR_GRAY2BGRA
6、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
7、灰度空间域BGR565之间的转换
cv::COLOR_GRAY2BGR555 cv::COLOR_BGR5552GRAY
8、RGB和BGR颜色空间与CIE XYZ之间的转换
cv::COLOR_RGB2XYZ cv::COLOR_BGR2XYZ cv::COLOR_XYZ2RGB cv::COLOR_XYZ2BGR
9、RGB和BGR颜色空间与uma色度(YCrCb空间)之间的转换
cv::COLOR_RGB2YCrCb cv::COLOR_BGR2YCrCb cv::COLOR_YCrCb2RGB cv::COLOR_YCrCb2BGR
10、RGB和BGR颜色空间与HSV颜色空间之间的相互转换
cv::COLOR_RGB2HSV
cv::COLOR_BGR2HSV
cv::COLOR_HSV2RGB
cv::COLOR_HSV2BGR
11、RGB和BGR颜色空间与HLS颜色空间之间的相互转换
cv::COLOR_RGB2HLS cv::COLOR_BGR2HLS cv::COLOR_HLS2RGB cv::COLOR_HLS2BGR
12、RGB和BGR颜色空间与CIE Lab颜色空间之间的相互转换
cv::COLOR_RGB2Lab cv::COLOR_BGR2Lab cv::COLOR_Lab2RGB cv::COLOR_Lab2BGR
13、RGB和BGR颜色空间与CIE Luv颜色空间之间的相互转换
cv::COLOR_RGB2Luv cv::COLOR_BGR2Luv cv::COLOR_Luv2RGB cv::COLOR_Luv2BGR
14、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)
复制代码
结果:
二、颜色翻转
(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)
复制代码
结果:
三、马赛克效果
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)
复制代码
结果:
四、毛玻璃效果
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)
复制代码
结果:
五、图片融合
(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)
复制代码
结果:
六、边缘检测
(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)
复制代码
七、浮雕功能
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)
复制代码
结果:
七、颜色风格
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)
复制代码
结果:
八、油画特效
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 currentMaxfor 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)
复制代码
结果: