赞
踩
- #彩色图片直方图
- def ImageHist(image,type):
- color = (255,255,255)
- windowName = 'Gray'
- if type == 31:
- color = (255,0,0)
- windowName = 'B Hist'
- elif type == 32:
- color = (0,255,0)
- windowName = 'G Hist'
- elif type == 33:
- color = (0,0,255)
- windowName = 'R Hist'
- #计算图片直方图 para1.图片数据 2.计算直方图通道3,mask模板4,0-255表明水平轴统计多少点
- #5.0-255.0
- hist =cv2.calcHist([image],[0],None,[256],[0.0,255.0])
- minV,maxV,minL,maxL = cv2.minMaxLoc(hist) #直接获取最大最小值坐标和值
- histImg = np.zeros([256,256,3],np.uint8)
- for h in range(256):
- intenNormal = int(hist[h]*256/maxV) #值比较大,归一化到0-256之间
- cv2.line(histImg,(h,256),(h,256-intenNormal),color)#图片是反向绘制的
- cv2.imshow(windowName,histImg)
- return histImg
- img = cv2.imread('image0.jpg',1)
- channels = cv2.split(img) #BGR->B G R
- for i in range(0,3):
- ImageHist(channels[i],31+i)
- cv2.waitKey(0)
- import cv2
- import numpy as np
- ###灰度图 直方图均衡化
- img = cv2.imread('image0.jpg',1)
- gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
- cv2.imshow('gray1',gray)
- dst = cv2.equalizeHist(gray)
- cv2.imshow('grayreslult',dst)
- ###彩色图 直方图均衡化
- img = cv2.imread('image0.jpg',1)
- (b,g,r) = cv2.split(img)# 通道分解
- bH = cv2.equalizeHist(b)
- gH = cv2.equalizeHist(g)
- rH = cv2.equalizeHist(r)
- result =cv2.merge((bH,gH,rH)) #通道合成
- cv2.imshow('color',result)
- #YUV通道均衡化
- img = cv2.imread('image0.jpg',1)
- imgYUV = cv2.cvtColor(img,cv2.COLOR_BGR2YCrCb)
- channelYUV = cv2.split(imgYUV)
- channelYUV[0] =cv2.equalizeHist(channelYUV[0])
- channels =cv2.merge(channelYUV)
- result = cv2.cvtColor(channels,cv2.COLOR_YCrCb2BGR)
- cv2.imshow('YUVresult',result)
- cv2.waitKey(0)
- import cv2
- import numpy as np
- ####图片修补###
- #1.损坏的图片 2.找到一个数组描述损坏图片的损坏部分
- #3.inpoint
- img = cv2.imread('image0.jpg',1)
- for i in range(200,300):
- img[i,200] = (255,255,255)
- img[i,200+1] = (255,255,255)
- img[i,200-1] = (255,255,255)
- for i in range(150,250):
- img[250,i] = (255,255,255)
- img[250+1,i] = (255,255,255)
- img[250-1,i] = (255,255,255)
- cv2.imwrite('damaged.jpg',img)
- img = cv2.imread('damaged.jpg',1)
- cv2.imshow('image',img)
- imgInfo = img.shape
- height = imgInfo[0]
- width = imgInfo[1]
- paint = np.zeros((height,width,1),np.uint8)
- for i in range(200,300):
- paint[i,200] = 255
- paint[i,200+1] = 255
- paint[i,200-1] = 255
- for i in range(150,250):
- paint[250,i] = 255
- paint[250+1,i] = 255
- paint[250-1,i] = 255
- cv2.imshow('paint',paint)
- ##para1.损坏的数组2.mask模板
- imgDst = cv2.inpaint(img,paint,3,cv2.INPAINT_TELEA)
- cv2.imshow('imgDst',imgDst)
- cv2.waitKey(0)
- import cv2
- import numpy as np
- import matplotlib.pyplot as plt
- ###灰度图直方图源码##
- ##中心思想:统计图像中每个像素出现的概率
- img = cv2.imread('image0.jpg',1)
- imgInfo = img.shape
- height = imgInfo[0]
- width = imgInfo[1]
- gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
- count = np.zeros(256,np.float)
- for i in range(0,height):
- for j in range(0,width):
- pixel = gray[i,j]
- index = int(pixel)
- count[index] = count[index]+1
- for i in range(0,255):
- count[i] = count[i]/(height*width)
- x = np.linspace(0,255,256)
- y = count
- plt.bar(x,y,0.9,alpha=1,color='b')
- plt.show()
- cv2.waitKey(0)
- import cv2
- import numpy as np
- import matplotlib.pyplot as plt
- ##灰度直方图均值化源码实现
- #累计概率
- #1 原始概率为0.2,如果为累计概率则为 0.2
- #2 0.3 0.5
- #3 0.1 0.6
- img = cv2.imread('image0.jpg',1)
- cv2.imshow('src',img)
- imgInfo = img.shape
- height = imgInfo[0]
- width = imgInfo[1]
- gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
- count = np.zeros(256,np.float)
- for i in range(0,height):
- for j in range(0,width):
- pixel = gray[i,j]
- index = int(pixel)
- count[index] = count[index]+1
- for i in range(0,255):
- count[i] = count[i]/(height*width)
- #计算累计概率
- sum1 = float(0)
- for i in range(0,256):
- sum1 = sum1+count[i]
- count[i] = sum1
- #计算映射表
- map1 = np.zeros(256,np.uint16)
- for i in range(0,256):
- map1[i] = np.uint16(count[i]*255)
- #映射
- for i in range(0,height):
- for j in range(0,width):
- pixel = gray[i, j]
- gray[i,j] = map1[pixel]
-
- cv2.imshow('result',gray)
- cv2.waitKey(0)
- import cv2
- import numpy as np
- import matplotlib.pyplot as plt
- ##彩色图直方图均值化源码实现
- #操作方法与灰度图相同,将bgr分别处理
- #累计概率
- #1 原始概率为0.2,如果为累计概率则为 0.2
- #2 0.3 0.5
- #3 0.1 0.6
- img = cv2.imread('image0.jpg',1)
- cv2.imshow('src',img)
- imgInfo = img.shape
- height = imgInfo[0]
- width = imgInfo[1]
- count_b = np.zeros(256,np.float)
- count_g = np.zeros(256,np.float)
- count_r = np.zeros(256,np.float)
- for i in range(0,height):
- for j in range(0,width):
- (b,g,r) = img[i,j]
- index_b = int(b)
- index_g = int(g)
- index_r = int(r)
- count_b[index_b] = count_b[index_b]+1
- count_g[index_g] = count_g[index_g]+1
- count_r[index_r] = count_r[index_r]+1
- for i in range(0,255):
- count_b[i] = count_b[i]/(height*width)
- count_g[i] = count_g[i]/(height*width)
- count_r[i] = count_r[i]/(height*width)
- #计算累计概率
- sum_b = float(0)
- sum_g = float(0)
- sum_r = float(0)
- for i in range(0,256):
- sum_b = sum_b+count_b[i]
- sum_g = sum_g+count_g[i]
- sum_r = sum_r+count_r[i]
- count_b[i] = sum_b
- count_g[i] = sum_g
- count_r[i] = sum_r
- #计算映射表
- map_b = np.zeros(256,np.uint16)
- map_g = np.zeros(256,np.uint16)
- map_r = np.zeros(256,np.uint16)
- for i in range(0,256):
- map_b[i] = np.uint16(count_b[i]*255)
- map_g[i] = np.uint16(count_g[i]*255)
- map_r[i] = np.uint16(count_r[i]*255)
- #映射
- 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 = map_b[b]
- g = map_g[g]
- r = map_r[r]
- dst[i,j] = (b,g,r)
- cv2.imshow('dst',dst)
- cv2.waitKey(0)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。