将cv2.seamlessClone与python一起使用时调整大小错误

时间:2018-12-21 08:31:40

标签: python opencv resize

我想在男人的鼻子上放一个百搭的鼻子,同时根据男人的鼻子调整鼻子的大小。

man

nose

我想将鼻子调整到男人的鼻子,下面的鼻子太大。我使用cv2.resize,但是会导致一些错误。

man_with_nose

我试图改变调整joker_nose.jpg大小的方法,但这无济于事。

代码如下:

def get_noses(image):
    nose_cascade = cv2.CascadeClassifier("haarcascade_mcs_nose.xml")
    nose_cascade.load("D:\SoftWare\opencv\sources\data\haarcascades\haarcascade_mcs_nose.xml")

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    noses = nose_cascade.detectMultiScale(
        image,
        scaleFactor=1.3,
        minNeighbors=2,
        minSize=(10, 10),
        flags=cv2.CASCADE_SCALE_IMAGE
    )

    return noses

def put_joker_nose_to_face(image):
    nose_pic = cv2.imread(r"./joker_nose.jpg")

    # below line is the error causing part. If I comment this line, it's ok for
    # me put the nose to the man except the nose is too big
# ----------------------------------------------------------------
    temp_img = cv2.resize(nose_pic, (0,0), fx=0.5, fy=0.5) 
# ----------------------------------------------------------------

    # Create an all white mask
    mask = 255 * np.ones(nose_pic.shape, nose_pic.dtype)

    for nose in get_noses(image):
        x, y, w, h = nose
        print(x, y, w, h)

        nose_center = (int(x+w/2), int(y+h/2))
        image = cv2.seamlessClone(temp_img, image, mask, nose_center, cv2.NORMAL_CLONE)

    return image

src_img = cv2.imread(r"C:\Users\Administrator\Desktop\5.jpg", -1)

cv2.imwrite("man_with_joker_nose.jpg", put_joker_nose_to_face(src_img))

错误在下面

Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\put_hat_to_man.py", line 113, in <module>
    cv2.imwrite("man_with_hat.jpg", put_joker_nose_to_face(src_img))
  File "C:\Users\Administrator\Desktop\put_hat_to_man.py", line 84, in put_joker_nose_to_face
    image = cv2.seamlessClone(temp_img, image, mask, nose_center, cv2.NORMAL_CLONE)
cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\core\src\matrix.cpp:465: error: (-215:Assertion failed) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function 'cv::Mat::Mat'

我不是说英语的人,我会尽力而为。预先感谢。

似乎我无法发布图像,因此我将图像设置为解决方法的链接。

0 个答案:

没有答案