如何使用c ++清除OpenCV中的白色背景? [英] How do I clear a white background in OpenCV with c++?

查看:226
本文介绍了如何使用c ++清除OpenCV中的白色背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此程序显示具有图像的连续帧。

This program shows sequential frames with images.

然而,如您所见,蠕虫图像具有白色背景。

However, as you see, the worm image has a white background.

但是我已经剪切了蠕虫图像的背景,因此当前的蠕虫图像背景是透明的。

But I already cut the worm image's background, So the current worm images background is transparent.

我想透明地处理蠕虫图像的背景并显示蠕虫图像不是灰色而是多彩。

I wants to process the worm image's background transparently and show the worm image not gray but colorful.

我试图编辑成cvtColor(图片,srcBGR,CV_BGR2BGRA)但是,发生了错误。

I tried to edit into cvtColor(image, srcBGR, CV_BGR2BGRA), however, occured error.

以下是代码。

#include<opencv2/core.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/imgproc.hpp>
#include<iostream>
#include<vector>

using namespace std;
using namespace cv;

int main(){
    VideoCapture cap;
    cap.open(0);

    if(!cap.isOpened()){
        cerr << "Error opening the webcam!" << endl;
        return -1;
    }
    Mat image = imread("images/worm.png", 0);
    cv::resize(image,image,Size(70, 120));  
    Mat frame;
    while(1){
        cap >> frame; 
        Mat newFrame = frame.clone();
        int cx = (newFrame.cols - 70) / 2;
        if (!image.empty()) {
            // Get a BGR version of the face, since the output is BGR color
            Mat srcBGR = Mat(image.size(), CV_8UC3);
            cvtColor(image, srcBGR, CV_GRAY2BGR);
            // Get the destination ROI (and make sure it is within the image)
            Rect dstRC = Rect(cx, newFrame.rows/2, 70, 120);
            Mat dstROI = newFrame(dstRC);
            // Copy the pixels from src to dst.
            srcBGR.copyTo(dstROI);  
        }
        imshow("frame", newFrame);
        char key = (char) waitKey(30);
        // Exit this loop on escape:
        if(key == 27)
            break;
    }   

    return 0;
}


推荐答案

试试这个:

#include <windows.h>
#include <iostream>
#include <vector>
#include <stdio.h>
#include "fstream"
#include "iostream"
#include <algorithm>
#include <iterator>
#include "opencv2/opencv.hpp"

using namespace std;
using namespace cv;

//-----------------------------------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------------------------------
int main(int argc, unsigned int** argv)
{
    Mat img = imread("background.jpg", 1);
    if (img.empty())
    {
        cout << "Can't read image." << endl;
        return 0;
    }

    Mat overlay = imread("overlay.png", -1);

    if (overlay.empty())
    {
        cout << "Can't read overlay image." << endl;
        return 0;
    }


    Rect target_roi(0,0,img.cols,img.rows); // Set here, where to place overlay.

    cv::resize(overlay, overlay, Size(target_roi.width, target_roi.height));

    Mat mask;
    if (overlay.channels() == 4)
    {
        vector<Mat> ch;
        split(overlay, ch);
        mask = 255-ch[3].clone();
        mask.convertTo(mask, CV_32FC1, 1.0 / 255.0);
        ch.erase(ch.begin()+3);
        merge(ch, overlay);
    }

    else
    {
        if (overlay.channels() == 3)
        {
            cvtColor(overlay, overlay, COLOR_BGR2GRAY);
        }

        overlay.convertTo(mask, CV_32FC1, 1.0 / 255.0);
    }


    for (int i = 0; i < overlay.rows; ++i)
    {
        for (int j = 0; j < overlay.cols; ++j)
        {
            float blending_coeff = mask.at<float>(i, j);

            Vec3b v1 = img.at<Vec3b>(i + target_roi.y, j + target_roi.x);
            Vec3b v2;
            if (overlay.channels() == 1)
            {
                int v = overlay.at<uchar>(i, j);
                v2 = (v, v, v);
            }
            else
            {
                v2 = overlay.at<Vec3b>(i, j);
            }

            Vec3f v1f(v1[0], v1[1], v1[2]);
            Vec3f v2f(v2[0], v2[1], v2[2]);

            Vec3f r = v1f*blending_coeff + (1.0 - blending_coeff)*v2f;
            img.at<Vec3b>(i + target_roi.y, j + target_roi.x) = r;
        }
    }

    imshow("mask", img);
    imwrite("result.png", img);
    waitKey();
}

这篇关于如何使用c ++清除OpenCV中的白色背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