当前位置:   article > 正文

Python cv模块实际应用_cv模块功能python

cv模块功能python
  • 主要列出一些cv模块的实际应用代码,不做详细的解释

常用功能

彩色图像的边缘检测

  1. import cv2
  2. img = cv2.imread("1.PNG")
  3. gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  4. edges = cv2.Canny(gray_img, 100, 200)
  5. cv2.imwrite("edges2.jpg", edges)

 

 

图像读取和显示

  1. import cv2
  2. img = cv2.imread('2.png')
  3. cv2.imshow('image', img)
  4. cv2.waitKey(0)
  5. cv2.destroyAllWindows()

图像裁剪

  1. import cv2
  2. img = cv2.imread('2.png')
  3. crop_img = img[100:200,100:200]
  4. cv2.imshow('image',crop_img)
  5. cv2.waitKey(0)
  6. cv2.destroyAllWindows()

图像模糊

  1. import cv2
  2. img = cv2.imread('2.png')
  3. blur_img = cv2.GaussianBlur(img, (5, 5), 10)
  4. cv2.imshow('image', blur_img)
  5. cv2.waitKey(0)
  6. cv2.destroyAllWindows()

图像旋转

  1. import cv2
  2. image = cv2.imread("0.jpg")
  3. (h, w) = image.shape[:2]
  4. center = (w//2,h//2)
  5. angle = 45
  6. M = cv2.getRotationMatrix2D(center, angle, 1.0)
  7. rotated = cv2.warpAffine(image, M, (w, h))
  8. cv2.imshow("Rotated Image", rotated)
  9. cv2.waitKey(0)
  10. cv2.destroyAllWindows()

图像缩放

  1. import cv2
  2. image = cv2.imread("0.jpg")
  3. scale = 0.5
  4. resized = cv2.resize(image, None, fx=scale, fy=scale, interpolation=cv2.INTER_LINEAR) # 缩放图像
  5. cv2.imshow("Resized Image", resized)
  6. cv2.waitKey(0)
  7. cv2.destroyAllWindows()
  8. scale = 2
  9. resized = cv2.resize(image, None, fx=scale, fy=scale, interpolation=cv2.INTER_LINEAR) # 缩放图像
  10. cv2.imshow("Resized Image", resized)
  11. cv2.waitKey(0)
  12. cv2.destroyAllWindows()

图像按边缘提取分割

  1. import cv2
  2. image = cv2.imread("0.jpg")
  3. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  4. thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
  5. contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
  6. for contour in contours:
  7. (x, y, w, h) = cv2.boundingRect(contour)
  8. cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
  9. cv2.imshow("Segmented Image", image)
  10. cv2.waitKey(0)
  11. cv2.destroyAllWindows()
  12. cv2.imwrite("3.jpg", image)

 

寻找目标

  1. import cv2
  2. target_img = cv2.imread('1.jpg')
  3. shoot_img = cv2.imread('0.jpg')
  4. target_gray = cv2.cvtColor(target_img, cv2.COLOR_BGR2GRAY)
  5. shoot_gray = cv2.cvtColor(shoot_img, cv2.COLOR_BGR2GRAY)
  6. target_height, target_width = target_gray.shape[:2]
  7. res = cv2.matchTemplate(shoot_gray, target_gray, cv2.TM_CCOEFF_NORMED)
  8. min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
  9. #print("locate", max_loc)
  10. top_left = max_loc
  11. bottom_right = (top_left[0] + target_width, top_left[1] + target_height)
  12. cv2.rectangle(shoot_img,top_left, bottom_right, (0, 0, 255), 2)
  13. cv2.imshow('detected', shoot_img)
  14. cv2.waitKey(0)
  15. cv2.imwrite("detected.jpg",shoot_img)

 

 

 

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

闽ICP备14008679号