import cv2
import numpy as np
####灰度处理:图像运算基础,强调实时性####
#定点快于浮点,+-快于*/ 移位快于*/
#方法1.imread
img0 = cv2.imread('image0.jpg',0)
img1 = cv2.imread('image0.jpg',1)
imgInfo = img1.shape
height = imgInfo[0]
width = imgInfo[1]
#方法2.cvtColor
dst = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)#完成颜色空间转换para1.原始数据para2.颜色转换方式
#方法3.灰度图像若为RGB则R=G=B,即颜色值=R+G+B)/3
dst2 = 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]
gray = (int(b)+int(g)+int(r))/3#uint8转换为int类型防止计算过程数据溢出
dst2[i,j] = np.uint8(gray)
#方法4.心理学计算公式:gray=r*0.299+g*0.587+b*0.114
dst2 = 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]
gray = int(b)*0.114+int(g)*0.587+int(r)*0.299
dst2[i,j] = np.uint8(gray)
#方法4优化,浮点运算转定点,给当前每个值乘与一个4,提高运算,存在误差
#gray = (r*1+g*2+b*1)/4-->gray = (r+(g<<1)+b)>>2
######################################################
import cv2
import numpy as np
###颜色反转:灰度图和彩图,255-当前灰度值
#灰度图的颜色反转
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
#彩色图的颜色反转
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)
####马赛克效果####
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):
#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)
###毛玻璃效果:某个像素点周围随机的像素点替换掉当前的像素点####
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)
#####图片融合:dst=src1*a+src2*(1-a)(0<a<1)####
img0 = cv2.imread('image0.jpg',1)
img1 = cv2.imread('image1.jpg',1)
imgInfo = img0.shape
height = imgInfo[0]
width = imgInfo[1]
#ROI感兴趣范围,roi的w和h要比原来两张图的h和w都小
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)
####边缘检测:卷积运算,canny,sobel#####
#1灰度图像处理,2高斯滤波,3canny方法
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)
imgG = cv2.GaussianBlur(gray,(3,3),0)#高斯滤波
dst = cv2.Canny(img,50,50)#1.data 2.图片门限,图片经过卷积运算之后点大于门限则认为是边缘点
##利用sobel算子实现边缘提取##
#sobel 1.算子模板 2.图片卷积 3.阈值判决
img =cv2.imread('image0.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
#[1 2 1][1 0 -1]第一个为竖直方向的算子,第二个为水平,竖直方向的算子和图片进行卷积后得到的
#[0 0 0][2 0 -2]是竖直方向的梯度,sqrt(竖直梯度平方+水平梯度平方)=幅值,幅值与判决门限进行比较
#[-1-2 -1][1 0 -1]大于则是边缘,小于则不是边缘
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]+gray[i,j+1]*2+gray[i,j+2]-gray[i+2,j]-gray[i+2,j+1]*2-gray[i+2,j+2]
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