繁体   English   中英

OpenCV裁剪图像并显示已删除裁剪的原始图像

[英]OpenCV crop image and display the original with crop removed

我有一个如下图像:

在此处输入图片说明

在此图像上,我正在按如下方式运行人脸检测代码:如果要运行此代码,请在此处查看更多详细信息

人脸检测器:

import cv2

# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# Read the input image
img = cv2.imread('5.jpg')

# Convert into grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Detect faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4)

# Draw rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(img, (x-20, y-70), (x + w + 50, y + h + 50), (255, 0, 0), 2)

crop_img = img[y:y+h, x:x+w]


cv2.imwrite("cropped.jpg", crop_img)

作为此代码图像的输出,我得到

在此处输入图片说明

我想要的是:

在此处输入图片说明

在输出中,我需要整个图像,但要去除脸部。 如何达到这个结果?

只需使用numpy覆盖像素:

img[y1:y1+h, x1:x1+w, 0] = 0
img[y1:y1+h, x1:x1+w, 1] = 0
img[y1:y1+h, x1:x1+w, 2] = 0

请注意,numpy具有一个奇怪的索引系统,即行第一,列第二(与通用索引相反)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM