Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I have tried to change one color to another, so I have created a class that has these functions:

cv::Mat SolColorChanger::changeColor(const cv::Mat& hsvImageIn)
{
    MatVector hsvChannels;
    cv::split(hsvImageIn, hsvChannels);

    cv::Mat msk;
    cv::bitwise_xor(hsvChannels[0] > m_oldColor - m_deltaColor, hsvChannels[0] < m_oldColor + m_deltaColor, msk);
    cv::bitwise_not(msk,  msk);
    cv::morphologyEx(msk, msk, cv::MORPH_CLOSE, cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 5)));
    Displayer::displayImage(msk, "color mask", DISPLAY);
    hsvChannels[0].setTo(m_newColor, msk);

    cv::Mat edited;
    cv::merge(hsvChannels, edited);
    cv::cvtColor(edited, edited, cv::COLOR_HSV2BGR);

    return edited;
}

void SolColorChanger::execute()
{
    cv::Mat hsv;
    cv::cvtColor(m_image2edit, hsv, cv::COLOR_BGR2HSV);
    Displayer::displayImage(m_image2edit, "input image", DISPLAY);
    Displayer::displayImage(hsv, "hsv image", DISPLAY);

    cv::Mat colored = changeColor(hsv);
    Displayer::displayImage(colored, "edited", DISPLAY);

    IO::saveImage(colored, m_savingName);
}

The m_oldColor is the color to be changed (red, blue, orange, etc. it has a H value); m_newColor is the new H value and the m_deltaColor is the error of hue value. I have not tested on huge images and in a loop to see its performances, so let me now how fast is it, if you test it.