OpenCV将一种颜色转换为任何其他颜色

时间:2017-03-18 19:37:41

标签: c++ opencv visual-studio-2015 colors

我的英语不好。我有一个图像,显示3个不同颜色的圆圈,一个红色,一个绿色和一个蓝色,我可以将这个图像显示在3个通道中,但它们显示为白色,在代码中我显示图像调用"复制R& #34;而且我不知道如何将这个副本R制作成另一个图像,我希望与原始图像重叠并改变红色。我怎么能这样做?

这是我的代码,对不起是我第一次提出问题并且不知道如何正确发布

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

#define w 400

using namespace cv;

///功能标题

void MyFilledCircle(Mat img, Point center);
void MyFilledCircle1(Mat img, Point center);
void MyFilledCircle2(Mat img, Point center);
int main(void) {

//![create_images]

char window[] = "Original";

/// Create black empty images
Mat image = Mat::zeros(w, w, CV_8UC3);


/// 1.b. Creating circles
MyFilledCircle(image, Point(200, 200));
MyFilledCircle1(image, Point(150, 150));
MyFilledCircle2(image, Point(250, 250));


Mat channel[3];
split(image, channel);

//channel[0] = Mat::zeros(image.rows, image.cols, CV_8UC1);

merge(channel, 3, image);

Mat imageHSV;
Mat copy;


imshow(window, image);
//imshow("Color 1", imageHSV);

inRange(image, Scalar(0, 0, 255), Scalar(0, 0, 255), copy);
imshow("copy R", copy);


imshow("B", channel[0]);
imshow("G", channel[1]);
imshow("R", channel[2]);





//imshow("0", canal0);

//imwrite("dest.jpg", image);




waitKey(0);
return(0);
}

/// Function Declaration




//![myfilledcircle]

void MyFilledCircle1(Mat img, Point center)
{
circle(img,
    center,
    50,
    Scalar(0, 255, 0),
    FILLED,
    LINE_8);
}

void MyFilledCircle(Mat img, Point center)
{
circle(img,
    center,
    50,
    Scalar(0, 0, 255),
    FILLED,
    LINE_8);
}


void MyFilledCircle2(Mat img, Point center)
{
circle(img,
    center,
    50,
    Scalar(255, 0, 0),
    FILLED,
    LINE_8);
}

1 个答案:

答案 0 :(得分:0)

从它的外观来看,copy看起来像是一个二元蒙版,你想在图像上叠加这个蒙版,这样只有蒙版中的非零像素保留其原始颜色。

如果我的假设是正确的,那么使用减法方法,如下所示,应该可以帮助你:

    Mat result;
    cvtColor(copy,copy,CV_GRAY2BGR);//change copy to a 3 channel image
    absdiff(image,image,result);//initialize mask as a black image of img.size()
    subtract(copy,image,result);
    subtract(copy,result,result);