楼主: ReneeBK
2877 12

OpenCV with Python By Example [推广有奖]

  • 1关注
  • 62粉丝

VIP

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49407 个
通用积分
51.8704
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57815 点
帖子
4006
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

1论坛币



关键词:example python ExamP Exam AMPL

本帖被以下文库推荐

沙发
ReneeBK 发表于 2016-3-26 12:37:38 |只看作者 |坛友微信交流群
2.0
  1. import cv2
  2. import numpy as np

  3. img = cv2.imread('../images/input_sharp_edges.jpg')
  4. rows, cols = img.shape[:2]

  5. kernel_identity = np.array([[0,0,0], [0,1,0], [0,0,0]])
  6. kernel_3x3 = np.ones((3,3), np.float32) / 9.0
  7. kernel_5x5 = np.ones((5,5), np.float32) / 25.0

  8. cv2.imshow('Original', img)

  9. output = cv2.filter2D(img, -1, kernel_identity)
  10. cv2.imshow('Identity filter', output)

  11. output = cv2.filter2D(img, -1, kernel_3x3)
  12. cv2.imshow('3x3 filter', output)

  13. output = cv2.filter2D(img, -1, kernel_5x5)
  14. cv2.imshow('5x5 filter', output)

  15. cv2.waitKey()
复制代码

使用道具

藤椅
ReneeBK 发表于 2016-3-26 12:38:26 |只看作者 |坛友微信交流群
  1. import cv2
  2. import numpy as np

  3. img = cv2.imread('../images/input_train.jpg', cv2.IMREAD_GRAYSCALE)
  4. rows, cols = img.shape

  5. sobel_horizontal_1 = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5)
  6. sobel_horizontal_2 = cv2.Sobel(img, cv2.CV_64F, 2, 0, ksize=5)
  7. sobel_horizontal_3 = cv2.Sobel(img, cv2.CV_64F, 3, 0, ksize=5)
  8. sobel_vertical = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5)
  9. laplacian = cv2.Laplacian(img, cv2.CV_64F)
  10. canny = cv2.Canny(img, 50, 240)

  11. cv2.imshow('Original', img)
  12. #cv2.imshow('Sobel horizontal 1', sobel_horizontal_1)
  13. #cv2.imshow('Sobel horizontal 2', sobel_horizontal_2)
  14. #cv2.imshow('Sobel horizontal 3', sobel_horizontal_3)
  15. #cv2.imshow('Sobel vertical', sobel_vertical)
  16. cv2.imshow('Laplacian', laplacian)
  17. cv2.imshow('Canny', canny)

  18. cv2.waitKey()
复制代码

使用道具

板凳
ReneeBK 发表于 2016-3-26 12:39:02 |只看作者 |坛友微信交流群
  1. import cv2
  2. import numpy as np

  3. img = cv2.imread('../images/input_histogram.jpg', 0)
  4. histeq = cv2.equalizeHist(img)

  5. #cv2.imshow('Input', img)
  6. #cv2.imshow('Histogram equalized', histeq)

  7. ##################
  8. # Histogram equalization of color images

  9. img = cv2.imread('../images/input_histogram_color.jpg')

  10. img_yuv = cv2.cvtColor(img, cv2.COLOR_BGR2YUV)
  11. img_yuv[:,:,0] = cv2.equalizeHist(img_yuv[:,:,0])

  12. img_output = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR)

  13. cv2.imshow('Color input image', img)
  14. cv2.imshow('Histogram equalized', img_output)

  15. cv2.waitKey()
复制代码

使用道具

