【问题标题】:OpenCV convert one color in to any otherOpenCV 将一种颜色转换为任何其他颜色
【发布时间】:2017-08-10 06:38:34
【问题描述】:

S对不起我的英语不好。我有一个图像,显示 3 个不同颜色的圆圈,一个红色、一个绿色和一个蓝色,我可以将此图像显示到 3 个通道中,但它们显示为白色,在代码中我显示一个名为“复制 R”的图像,我不知道如何将这个副本 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);
}

【问题讨论】:

  • 改变Scalar类型里面的数字,你会得到不同的颜色。
  • 如果你的意思是在 te inrange 操作中改变标量的值,我已经试过了,还是不行

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


【解决方案1】:

从外观上看,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);

【讨论】:

    猜你喜欢
    • 2013-08-15
    • 1970-01-01
    • 2012-09-17
    • 2020-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-14
    相关资源
    最近更新 更多