Dlib裁剪图像为蓝色

时间:2016-10-26 16:02:47

标签: python opencv dlib dlib-python

enter image description here我正在使用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)

2 个答案:

答案 0 :(得分:3)

在python中包含以下代码行,用于将图像从BGR转换为RGB

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

答案 1 :(得分:1)

正如评论中所提到的,您面临的问题是图像中的红色和蓝色通道已被交换。要更正此问题,您需要使用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;
}

这是输出图像: enter image description here