OpenCV - Blob/ Defect/ Anomaly Detection - Continued - Draw Contours

谁说胖子不能爱 提交于 2020-12-15 03:43:40

问题


OpenCV - Blob/ Defect/ Anomaly Detection

If you need the image it's in the original link above. My current code is as follows,

import cv2
import numpy as np
import imutils


# Read in the image in YCrCb, also show default
img = cv2.imread('/home/pi/Downloads/divot1.jpeg', cv2.IMREAD_COLOR)
imgS = cv2.resize(img, (768, 1024))


aimg = cv2.imread('/home/pi/Downloads/divot1.jpeg', cv2.COLOR_BGR2YCrCb)

blur = cv2.blur(aimg,(6,6))
rblur = cv2.resize(blur, (384, 512))


#Statistics based approach
mean = np.mean(blur, axis=(0,1))
std = np.std(blur, axis=(0,1))
mask = (np.abs(blur - mean)/ std >=4).any(axis=2)
mask_u8 = mask.astype(np.uint8) * 255
mask_u8C = mask_u8.copy()



contours, _ = cv2.findContours(mask_u8, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
print (contours)


#calculates center of object
#M = cv2.moments(c)
#cX = int(M["m10"] / M["m00"])
#cY = int(M["m01"] / M["m00"])
#draws the contour around and finds notates center
cv2.drawContours(mask_u8C, contours, -1, (128, 255, 0), 2)
#cv2.circle(np.float32(mask_u8), (cX, cY), 5, (0, 0, 255), -1)
#cv2.putText(np.float32(mask_u8), "Center", (cX - 30, cY - 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)

#resizes mask to match blurred image size
rmask_u8C = cv2.resize(mask_u8C, (768, 1024))
cv2.imshow("Mask", rmask_u8C)
cv2.imshow("Blur", rblur)
cv2.imshow("Natural", imgS)
cv2.waitKey(0)
cv2.destroyAllWindows()

Can anyone tell me why it won't draw the contour around the white shape? Is the mask being applied at the end as well once the contours are being drawn?

来源:https://stackoverflow.com/questions/65163149/opencv-blob-defect-anomaly-detection-continued-draw-contours

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!