案例#

1.图片修复

opencv中给我们提供了一个用于修复图片的函数

  1. cv.inpaint(src,inpaintMask,inpaintRadius,flags)
  2. src : 表示输入的图像
  3. inpaintMask: 掩膜,其实就是要修复哪些区域
  4. inpaintRadius: 表示修复半径
  5. flags: 表示修复时所使用的算法,有CV_INPAINT_TELEACV_INPAINT_NS可选,处理效果差不多

下面这里有一张我的示例图像,左边为待修复的图像,右边为修复之后的图像
opencv笔试题 - 图1

鼠标控制示例:

  1. src = cv.imread("img/lena.jpg")
  2. cv.imshow("src",src)
  3. def onMouse(e,x,y,flags,a):
  4. print(e,x,y,flags,a)
  5. cv.setMouseCallback("src",onMouse,param=[23])
  6. cv.waitKey()

参考示例代码:

  1. import cv2 as cv
  2. import numpy as np
  3. src = cv.imread("img/itheima_inpaint.jpg")
  4. cv.imshow("src",src)
  5. height,width = src.shape[0:2]
  6. inpaintMask = np.zeros((height,width), np.uint8)
  7. def onMouse(e,x,y,flags,a):
  8. global src
  9. # print(e,x,y,flags,a)
  10. if e == cv.EVENT_MOUSEMOVE and flags == cv.EVENT_FLAG_LBUTTON:
  11. cv.circle(inpaintMask,(x,y),10,255,-1)
  12. elif e == cv.EVENT_LBUTTONUP:
  13. # 修复图像
  14. src = cv.inpaint(src,inpaintMask,5,cv.INPAINT_TELEA)
  15. # 更新图像
  16. cv.imshow("src",src)
  17. cv.setMouseCallback("src",onMouse,param=[23])
  18. cv.waitKey()

2.图像切边

opencv笔试题 - 图2

  1. import cv2 as cv
  2. img = cv.imread("img/01_qiebian.jpg")
  3. cv.imshow("img",img)
  4. def fetchROI(img):
  5. # 将彩色图转成灰度图
  6. gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
  7. # 对图像进行二值化
  8. _,binary = cv.threshold(gray,0,255,cv.THRESH_BINARY_INV|cv.THRESH_OTSU)
  9. cv.imshow("binary",binary)
  10. # 查找最大的轮廓
  11. contours,_ = cv.findContours(binary,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)
  12. height,width = img.shape[0:2]
  13. rect = None
  14. for i,c in enumerate(contours):
  15. # 找到最大的轮廓矩形
  16. center,shape,angle = cv.minAreaRect(c)
  17. cv.circle(img,(int(center[0]),int(center[1])),3,(0,255,0))
  18. if shape[1] > width*0.75:
  19. rect = cv.boundingRect(c)
  20. x,y,w,h = rect
  21. return img[y:y+h,x:x+w]
  22. dst = fetchROI(img)
  23. cv.imshow("img",img)
  24. cv.imshow("dst",dst)
  25. cv.waitKey()

3.切边加旋转

opencv笔试题 - 图3

  1. import cv2 as cv
  2. img = cv.imread("img/02_qiebian.jpg")
  3. cv.imshow("img",img)
  4. def fetchROI(img):
  5. # 将彩色图转成灰度图
  6. gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
  7. # 对图像进行二值化
  8. _,binary = cv.threshold(gray,0,255,cv.THRESH_BINARY_INV|cv.THRESH_OTSU)
  9. cv.imshow("binary",binary)
  10. # 查找最大的轮廓
  11. contours,_ = cv.findContours(binary,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)
  12. height,width = img.shape[0:2]
  13. rect = None
  14. for i,c in enumerate(contours):
  15. # 找到最大的轮廓矩形
  16. center,shape,angle = cv.minAreaRect(c)
  17. cv.circle(img,(int(center[0]),int(center[1])),3,(0,255,0))
  18. if shape[1] > width*0.75:
  19. rect = cv.boundingRect(c)
  20. x,y,w,h = rect
  21. return img[y:y+h,x:x+w]
  22. def rotaIMG(img):
  23. # 将彩色图转成灰度图
  24. gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
  25. # 对图像进行二值化
  26. _, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
  27. cv.imshow("binary", binary)
  28. # 查找最大的轮廓
  29. contours, _ = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
  30. height, width = img.shape[0:2]
  31. center, shape, angle = None,None,None
  32. for i, c in enumerate(contours):
  33. # 找到最大的轮廓矩形
  34. center1, shape1, angle1 = cv.minAreaRect(c)
  35. cv.circle(img, (int(center1[0]), int(center1[1])), 3, (0, 255, 0))
  36. if shape1[1] > width * 0.75:
  37. center, shape, angle = center1, shape1, angle1
  38. # 将图像旋转正确
  39. M = cv.getRotationMatrix2D((int(width/2),int(height/2)),angle,1)
  40. print(shape)
  41. # 旋转
  42. img = cv.warpAffine(img,M,(width,height))
  43. cv.imshow("img",img)
  44. return img
  45. img = rotaIMG(img)
  46. dst = fetchROI(img)
  47. cv.imshow("img",img)
  48. cv.imshow("dst",dst)
  49. cv.waitKey()

4.直线检测

opencv笔试题 - 图4

  1. import cv2 as cv
  2. import numpy as np
  3. img = cv.imread("img/engline.jpg")
  4. gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
  5. thresh,binary = cv.threshold(gray,0,255,cv.THRESH_BINARY_INV|cv.THRESH_OTSU)
  6. kernel = cv.getStructuringElement(cv.MORPH_RECT,(20,1))
  7. morph = cv.morphologyEx(binary,cv.MORPH_OPEN,kernel)
  8. kernel = cv.getStructuringElement(cv.MORPH_RECT,(3,3))
  9. morph = cv.dilate(morph, kernel)
  10. lines = cv.HoughLinesP(morph,1,np.pi/180,30,20,0)
  11. for line in lines:
  12. x1,y1,x2,y2 = line[0]
  13. cv.line(img,(x1,y1),(x2,y2),(0,0,255),1)
  14. cv.imshow("img",img)
  15. cv.waitKey()

5.零件计算

opencv笔试题 - 图5

  1. import cv2 as cv
  2. img = cv.imread("img/lingjian.png")
  3. cv.imshow("img",img)
  4. # 将彩色图转成灰度图
  5. gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
  6. # 对图像进行二值化
  7. _, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_TRIANGLE)
  8. cv.imshow("binary", binary)
  9. dist = cv.distanceTransform(binary,cv.DIST_L2,3)
  10. dist = cv.normalize(dist,0,1,norm_type=cv.NORM_MINMAX)
  11. cv.imshow("dst",dist)
  12. _,dist2 = cv.threshold(dist,0.5,1.0,cv.THRESH_BINARY)
  13. cv.imshow("dist2",dist2)
  14. dist2 = cv.convertScaleAbs(dist2,cv.CV_8UC1)
  15. contours,_ = cv.findContours(dist2,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)
  16. print("零件总数:",len(contours))
  17. import numpy as np
  18. np.random.seed(123456)
  19. dst = np.zeros_like(img)
  20. # 绘制轮廓
  21. for i,c in enumerate(contours):
  22. cv.drawContours(dst,contours,i,(np.random.randint(0,255),np.random.randint(0,255),np.random.randint(0,255)),-1)
  23. cv.imshow("idstg",dst)
  24. cv.waitKey()