报纸
ReneeBK 发表于 2016-3-26 12:39:52 |只看作者 |坛友微信交流群
  1. # http://lodev.org/cgtutor/filtering.html

  2. import cv2
  3. import numpy as np

  4. #img = cv2.imread('../images/input_sharp_edges.jpg', cv2.IMREAD_GRAYSCALE)
  5. img = cv2.imread('../images/input_tree.jpg')
  6. rows, cols = img.shape[:2]
  7. #cv2.imshow('Original', img)

  8. ###################
  9. # Motion Blur
  10. size = 15
  11. kernel_motion_blur = np.zeros((size, size))
  12. kernel_motion_blur[int((size-1)/2), :] = np.ones(size)
  13. kernel_motion_blur = kernel_motion_blur / size
  14. output = cv2.filter2D(img, -1, kernel_motion_blur)
  15. #cv2.imshow('Motion Blur', output)

  16. ###################
  17. # Sharpening
  18. kernel_sharpen_1 = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
  19. kernel_sharpen_2 = np.array([[1,1,1], [1,-7,1], [1,1,1]])
  20. kernel_sharpen_3 = np.array([[-1,-1,-1,-1,-1],
  21.                              [-1,2,2,2,-1],
  22.                              [-1,2,8,2,-1],
  23.                              [-1,2,2,2,-1],
  24.                              [-1,-1,-1,-1,-1]]) / 8.0
  25. output_1 = cv2.filter2D(img, -1, kernel_sharpen_1)
  26. output_2 = cv2.filter2D(img, -1, kernel_sharpen_2)
  27. output_3 = cv2.filter2D(img, -1, kernel_sharpen_3)
  28. #cv2.imshow('Sharpening', output_1)
  29. #cv2.imshow('Excessive Sharpening', output_2)
  30. #cv2.imshow('Edge Enhancement', output_3)

  31. ###################
  32. # Embossing
  33. img_emboss_input = cv2.imread('../images/input_house.jpg')
  34. kernel_emboss_1 = np.array([[0,-1,-1],
  35.                             [1,0,-1],
  36.                             [1,1,0]])
  37. kernel_emboss_2 = np.array([[-1,-1,0],
  38.                             [-1,0,1],
  39.                             [0,1,1]])
  40. kernel_emboss_3 = np.array([[1,0,0],
  41.                             [0,0,0],
  42.                             [0,0,-1]])
  43. gray_img = cv2.cvtColor(img_emboss_input,cv2.COLOR_BGR2GRAY)
  44. output_1 = cv2.filter2D(gray_img, -1, kernel_emboss_1)
  45. output_2 = cv2.filter2D(gray_img, -1, kernel_emboss_2)
  46. output_3 = cv2.filter2D(gray_img, -1, kernel_emboss_3)
  47. cv2.imshow('Input', img_emboss_input)
  48. cv2.imshow('Embossing - South West', output_1 + 128)
  49. cv2.imshow('Embossing - South East', output_2 + 128)
  50. cv2.imshow('Embossing - North West', output_3 + 128)

  51. ###################
  52. # Erosion and dilation

  53. img = cv2.imread('../images/input_morphology.png',0)
  54. kernel = np.ones((5,5), np.uint8)
  55. img_erosion = cv2.erode(img, kernel, iterations=1)
  56. img_dilation = cv2.dilate(img, kernel, iterations=1)
  57. #cv2.imshow('Input', img)
  58. #cv2.imshow('Erosion', img_erosion)
  59. #cv2.imshow('Dilation', img_dilation)

  60. cv2.waitKey()
复制代码

使用道具

地板
ReneeBK 发表于 2016-3-26 12:41:15 |只看作者 |坛友微信交流群
  1. import cv2
  2. import numpy as np

  3. img = cv2.imread('../images/input_flowers.jpg')
  4. rows, cols = img.shape[:2]

  5. kernel_x = cv2.getGaussianKernel(cols,200)
  6. kernel_y = cv2.getGaussianKernel(rows,200)
  7. kernel = kernel_y * kernel_x.T
  8. mask = 255 * kernel / np.linalg.norm(kernel)
  9. output = np.copy(img)

  10. for i in range(3):
  11.     output[:,:,i] = output[:,:,i] * mask

  12. #cv2.imshow('Original', img)
  13. #cv2.imshow('Vignette', output)

  14. ################
  15. # Shifting the focus

  16. kernel_x = cv2.getGaussianKernel(int(1.5*cols),200)
  17. kernel_y = cv2.getGaussianKernel(int(1.5*rows),200)
  18. kernel = kernel_y * kernel_x.T
  19. mask = 255 * kernel / np.linalg.norm(kernel)
  20. mask = mask[int(0.5*rows):, int(0.5*cols):]
  21. output = np.copy(img)

  22. for i in range(3):
  23.     output[:,:,i] = output[:,:,i] * mask

  24. cv2.imshow('Input', img)
  25. cv2.imshow('Vignette with shifted focus', output)


  26. cv2.waitKey()
