【发布时间】:2020-09-29 10:37:44
【问题描述】:
(已解决,但有一个新问题,请参阅下面的“更新”)我加载了一些用于皮肤检测的图像(请参阅下面的代码),这些新保存的图像的非皮肤区域被涂黑了。我正在研究缩放/轮廓作为预处理的下一步,但是有许多图像超过 80% 或 90% 的照片变成黑色,因为它们一开始就不是人类照片。 (我应该/是否有办法先删除这些图像?我已经查看了许多来源,但仍然没有弄清楚。感谢任何帮助!
min_YCrCb = np.array([0,133,77],np.uint8)
max_YCrCb = np.array([235,173,127],np.uint8)
files = glob.glob ('%s/*.jpg' %(folder))
# read each image
for f in files:
image = cv2.imread(f)
#detect human skin
imageYCrCb = cv2.cvtColor(image,cv2.COLOR_BGR2YCR_CB)
skinRegionYCrCb = cv2.inRange(imageYCrCb,min_YCrCb,max_YCrCb)
skinYCrCb = cv2.bitwise_and(image, image, mask = skinRegionYCrCb)
#write new images to "newimages" folder
cv2.imwrite('../data/newimages/ycrcb%04i.png' %i, np.hstack([skinYCrCb]))
Would like to remove images like these
Suggestion of ways to remove background on images like this? (zooming/contouring etc?)
更新 我已经删除了黑色像素超过 80% 的图片,并希望进行边界框裁剪以删除图像中多余的黑色区域(请参阅附图)。它适用于一张图片,但当我放入 for 循环时不起作用?
files = glob.glob('..data/*.jpg')
i=0
for f in files:
img = cv2.imread(f)
image = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#find contours
contours, _ = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#draw these contours
for cnt in contours:
rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int0(box)
img = cv2.drawContours(img, [box], 0, (0,0,0),1)
x, y, w, h = cv2.boundingRect(box)
crop = img[y:y+h, x:x+w]
cv2.imwrite('../data/images%04i.jpg' %i, crop)
i+=1
【问题讨论】:
-
您的代码不完整,您没有包含输入图像。
-
如何实现皮肤检测?
-
抱歉!我已经更新了代码并附上了一些图片..
-
去除图片背景是什么意思?结果应该是什么?用透明或其他东西代替黑色?
-
谢谢!我正在做边界框裁剪以删除黑色 bkgrd。适用于 1 张图片,但在 for 循环中..(请参阅编辑后的帖子)
标签: python opencv image-processing crop bounding-box