OpenCV和C ++:更改图像中某些对象的颜色

时间:2018-03-27 15:09:31

标签: c++ opencv

我是OpenCV的新手,我正在尝试用图像做一些简单的操作。

我有这张图片:https://imgur.com/j7mtwYC。首先,当点击左按钮时,我必须计算点击点的9×9邻域上的平均RGB颜色,然后通过对三个通道R,G和B应用静态阈值来分割足球衬衫(ΔR<1。 50,ΔG<50,ΔB<50),最后将新颜色应用于衬衫(RGB =(92,37,201))。这是我的代码:

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

#define NEIGHBORHOOD_SIZE 9
#define THRESH_R 50
#define THRESH_G 50
#define THRESH_B 50
#define THRESH_H 10

using namespace std;
using namespace cv;


static void onMouse (int event, int x, int y, int f, void* userdata) {

    if(event == EVENT_LBUTTONDOWN) {
        cout << "Left button clicked - position (" << x << ", " << y << ")" << endl;

        Mat image = (*(Mat*) userdata);

        //RGB of clicked point
        Vec3b color = image.at<Vec3b>(y,x);
        cout << "R: " << color[2] << " G: " << (int)color[1] << " B: " << (int)color[0] << endl;
        //mean
        Scalar m = mean(image(Rect(Point(x,y), Size(NEIGHBORHOOD_SIZE,NEIGHBORHOOD_SIZE))));
        cout << "Mean: " << "R: " << m[2] << " G: " << m[1] << " B: " << m[0] << endl;

        //color segmentation code to be insert


        Mat image_out = image.clone();
        namedWindow("final_result", CV_WINDOW_AUTOSIZE);
        cv::imshow("final_result", image_out);
        cv::waitKey(0);
  }

}


int main() {

    Mat image = imread("robocup.jpg");
    namedWindow("Robocup", CV_WINDOW_AUTOSIZE);
    if(!image.empty()) {
        imshow("Robocup", image);
    }
    setMouseCallback("Robocup", onMouse, &image);
    waitKey(0);
    destroyWindow("Robocup");
    return 0;
}

任何人都可以帮助我吗?谢谢!

更新

我已经完成了关于平均值的部分,但我不知道如何细分e改变衬衫的颜色

0 个答案:

没有答案