当前位置:   article > 正文

CV2-个人参考_opencv cv2.minmaxloc

opencv cv2.minmaxloc

 找出图像中最大和最小像素:

              cv2.minMaxLoc() 

  1. # -*- coding:utf-8 -*-
  2. import cv2
  3. img = cv2.imread('C:\\Users\\wu\\Desktop\\111VS.png') # 读取图片
  4. img = cv2.resize(img, (1000, 800)) # 裁剪图片
  5. imgContour = img.copy() # 复制图片
  6. img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 图片二值化
  7. minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(img_gray) # 获取最大最小像素及坐标
  8. cv2.drawMarker(img, minLoc, (0, 255, 0), markerType=0) # 绘制 0 + 十字
  9. cv2.drawMarker(img, maxLoc, (255, 0, 255), markerType=0)
  10. cv2.putText(img, text="min", org=minLoc, fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, color=(255, 0, 0), thickness=2)
  11. # putText各参数依次是:图片,添加的文字,左上角坐标,字体,字体大小,颜色黑,字体粗细
  12. cv2.putText(img, text="max", org=maxLoc, fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, color=(255, 0, 255),
  13. thickness=2)
  14. print('最小值:{}, 位置:{}'.format(minVal, minLoc))
  15. print('最大值:{}, 位置:{}'.format(maxVal, maxLoc))
  16. cv2.imshow("Image", img)
  17. cv2.waitKey(0)

图像阈值处理 

 cv2.threshold()

  1. # -*- coding:utf-8 -*-
  2. import cv2
  3. import numpy as np
  4. img = cv2.imread('C:\\Users\\wu\\Desktop\\test.bmp') # 读取图片
  5. img = cv2.resize(img, (300, 200)) # 裁剪图片
  6. imgContour = img.copy() # 复制图片
  7. gray = cv2.cvtColor(imgContour, cv2.COLOR_BGR2GRAY) # 图片二值化
  8. # 图像阈值处理
  9. ret, thresh1 = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
  10. ret, thresh2 = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
  11. ret, thresh3 = cv2.threshold(gray, 127, 255, cv2.THRESH_TRUNC)
  12. ret, thresh4 = cv2.threshold(gray, 127, 255, cv2.THRESH_TOZERO)
  13. ret, thresh5 = cv2.threshold(gray, 127, 255, cv2.THRESH_TOZERO_INV)
  14. ret, thresh6 = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
  15. ret, thresh7 = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
  16. ret, thresh8 = cv2.threshold(gray, 127, 255, cv2.THRESH_TRUNC + cv2.THRESH_OTSU)
  17. ret, thresh9 = cv2.threshold(gray, 127, 255, cv2.THRESH_TOZERO + cv2.THRESH_OTSU)
  18. ret, thresh10 = cv2.threshold(gray, 127, 255, cv2.THRESH_TOZERO_INV + cv2.THRESH_OTSU)
  19. ret, thresh11 = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY + cv2.THRESH_TRIANGLE)
  20. ret, thresh12 = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_TRIANGLE)
  21. ret, thresh13 = cv2.threshold(gray, 127, 255, cv2.THRESH_TRUNC + cv2.THRESH_TRIANGLE)
  22. ret, thresh14 = cv2.threshold(gray, 127, 255, cv2.THRESH_TOZERO + cv2.THRESH_TRIANGLE)
  23. ret, thresh15 = cv2.threshold(gray, 127, 255, cv2.THRESH_TOZERO_INV + cv2.THRESH_TRIANGLE)
  24. imgHor1 = np.hstack((thresh1, thresh2, thresh3, thresh4))
  25. imgHor2 = np.hstack((thresh5, thresh6, thresh7, thresh8))
  26. imgHor3 = np.hstack((thresh9, thresh10, thresh11, thresh12))
  27. imgHor4 = np.hstack((thresh13, thresh14, thresh15, gray))
  28. Image = np.vstack((imgHor1, imgHor2,imgHor3,imgHor4))
  29. cv2.imshow("Image", Image)
  30. cv2.waitKey(0)

 

轮廓提取

cv2.findContours()

  1. # -*- coding:utf-8 -*-
  2. import cv2
  3. import numpy as np
  4. img = cv2.imread('C:\\Users\\wu\\Desktop\\111VS.png') # 读取图片
  5. img = cv2.resize(img, (1000, 800)) # 裁剪图片
  6. img1 = img.copy()
  7. img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 图片二值化
  8. (_, im_bw) = cv2.threshold(img_gray, 100, 255,
  9. cv2.THRESH_BINARY | cv2.THRESH_OTSU) # 阈值处理 凸包cv2.THRESH_OTSU cv2.THRESH_TRIANGLE
  10. (cnts, _) = cv2.findContours(im_bw.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) # 提取轮廓
  11. cnt = max(cnts, key=cv2.contourArea) # 提取最大轮廓
  12. cv2.drawContours(img, cnt, -1, (128, 0, 128), 2) # 绘制轮廓 -1 全部绘制 颜色 线条粗细
  13. hull = cv2.convexHull(cnt, returnPoints=True) # 凸包
  14. cv2.polylines(img, [hull], True, (128, 0, 128), 2) # 绘制凸包
  15. (_, im_bw) = cv
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/79136
推荐阅读
相关标签
  

闽ICP备14008679号