复制代码

使用道具

7
ReneeBK 发表于 2016-3-26 12:42:57 |只看作者 |坛友微信交流群
  1. import cv2
  2. import numpy as np

  3. img = cv2.imread('../images/input_bilateral_filter.jpg')
  4. img = cv2.resize(img, None, fx=0.4, fy=0.4, interpolation=cv2.INTER_AREA)

  5. img_gaussian = cv2.GaussianBlur(img, (13,13), 0)
  6. img_bilateral = cv2.bilateralFilter(img, 13, 70, 50)

  7. cv2.imshow('Input', img)
  8. cv2.imshow('Gaussian filter', img_gaussian)
  9. cv2.imshow('Bilateral filter', img_bilateral)
  10. cv2.waitKey()
复制代码

使用道具

8
ReneeBK 发表于 2016-3-26 12:43:40 |只看作者 |坛友微信交流群
  1. import cv2
  2. import numpy as np

  3. img = cv2.imread('../images/input_median_filter.png')
  4. output = cv2.medianBlur(img, 7)
  5. cv2.imshow('Input', img)
  6. cv2.imshow('Median filter', output)
  7. cv2.waitKey()
复制代码

使用道具

9
ReneeBK 发表于 2016-3-26 12:45:08 |只看作者 |坛友微信交流群
  1. import cv2
  2. import numpy as np

  3. left_ear_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_mcs_leftear.xml')
  4. right_ear_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_mcs_rightear.xml')

  5. if left_ear_cascade.empty():
  6.         raise IOError('Unable to load the left ear cascade classifier xml file')

  7. if right_ear_cascade.empty():
  8.         raise IOError('Unable to load the right ear cascade classifier xml file')

  9. img = cv2.imread('../images/input_ear_3.jpg')

  10. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

  11. left_ear = left_ear_cascade.detectMultiScale(gray, 1.3, 5)
  12. right_ear = right_ear_cascade.detectMultiScale(gray, 1.3, 5)

  13. for (x,y,w,h) in left_ear:
  14.     cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 3)

  15. for (x,y,w,h) in right_ear:
  16.     cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 3)

  17. cv2.imshow('Ear Detector', img)
  18. cv2.waitKey()
  19. cv2.destroyAllWindows()
复制代码

使用道具

10
ReneeBK 发表于 2016-3-26 12:46:07 |只看作者 |坛友微信交流群
  1. import cv2
  2. import numpy as np

  3. face_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_frontalface_alt.xml')
  4. eye_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_eye.xml')

  5. if face_cascade.empty():
  6.         raise IOError('Unable to load the face cascade classifier xml file')

  7. if eye_cascade.empty():
  8.         raise IOError('Unable to load the eye cascade classifier xml file')

  9. cap = cv2.VideoCapture(0)
  10. ds_factor = 0.5

  11. while True:
  12.     ret, frame = cap.read()
  13.     frame = cv2.resize(frame, None, fx=ds_factor, fy=ds_factor, interpolation=cv2.INTER_AREA)
  14.     gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

  15.     faces = face_cascade.detectMultiScale(gray, 1.3, 5)
  16.     for (x,y,w,h) in faces:
  17.         roi_gray = gray[y:y+h, x:x+w]
  18.         roi_color = frame[y:y+h, x:x+w]
  19.         eyes = eye_cascade.detectMultiScale(roi_gray)
  20.         for (x_eye,y_eye,w_eye,h_eye) in eyes:
  21.             center = (int(x_eye + 0.5*w_eye), int(y_eye + 0.5*h_eye))
  22.             radius = int(0.3 * (w_eye + h_eye))
  23.             color = (0, 255, 0)
  24.             thickness = 3
  25.             cv2.circle(roi_color, center, radius, color, thickness)

  26.     cv2.imshow('Eye Detector', frame)

  27.     c = cv2.waitKey(1)
  28.     if c == 27:
  29.         break

  30. cap.release()
  31. cv2.destroyAllWindows()
复制代码

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-28 13:51