当前位置:   article > 正文

python opencv的最基础初学

python opencv的最基础初学

滤波

 这个在MATLAB叫点乘,内积不是这个。而且内积是欧几里得空间定义的,矩阵的标准内积

121    75     78

24     204    113

154   104     235

几个取平均

算法里说了nXn的滤波一般是奇数

  1. img = cv2. imread('lenaNoise. png')
  2. cv2. imshow('img', img)
  3. cv2. waitKey(0)
  4. cv2.destroyAllWindows()
  1. #均值滤波
  2. #简单的平均卷积操作
  3. blur = cv2.blur(img, (3, 3))
  4. cv2.imshow('blur', blur)
  5. cv2. waitKey(0)
  6. cv2.destroyAllWindows()
  7. #方框滤波
  8. #基本和均值一样,可以选择归一化
  9. box = cv2. boxFilter(img,-1,(3,3), normalize=True)
  10. cv2. imshow('box', box)
  11. cv2. waitKey(0)
  12. cv2.destroyAllWindows()
  1. #方框滤波
  2. #基本和均值一样,可以选择归一化,容易越界
  3. #越界就是全变为255
  4. box = cv2. boxFilter(img,-1,(3,3), normalize=False)
  5. cv2. imshow('box', box)
  6. cv2.waitKey(0)
  7. cv2.destroyAllWindows()
  1. #高斯滤波
  2. #高斯模糊的卷积核里的数值是满足高斯分布,相当于更重视中间的
  3. aussian = cv2. GaussianBlur(img,(5,5),1)
  4. #1表示权重,就是选取的那个最重要的值,离那个值越远,越不重要,所以比重就越低
  5. cv2. imshow('aussian', aussian)
  6. cv2. waitKey(0)
  7. cv2. destroyAllWindows
  1. #中值滤波
  2. #相当于用中值代替
  3. median = cv2.medianBlur(img, 5)#中值滤波
  4. cv2. imshow('median', median)
  5. cv2.waitKey(0)
  6. cv2. destroyAllWindows()

中值滤波除噪点确实很强,但代价是图像细节损失的最为严重

  1. #展示所有的
  2. res = np. hstack((blur. aussian. median))
  3. #可以换成vstack
  4. cv2. imshow(' meaian vs average'. res)
  5. cv2. waitKey(0)
  6. cv2. destroyAHWindows()

形态学-腐蚀操作

阈值吃力刚刚好啊bushi       It is useful for removing small white noises

 不同的核大小腐蚀程度不一样

  1. img = cv2. imreadC dige. png')
  2. cv2. imshowC itng'. img)
  3. cv2. waiiKey(O)
  4. cv2. destroyAllWindows ()
  5. kernel = np. ones ((3,3), np. uint8)
  6. erosion = cv2. erode(img. kernel. iterations= 2)
  7. cv2. imshow('erosion', erosion)
  8. cv2.waitKey(0)
  9. cv2. destroyAllWindows()
  10. pie = cv2. imread('pie. png')
  11. cv2. imshow('pie'. pie)
  12. cv2. waixKey(0)
  13. cv2. destroyA11Windows()
  14. #集合
  15. kernel = np. ones ((30,30), np. uint8)
  16. erosion_l = cv2. erode(pie, kernel, iterations = 1)
  17. erosion_2 = cv2. erode(pie, kernel, iterations = 2)
  18. erosion_3 = cv2. erode(pie, kernel, iterations = 3)
  19. res = np. hstack((erosion_l, erosion_2,erosion_3))
  20. cv2. imshow('res', res)
  21. cv2.waitKey(0)
  22. cv2. destroyAllWindows ()

 kernel到底是什么? 

形态学-膨胀操作

  1. img = cv2. inread ('dige. png')
  2. cv2. imshow('img'. img)
  3. cv2.waitKey(0)
  4. cv2. destroyAllWindows()
  5. kernel = np. ones((3. 3),np. uint8)
  6. erosion = cv2. erode(img, kernel, iterations = 1)
  7. cv2. imshow(' erosion', erosion)
  8. cv2. waitKey(0)
  9. cv2. desrroyAllWindows()
  10. pie = cv2. imread (' pie. png')
  11. cv2. imshow('pie', pie)
  12. cv2. waitKey(0)
  13. cv2. destroyAllWindows()
  14. kernel = np. ones((30.30). np. uint8)
  15. erosion_l = cv2. erode(pie, kernel, iterations = 1)
  16. erosion_2 = cv2. erode(pie, kernel, iterations = 2)
  17. erosion_3 = cv2. erode(pie, kernel, iterations = 3)
  18. res = np. hstack((erosion_l, erosion_2, erosion_3))
  19. cv2. imshow('res', res)
  20. cv2.waitKey(0)
  21. cv2. destroyAllWindows()

kernel核矩阵越小,越近圆

开运算和闭运算

把腐蚀膨胀合在一起了,用来提取前景或者提取噪音 huh?

  1. #开:先腐 ,再 胀
  2. img = cv2. imread(' dige. png')
  3. kernel = np. ones((5.5), np. uint8)
  4. opening = cv2. morphologyExCimg. cv2. MORHi.WEN. kernel)
  5. cv2. imshow('opening'. opening)
  6. cv2. waitKey(0)
  7. cv2. destroyAllWindows()
  8. #:先房底.再用蚀
  9. img = cv2. imread('dige. png')
  10. kernel = np. ones ((5.5). np. uint8)
  11. closing = cv2. morphologyExdmg. cv2. MORPH_CLOSE, kernel)
  12. cv2. imshow('closing',closing)
  13. cv2. waitKey(O)
  14. cv2. destroyAllWindows()

梯度运算

感觉地方感觉有点问题。膨胀减掉腐蚀的图像应该和梯度操作的结果不太一样。最后的边界的宽度差异很大。梯度 = 原图 - 腐蚀.

  1. #梯度=影胀-腐蚀?
  2. pie = cv2. imread('pie. png')
  3. kernel = np. ones((7,7), np. uint8)
  4. dilate = cv2. di late(pie, kernel, iterations = 5)
  5. erosion = cv2. erode(pie, kernel.iterations = 5)
  6. res = np. hstack((dilate. erosion))
  7. cv2. imshow('res', res)
  8. cv2.wairKey(0)
  9. cv2. destroyAllWindows()
  10. gradient = cv2. morphologyEx(pie,cv2. MORPH_GRADIENT,kernel)
  11. cv2. imshow('gradient', gradient)
  12. cv2. waitKey(0)
  13. cv2. destroyAllWindows()

礼帽和黑帽

剩下毛边和剩下原轮廓

  1. #礼帽=原始输入-开运算结果
  2. #黑帽=闭运算-原始输入
  3. #礼帽
  4. lag = cv2. imread('dige. png')
  5. tophar = cv2. morphologyEx(img. cv2. MORPH_TOPHAT. kernel)
  6. cv2. imsho,(' tophat'. tophat)
  7. cv2. waitKey(O)
  8. cv2. destroyAllWindows()
  9. #黑帽
  10. img = cv2. imread(' dige. png')
  11. blackhat = cv2. morphologyEx(img, cv2. MORPH_BLAG{HAT. kernel)
  12. cv2. imshow('blackhat '. blackhat )
  13. cv2.waitKey(0)
  14. cv2. destroyAllWindows()

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

闽ICP备14008679号