OpenCV:自定义区域/图像作为"背景"的来源。对于GrabCut

时间:2016-10-13 20:43:47

标签: c++ opencv image-processing

我有人类的图像,我想消除一些模式。请看下面的三张图片:

GrabCut从第一张图片中提取了图形(第二张图片),没有任何问题。

现在我有一个对应于脸部的矩形(第二张图片上的圆圈),我想用它作为"背景" (而图像的其余部分将是前景和背景的组合),以消除仅留下衣服的皮肤。

大致预期的结果出现在第三张图片上:

enter image description here

有没有办法让GrabCut这样做?我无法手动分配区域/蒙版,我所拥有的只是面部检测提供的矩形。

UPD:在下面的代码中我尝试使用mask,来做,但是第2阶段似乎不起作用(因为至少应该剪切脸部)。也许我只是不明白它是如何工作的,因为我刚刚修改了另一个例子代码正在运行:

def load():    
    global name
    global count
    global shares
    global pp
    global sp
    global commission
    name=input("Enter stock name OR -999 to Quit: ")
    count =0
    while name != '-999':
        count=count+1
        shares=int(input("Enter number of shares: "))
        pp=float(input("Enter purchase price: "))
        sp=float(input("Enter selling price: "))
        commission=float(input("Enter commission: "))
        name=input("\nEnter stock name OR -999 to Quit: ")

totalpr=0
def calc():
    global amount_paid
    global amount_sold
    global profit_loss
    global commission_paid_sale
    global commission_paid_purchase
    global totalpr
    amount_paid=shares*pp
    commission_paid_purchase=amount_paid*commission
    amount_sold=shares*sp
    commission_paid_sale=amount_sold*commission
    profit_loss=(amount_sold - commission_paid_sale) -(amount_paid + commission_paid_purchase)
    totalpr=totalpr+profit_loss

def display():
    print("\nStock Name:", name)
    print("Amount paid for the stock:       $",      format(amount_paid, '10,.2f'))
    print("Commission paid on the purchase: $", format(commission_paid_purchase, '10,.2f'))
    print("Amount the stock sold for:       $", format(amount_sold, '10,.2f'))
    print("Commission paid on the sale:     $", format(commission_paid_sale, '10,.2f'))
    print("Profit (or loss if negative):    $", format(profit_loss, '10,.2f'))

def main():
    load()
    calc()
    display()

main()

print("\nTotal Profit is $", format(totalpr, '10,.2f'))

1 个答案:

答案 0 :(得分:0)

好的,我发现了错误,而不是

separated.copyTo(separated,mymask); 

最后一行应更改为:

cv::Mat res(separated.size(),CV_8UC3,cv::Scalar(255,255,255));
separated.copyTo(res,mymask);  // bg pixels not copied
//*************************//        
return(res); // return the innerings of assumption rectangle

对于第二次抓取调用,还需要更多的迭代,例如5-7次迭代。

结果不太好,所以我欢迎可以改进它的答案。

results