Dlib 裁剪后的图像是蓝色的
Dlib cropped images are blue
我正在使用 D-lib 提取面部的某些区域。我正在使用 opencv 裁剪使用 dlib 地标点检测器检测到的区域。
但是,裁剪后的图像是蓝色的。知道为什么要改变吗?
而且我发现一些图像跳过了这段代码。因此,例如,如果我的源文件夹中有 20 张图像,在 运行 它们通过 dlib 检测器之后,我应该在目标文件夹中获得 40 张结果图像,因为我从每个输入中提取了两张图像。但事实并非如此。我只得到 15-20 张图片。但是它们是程序中的运行,而不是我程序中添加的那些异常。
请在下面找到我的代码:- 并找到附件中的图片。
import sys
import os
import dlib
import glob
from skimage import io
import cv2
predictor_path = "/home[![enter image description here][1]][1]/PycharmProjects/Face_recognition/shape_predictor_68_face_landmarks.dat"
faces_folder_path = "/media/External_HDD/My_files/Datasets"
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)
win = dlib.image_window()
a=[]
number=1
scanned=1
for f in glob.glob(os.path.join(faces_folder_path, "*.png")):
print("Processing file: {}".format(f))
img = io.imread(f)
name=f[-14:-4]
print("Number of images scanned is :",scanned)
scanned=scanned+1
win.clear_overlay()
win.set_image(img)
# Ask the detector to find the bounding boxes of each face. The 1 in the
# second argument indicates that we should upsample the image 1 time. This
# will make everything bigger and allow us to detect more faces.
dets = detector(img, 1)
print("Number of faces detected: {}".format(len(dets)))
if len(dets)>1:
print ("The file has an anomaly")
a.append(name)
print("The number of anomalies detected: {}".format(len(a)))
continue
for k, d in enumerate(dets):
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
k, d.left(), d.top(), d.right(), d.bottom()))
# Get the landmarks/parts for the face in box d.
shape = predictor(img, d)
print("Part 0: {}, Part 1: {} ...".format(shape.part(0),
shape.part(1)))
print ("Part 27: {}, Part 19: {}, Part 0: {}, Part 28: {}".format(shape.part(27),shape.part(19),shape.part(0),shape.part(28)))
left_corner= shape.part(17)
left_x= left_corner.x
left_y=left_corner.y
left_y=left_y-200
center=shape.part(29)
center_x=center.x
center_y=center.y
print (left_x,left_y)
print (center_x,center_y)
right_crop_center_x=center_x
right_crop_center_y=center_y-700
right=shape.part(15)
right_x=right.x
right_x=right_x-200
right_y=right.y
os.chdir("/home/PycharmProjects/cropped")
win.add_overlay(shape)
crop_left= img[left_y:center_y,left_x:center_x]
# cv2.imshow("cropped_left", crop_left)
cv2.imwrite(name + "_crop_left" +".png" ,crop_left)
crop_right=img[right_crop_center_y:right_y,right_crop_center_x:right_x]
# cv2.imshow("cropped_right", crop_right)
cv2.imwrite(name + "_crop_right" +".png",crop_right)
print("Number of images completed is :{}".format(number))
number = number + 1
cv2.waitKey(2)
print len(a)
如评论中所述,您面临的问题是图像中的红色和蓝色通道已互换。要更正此问题,您需要使用 cvtColor
函数。下面是更正此问题的 C++ 代码(我目前无法在 Python 中访问 OpenCV):
#include <cv.h>
#include <highgui.h>
int main()
{
cv::Mat bgrImage = cv::imread("inImage.png");
cv::Mat rgbImage;
cv::cvtColor(bgrImage, rgbImage, CV_BGR2RGB);
cv::imshow("BGR Output", bgrImage);
cv::imshow("RGB Processed", rgbImage);
cv::waitKey(0);
return 1;
}
这是输出图像:
在 python 中包含以下代码行以将图像从 BGR 转换为 RGB
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
请在下面找到我的代码:- 并找到附件中的图片。
import sys
import os
import dlib
import glob
from skimage import io
import cv2
predictor_path = "/home[![enter image description here][1]][1]/PycharmProjects/Face_recognition/shape_predictor_68_face_landmarks.dat"
faces_folder_path = "/media/External_HDD/My_files/Datasets"
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)
win = dlib.image_window()
a=[]
number=1
scanned=1
for f in glob.glob(os.path.join(faces_folder_path, "*.png")):
print("Processing file: {}".format(f))
img = io.imread(f)
name=f[-14:-4]
print("Number of images scanned is :",scanned)
scanned=scanned+1
win.clear_overlay()
win.set_image(img)
# Ask the detector to find the bounding boxes of each face. The 1 in the
# second argument indicates that we should upsample the image 1 time. This
# will make everything bigger and allow us to detect more faces.
dets = detector(img, 1)
print("Number of faces detected: {}".format(len(dets)))
if len(dets)>1:
print ("The file has an anomaly")
a.append(name)
print("The number of anomalies detected: {}".format(len(a)))
continue
for k, d in enumerate(dets):
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
k, d.left(), d.top(), d.right(), d.bottom()))
# Get the landmarks/parts for the face in box d.
shape = predictor(img, d)
print("Part 0: {}, Part 1: {} ...".format(shape.part(0),
shape.part(1)))
print ("Part 27: {}, Part 19: {}, Part 0: {}, Part 28: {}".format(shape.part(27),shape.part(19),shape.part(0),shape.part(28)))
left_corner= shape.part(17)
left_x= left_corner.x
left_y=left_corner.y
left_y=left_y-200
center=shape.part(29)
center_x=center.x
center_y=center.y
print (left_x,left_y)
print (center_x,center_y)
right_crop_center_x=center_x
right_crop_center_y=center_y-700
right=shape.part(15)
right_x=right.x
right_x=right_x-200
right_y=right.y
os.chdir("/home/PycharmProjects/cropped")
win.add_overlay(shape)
crop_left= img[left_y:center_y,left_x:center_x]
# cv2.imshow("cropped_left", crop_left)
cv2.imwrite(name + "_crop_left" +".png" ,crop_left)
crop_right=img[right_crop_center_y:right_y,right_crop_center_x:right_x]
# cv2.imshow("cropped_right", crop_right)
cv2.imwrite(name + "_crop_right" +".png",crop_right)
print("Number of images completed is :{}".format(number))
number = number + 1
cv2.waitKey(2)
print len(a)
如评论中所述,您面临的问题是图像中的红色和蓝色通道已互换。要更正此问题,您需要使用 cvtColor
函数。下面是更正此问题的 C++ 代码(我目前无法在 Python 中访问 OpenCV):
#include <cv.h>
#include <highgui.h>
int main()
{
cv::Mat bgrImage = cv::imread("inImage.png");
cv::Mat rgbImage;
cv::cvtColor(bgrImage, rgbImage, CV_BGR2RGB);
cv::imshow("BGR Output", bgrImage);
cv::imshow("RGB Processed", rgbImage);
cv::waitKey(0);
return 1;
}
这是输出图像:
在 python 中包含以下代码行以将图像从 BGR 转换为 RGB
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)