目录
主要内容
图像灰度处理1
算法优化
马赛克
毛玻璃效果
图片融合
边缘检测
浮雕效果
颜色风格
油画效果
主要内容
图像灰度处理1
#imread #方法1 imread import cv2img0 = cv2.imread('image0.jpg',0)img1 = cv2.imread('image0.jpg',1)print(img0.shape)print(img1.shape)cv2.imshow('src',img0)cv2.waitKey(0)
#方法2 cvtColorimport cv2img = cv2.imread('image0.jpg',1)dst = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)# 颜色空间转换 1 data 2 BGR graycv2.imshow('dst',dst)cv2.waitKey(0)
import cv2import numpy as npimg = cv2.imread('image0.jpg',1)imgInfo = img.shapeheight = imgInfo[0]width = imgInfo[1]# RGB R=G=B = gray (R+G+B)/3dst = 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))/3dst[i,j] = np.uint8(gray)cv2.imshow('dst',dst)cv2.waitKey(0)
#方法4 gray = r*0.299+g*0.587+b*0.114import cv2import numpy as npimg = cv2.imread('image0.jpg',1)imgInfo = img.shapeheight = 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.299+g*0.587+b*0.114dst[i,j] = np.uint8(gray)cv2.imshow('dst',dst)cv2.waitKey(0)
算法优化
# 1 灰度 最重要 2 基础 3 实时性 # 定点-》浮点 +- */ >> # r*0.299+g*0.587+b*0.114import cv2import numpy as npimg = cv2.imread('image0.jpg',1)imgInfo = img.shapeheight = imgInfo[0]width = imgInfo[1]# RGB R=G=B = gray (R+G+B)/3dst = 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*1+g*2+b*1)/4gray = (r+(g<<1)+b)>>2dst[i,j] = np.uint8(gray)cv2.imshow('dst',dst)cv2.waitKey(0)
乘以2相当于左移一位,除以4相当于右移二位
马赛克
用右上角一个像素替换所有的
每隔10个像素替换一个像素
import cv2import numpy as npimg = cv2.imread('image0.jpg',1)imgInfo = img.shapeheight = imgInfo[0]width = imgInfo[1]for m in range(100,300):for n in range(100,200):# pixel ->10*10if 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)
获取bgr时一个10*10取一次值就好, 上面这个运算量会增大
可以将获取bgr的代码提前写赋值循环之前
import cv2import numpy as npimg = cv2.imread('image0.jpg',1)imgInfo = img.shapeheight = imgInfo[0]width = imgInfo[1]for m in range(100,300):for n in range(100,200):# pixel ->10*10if m%10 == 0 and n%10==0:(b,g,r) = img[m,n]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('dst1',img)cv2.waitKey(0)
毛玻璃效果
import cv2import numpy as npimport randomimg = cv2.imread('image0.jpg',1)imgInfo = img.shapeheight = imgInfo[0]width = imgInfo[1]dst = np.zeros((height,width,3),np.uint8)mm = 8for 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)
图片融合
# dst = src1*a+src2*(1-a)import cv2import numpy as npimg0 = cv2.imread('image0.jpg',1)img1 = cv2.imread('image1.jpg',1)imgInfo = img0.shapeheight = imgInfo[0]width = imgInfo[1]# ROIroiH = int(height/2)roiW = int(width/2)img0ROI = img0[0:roiH,0:roiW]img1ROI = img1[0:roiH,0:roiW]# dstdst = np.zeros((roiH,roiW,3),np.uint8)dst = cv2.addWeighted(img0ROI,0.5,img1ROI,0.5,0)#add src1*a+src2*(1-a)# 1 src1 2 a 3 src2 4 1-acv2.imshow('dst',dst)cv2.waitKey(0)
边缘检测
其实质是图像的卷积
import cv2import numpy as npimport randomimg = cv2.imread('image0.jpg',1)imgInfo = img.shapeheight = 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) #图片卷积——》thcv2.imshow('dst',dst)cv2.waitKey(0)
import cv2import numpy as npimport randomimport mathimg = cv2.imread('image0.jpg',1)imgInfo = img.shapeheight = imgInfo[0]width = imgInfo[1]cv2.imshow('src',img)# sobel 1 算子模版 2 图片卷积 3 阈值判决 # [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 = dst# sqrt(a*a+b*b) = f>thgray = 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]*1gx = 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] = 255else:dst[i,j] = 0cv2.imshow('dst',dst)cv2.waitKey(0)
浮雕效果
import cv2import numpy as npimg = cv2.imread('image0.jpg',1)imgInfo = img.shapeheight = imgInfo[0]width = imgInfo[1]gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)# newP = gray0-gray1+150dst = 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+150if newP > 255:newP = 255if newP < 0:newP = 0dst[i,j] = newPcv2.imshow('dst',dst)cv2.waitKey(0)
颜色风格
import cv2import numpy as npimg = cv2.imread('image0.jpg',1)cv2.imshow('src',img)imgInfo = img.shapeheight = imgInfo[0]width = imgInfo[1]#rgb -》RGB new “蓝色”# b=b*1.5# g = g*1.3dst = 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.5g = g*1.3if b>255:b = 255if g>255:g = 255dst[i,j]=(b,g,r)cv2.imshow('dst',dst)cv2.waitKey(0)
油画效果
#1 gray 2 7*7 10*10 3 0-255 256 4 640-63 64-127 # 3 10 0-63 99 # 4 count 5 dst = resultimport cv2import numpy as npimg = cv2.imread('image00.jpg',1)cv2.imshow('src',img)imgInfo = img.shapeheight = 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]+1currentMax = array1[0]l = 0for 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)
灰度等级划分