Ask Your Question

Revision history [back]

OpenCV and C++: change the color of some object in the image

I'm new to OpenCV and I'm trying to do some simple operations with an image.

I have this image. First of all, when the left button is clicked I have to compute the mean RGB color over a 9x9 neighborhood of the clicked point, then segment the soccer shirts by applying a static threshold to the three channels R, G and B (ΔR < 50, ΔG < 50, ΔB < 50), finally apply the new color to the shirts (RGB = (92,37,201)). This is my code:

#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);

        //mean on the neighborhood code to be insert

        //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;
}

Anyone can help me? Thank you!