#方法1 imread 的第二个参数设置成0
import cv2
img0 = cv2.imread(r"C:\Users\Administrator\Desktop\222\233.jpg",0)
img1 = cv2.imread(r"C:\Users\Administrator\Desktop\222\233.jpg",1)
cv2.imshow('image',img0)
print(img0.shape)
print(img1.shape)#多一个深度3
cv2.imshow('image1',img1)
cv2.waitKey(0)
(698, 1024)
(698, 1024, 3)
#方法2 cvtColor
import cv2
img1 = cv2.imread(r"C:\Users\Administrator\Desktop\222\233.jpg",1)
dst = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)
cv2.imshow('dst',dst)
cv2.waitKey(0)
灰度值实现1
令Gray=(R+G+B)/3
import cv2
import numpy as np
img = cv2.imread(r"C:\Users\Administrator\Desktop\222\233.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]
tmp=(int(b)+int(g)+int(r))/3 #(转成int防止溢出)
dst[i,j]=[tmp,tmp,tmp]
cv2.imshow('dst',dst)
cv2.waitKey(0)
对于彩色转灰度,有一个很著名的心理学公式:
Gray = R* 0.299 + G* 0.587 + B* 0.114
以上公式同样可以实现图片转为灰度图
灰度图像代码的优化
定点数运算效率大于浮点; ±操作大于* /操作
import cv2
import numpy as np
img = cv2.imread(r"C:\Users\Administrator\Desktop\222\233.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)
# tmp=(r*1+g*2+b*1)/4 #将0.299,0.587 ,0.114转成大概的整数
tmp=(r+(g<<1)+b)>>2 #将乘法转成移位计算,效率优化
dst[i,j]=[tmp,tmp,tmp]
cv2.imshow('dst',dst)
cv2.waitKey(0)
#灰色图片反转 255-当前
import cv2
import numpy as np
img1 = cv2.imread(r"C:\Users\Administrator\Desktop\222\233.jpg",1)
height =img1.shape[0]
width =img1.shape[1]
gray = cv2.cvtColor(img1,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)
#彩色图片颜色反转 R,G,B分为为255-当前
import cv2
import numpy as np
img1 = cv2.imread(r"C:\Users\Administrator\Desktop\222\233.jpg",1)
height =img1.shape[0]
width =img1.shape[1]
dst = np.zeros((height,width,3),np.uint8)
for i in range(0,height):
for j in range(0,width):
(b,g,r)=img1[i,j]
dst[i,j]=(255-b,255-g,255-r)
cv2.imshow('dst',dst)
cv2.waitKey(0)
#彩色图片颜色反转 R,G,B分为为255-当前
import cv2
import numpy as np
img = cv2.imread(r"C:\Users\Administrator\Desktop\222\233.jpg",1)
height =img.shape[0]
width =img.shape[1]
for m in range(100,300):
for n in range(100,200):
if 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):
img[i+m,j+n]=(b,g,r)
cv2.imshow('img',img)
cv2.waitKey(0)
#当前像素 为原图对应位置行列+random(一个较小值如小于8)的位置的像素
import cv2
import numpy as np
import random
img = cv2.imread(r"C:\Users\Administrator\Desktop\222\233.jpg",1)
height =img.shape[0]
width =img.shape[1]
print(img.shape)
dst = np.zeros((height,width,3),np.uint8)
mm=8
for i in range(0,height-mm):
for j in range(0,width-mm):
index =int(random.random()*mm)
(b,g,r)=img[i+index,j+index]
dst[i,j]=(b,g,r)
cv2.imshow('dst',dst)
cv2.waitKey(0)
(698, 1024, 3)
#当前像素 为原图对应位置行列+random(一个较小值如小于8)的位置的像素
import cv2
import numpy as np
import random
img = cv2.imread(r"C:\Users\Administrator\Desktop\222\233.jpg",1)
img2 = cv2.imread(r"C:\Users\Administrator\Desktop\222\333.jpg",1)
height =img.shape[0]
width =img.shape[1]
#ROI 选择一个范围 比两张图都小的范围
roiH=int(height/3)
roiW=int(width/3)
imgROI=img[0:roiH,0:roiW]
img2ROI=img2[0:roiH,0:roiW]
#dst
dst = np.zeros((roiH,roiW,3),np.uint8)
dst =cv2.addWeighted(imgROI,0.5,img2ROI,0.5,0)
#src1*a +src2*(1-a) 1:src1 2:a 3:src2 4:1-a
cv2.imshow('dst',dst)
cv2.waitKey(0)
运用卷积运算
#1转换成灰度图片
#2进行高斯滤波 去除噪声
#3canny
import cv2
import numpy as np
import random
img = cv2.imread(r"C:\Users\Administrator\Desktop\222\233.jpg",1)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#进行高斯滤波
imgG = cv2.GaussianBlur(gray,(3,3),0)
#dst
dst = cv2.Canny(img,50,50)
cv2.imshow('dst',dst)
cv2.waitKey(0)
import cv2
import numpy as np
import random
import math
img = cv2.imread(r"C:\Users\Administrator\Desktop\222\233.jpg",1)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
height=gray.shape[0]
width=gray.shape[1]
#dst
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+1,j]*2+gray[i+2,j]*1-gray[i,j+2]*1-gray[i+1,j+2]*2-gray[i+2,j+2]*1
grad=math.sqrt(gy*gy+gx*gx)
if grad>50:
dst[i,j]=255
else:
dst[i,j]=0
cv2.imshow('dst',dst)
cv2.waitKey(0)
new= gary0-gray1+150
import cv2
import numpy as np
import random
import math
img = cv2.imread(r"C:\Users\Administrator\Desktop\222\233.jpg",1)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
height=gray.shape[0]
width=gray.shape[1]
#dst
dst = np.zeros((height,width,1),np.uint8)
for i in range(0,height):
for j in range(0,width-1):
gray0=int(gray[i,j])
gray1=int(gray[i,j+1])
new=gray0-gray1+150
if new>255:
new=255
if new<0:
new=0
dst[i,j]=new
cv2.imshow('dst',dst)
cv2.waitKey(0)
#简单的示例(用公式)
#rgb-》RGB new “蓝色”
#b=b*1.5
#g=g*1.3
import cv2
import numpy as np
import random
import math
img = cv2.imread(r"C:\Users\Administrator\Desktop\222\233.jpg",1)
height=img.shape[0]
width=img.shape[1]
#dst
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)
#0-255 分等级4,8个等级等 如分成四个灰度等级 1:0-63 2:64-127 3:128:191 4:192-255
#对灰度图分成小块 如7*7 10*10,记录块内划分到最多的灰度等级
#当前像素的颜色用这个最多的等级计算得出(如取块内最后一个属于这个灰度等级的原图像素颜色)
import cv2
import numpy as np
import random
import math
img = cv2.imread(r"C:\Users\Administrator\Desktop\222\233_mini.jpg",1)
height=img.shape[0]
width=img.shape[1]
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#dst
#当前小块为8*8
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)#当前颜色分为8个等级段
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('img',img)
cv2.imshow('dst',dst)
cv2.waitKey(0)
import cv2
import numpy as np
newImageInfo=(500,500,3)
dst = np.zeros(newImageInfo,np.uint8)
#line
#绘制线段 1 dst 2 begin 3 end 4 color
cv2.line(dst,(100,100),(400,400),(255,0,0))
cv2.line(dst,(100,200),(400,200),(255,255,0),20)#最后一个参数线条宽度
cv2.line(dst,(100,300),(400,300),(0,255,0),cv2.LINE_AA)#防锯齿化
cv2.imshow('dst',dst)
cv2.waitKey(0)
import cv2
import numpy as np
newImageInfo=(500,500,3)
dst = np.zeros(newImageInfo,np.uint8)
#绘制矩形 1:dst 2:左下角 3:右下角 4 颜色 5 是否fill -1为填充 若大于零则为线条宽度
cv2.rectangle(dst,(50,100),(200,300),(255,0,0),-1)
#绘制圆形 圆心 半径
cv2.circle(dst,(250,250),(50),(0,255,0),2)
#圆弧 center 轴 angle begin end
cv2.ellipse(dst,(256,256),(150,100),0,0,180,(255,255,0),-1)
#任意多边形
points =np.array([[150,50],[140,140],[200,170],[250,250],[150,50]],np.uint8)
print(points.shape)
points = points.reshape((-1,1,2))
print(points.shape)
cv2.polylines(dst,np.int32([points]),True,(0,255,255))
cv2.imshow('dst',dst)
cv2.waitKey(0)
(5, 2)
(5, 1, 2)
import cv2
import numpy as np
dst = cv2.imread(r"C:\Users\Administrator\Desktop\222\233.jpg",1)
height=dst.shape[0]
width=dst.shape[1]
font = cv2.FONT_HERSHEY_SIMPLEX
#绘制矩形 1:dst 2:左下角 3:右下角 4 颜色 5 是否fill -1为填充 若大于零则为线条宽度
cv2.rectangle(dst,(50,100),(200,300),(255,0,0),3)
#1dst 2 文字 3 坐標 4 字体 5 字体大小 6 颜色 7 粗细 8 线条类型
cv2.putText(dst,'this is a XXX',(100,300),font,1,(200,100,255),2,cv2.LINE_AA)
cv2.imshow('dst',dst)
cv2.waitKey(0)
import cv2
dst = cv2.imread(r"C:\Users\Administrator\Desktop\222\333.jpg",1)
height=int(dst.shape[0]*0.2)
width=int(dst.shape[1]*0.2)
dstResize=cv2.resize(dst,(width,height))
for i in range(0,height):
for j in range(0,width):
dst[i+100,j+50]=dstResize[i,j]
cv2.imshow('dst',dst)
cv2.waitKey(0)