当前位置:   article > 正文

Opencv基础:图像美化_opencv美化图片

opencv美化图片

  1. #彩色图片直方图
  2. def ImageHist(image,type):
  3. color = (255,255,255)
  4. windowName = 'Gray'
  5. if type == 31:
  6. color = (255,0,0)
  7. windowName = 'B Hist'
  8. elif type == 32:
  9. color = (0,255,0)
  10. windowName = 'G Hist'
  11. elif type == 33:
  12. color = (0,0,255)
  13. windowName = 'R Hist'
  14. #计算图片直方图 para1.图片数据 2.计算直方图通道3,mask模板4,0-255表明水平轴统计多少点
  15. #5.0-255.0
  16. hist =cv2.calcHist([image],[0],None,[256],[0.0,255.0])
  17. minV,maxV,minL,maxL = cv2.minMaxLoc(hist) #直接获取最大最小值坐标和值
  18. histImg = np.zeros([256,256,3],np.uint8)
  19. for h in range(256):
  20. intenNormal = int(hist[h]*256/maxV) #值比较大,归一化到0-256之间
  21. cv2.line(histImg,(h,256),(h,256-intenNormal),color)#图片是反向绘制的
  22. cv2.imshow(windowName,histImg)
  23. return histImg
  24. img = cv2.imread('image0.jpg',1)
  25. channels = cv2.split(img) #BGR->B G R
  26. for i in range(0,3):
  27. ImageHist(channels[i],31+i)
  28. cv2.waitKey(0)

  1. import cv2
  2. import numpy as np
  3. ###灰度图 直方图均衡化
  4. img = cv2.imread('image0.jpg',1)
  5. gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  6. cv2.imshow('gray1',gray)
  7. dst = cv2.equalizeHist(gray)
  8. cv2.imshow('grayreslult',dst)
  9. ###彩色图 直方图均衡化
  10. img = cv2.imread('image0.jpg',1)
  11. (b,g,r) = cv2.split(img)# 通道分解
  12. bH = cv2.equalizeHist(b)
  13. gH = cv2.equalizeHist(g)
  14. rH = cv2.equalizeHist(r)
  15. result =cv2.merge((bH,gH,rH)) #通道合成
  16. cv2.imshow('color',result)
  17. #YUV通道均衡化
  18. img = cv2.imread('image0.jpg',1)
  19. imgYUV = cv2.cvtColor(img,cv2.COLOR_BGR2YCrCb)
  20. channelYUV = cv2.split(imgYUV)
  21. channelYUV[0] =cv2.equalizeHist(channelYUV[0])
  22. channels =cv2.merge(channelYUV)
  23. result = cv2.cvtColor(channels,cv2.COLOR_YCrCb2BGR)
  24. cv2.imshow('YUVresult',result)
  25. cv2.waitKey(0)

  1. import cv2
  2. import numpy as np
  3. ####图片修补###
  4. #1.损坏的图片 2.找到一个数组描述损坏图片的损坏部分
  5. #3.inpoint
  6. img = cv2.imread('image0.jpg',1)
  7. for i in range(200,300):
  8. img[i,200] = (255,255,255)
  9. img[i,200+1] = (255,255,255)
  10. img[i,200-1] = (255,255,255)
  11. for i in range(150,250):
  12. img[250,i] = (255,255,255)
  13. img[250+1,i] = (255,255,255)
  14. img[250-1,i] = (255,255,255)
  15. cv2.imwrite('damaged.jpg',img)
  16. img = cv2.imread('damaged.jpg',1)
  17. cv2.imshow('image',img)
  18. imgInfo = img.shape
  19. height = imgInfo[0]
  20. width = imgInfo[1]
  21. paint = np.zeros((height,width,1),np.uint8)
  22. for i in range(200,300):
  23. paint[i,200] = 255
  24. paint[i,200+1] = 255
  25. paint[i,200-1] = 255
  26. for i in range(150,250):
  27. paint[250,i] = 255
  28. paint[250+1,i] = 255
  29. paint[250-1,i] = 255
  30. cv2.imshow('paint',paint)
  31. ##para1.损坏的数组2.mask模板
  32. imgDst = cv2.inpaint(img,paint,3,cv2.INPAINT_TELEA)
  33. cv2.imshow('imgDst',imgDst)
  34. cv2.waitKey(0)

  1. import cv2
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. ###灰度图直方图源码##
  5. ##中心思想:统计图像中每个像素出现的概率
  6. img = cv2.imread('image0.jpg',1)
  7. imgInfo = img.shape
  8. height = imgInfo[0]
  9. width = imgInfo[1]
  10. gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  11. count = np.zeros(256,np.float)
  12. for i in range(0,height):
  13. for j in range(0,width):
  14. pixel = gray[i,j]
  15. index = int(pixel)
  16. count[index] = count[index]+1
  17. for i in range(0,255):
  18. count[i] = count[i]/(height*width)
  19. x = np.linspace(0,255,256)
  20. y = count
  21. plt.bar(x,y,0.9,alpha=1,color='b')
  22. plt.show()
  23. cv2.waitKey(0)

  1. import cv2
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. ##灰度直方图均值化源码实现
  5. #累计概率
  6. #1 原始概率为0.2,如果为累计概率则为 0.2
  7. #2 0.3 0.5
  8. #3 0.1 0.6
  9. img = cv2.imread('image0.jpg',1)
  10. cv2.imshow('src',img)
  11. imgInfo = img.shape
  12. height = imgInfo[0]
  13. width = imgInfo[1]
  14. gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  15. count = np.zeros(256,np.float)
  16. for i in range(0,height):
  17. for j in range(0,width):
  18. pixel = gray[i,j]
  19. index = int(pixel)
  20. count[index] = count[index]+1
  21. for i in range(0,255):
  22. count[i] = count[i]/(height*width)
  23. #计算累计概率
  24. sum1 = float(0)
  25. for i in range(0,256):
  26. sum1 = sum1+count[i]
  27. count[i] = sum1
  28. #计算映射表
  29. map1 = np.zeros(256,np.uint16)
  30. for i in range(0,256):
  31. map1[i] = np.uint16(count[i]*255)
  32. #映射
  33. for i in range(0,height):
  34. for j in range(0,width):
  35. pixel = gray[i, j]
  36. gray[i,j] = map1[pixel]
  37. cv2.imshow('result',gray)
  38. cv2.waitKey(0)

  1. import cv2
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. ##彩色图直方图均值化源码实现
  5. #操作方法与灰度图相同,将bgr分别处理
  6. #累计概率
  7. #1 原始概率为0.2,如果为累计概率则为 0.2
  8. #2 0.3 0.5
  9. #3 0.1 0.6
  10. img = cv2.imread('image0.jpg',1)
  11. cv2.imshow('src',img)
  12. imgInfo = img.shape
  13. height = imgInfo[0]
  14. width = imgInfo[1]
  15. count_b = np.zeros(256,np.float)
  16. count_g = np.zeros(256,np.float)
  17. count_r = np.zeros(256,np.float)
  18. for i in range(0,height):
  19. for j in range(0,width):
  20. (b,g,r) = img[i,j]
  21. index_b = int(b)
  22. index_g = int(g)
  23. index_r = int(r)
  24. count_b[index_b] = count_b[index_b]+1
  25. count_g[index_g] = count_g[index_g]+1
  26. count_r[index_r] = count_r[index_r]+1
  27. for i in range(0,255):
  28. count_b[i] = count_b[i]/(height*width)
  29. count_g[i] = count_g[i]/(height*width)
  30. count_r[i] = count_r[i]/(height*width)
  31. #计算累计概率
  32. sum_b = float(0)
  33. sum_g = float(0)
  34. sum_r = float(0)
  35. for i in range(0,256):
  36. sum_b = sum_b+count_b[i]
  37. sum_g = sum_g+count_g[i]
  38. sum_r = sum_r+count_r[i]
  39. count_b[i] = sum_b
  40. count_g[i] = sum_g
  41. count_r[i] = sum_r
  42. #计算映射表
  43. map_b = np.zeros(256,np.uint16)
  44. map_g = np.zeros(256,np.uint16)
  45. map_r = np.zeros(256,np.uint16)
  46. for i in range(0,256):
  47. map_b[i] = np.uint16(count_b[i]*255)
  48. map_g[i] = np.uint16(count_g[i]*255)
  49. map_r[i] = np.uint16(count_r[i]*255)
  50. #映射
  51. dst = np.zeros((height,width,3),np.uint8)
  52. for i in range(0,height):
  53. for j in range(0,width):
  54. (b,g,r) = img[i, j]
  55. b = map_b[b]
  56. g = map_g[g]
  57. r = map_r[r]
  58. dst[i,j] = (b,g,r)
  59. cv2.imshow('dst',dst)
  60. cv2.waitKey(0)

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/268914
推荐阅读
相关标签
  

闽ICP备14008679号