Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV help me with Sepia kernel

I am trying to create a sepia effect. This is the code I am trying:

    Mat image_copy;
    cvtColor(image, image_copy, CV_BGRA2BGR);

    Mat kern = (Mat_<char>(4,4) <<  0.272, 0.534, 0.131, 0,
                0.349, 0.686, 0.168, 0,
                0.393, 0.769, 0.189, 0,
                0, 0, 0, 1);
    cv::transform(image_copy, image, kern);

But it doesn't work. I get a black image. No error, no exception, just black image. Any ideas?

I have tried applying different kernels and they do work. For example:

    Mat kern = (Mat_<char>(4,4) <<  10, 0, 0, 0,
                0, 10, 0, 0,
                0, 0, 10, 0,
                0, 0, 0, 10);
    cv::transform(image_copy, image, kern);
    image += cv::Scalar(10, 10, 10, 0);

Please help.

like image 655
Richard Knop Avatar asked Apr 07 '13 23:04

Richard Knop


1 Answers

It seems you are creating a kernel of char values but trying to store float values.

Make sure to declare the kernel with the same data type as the values you want to store:

#include <cv.h>
#include <highgui.h>

#include <iostream>

int main()
{
    cv::Mat image = cv::imread("test.jpg");
    if (!image.data)
    {
        std::cout << "!!! Failed imread" << std::endl;
        return -1;
    }

    cv::Mat image_copy = image.clone();

    cv::Mat kern = (cv::Mat_<float>(4,4) <<  0.272, 0.534, 0.131, 0,
                                             0.349, 0.686, 0.168, 0,
                                             0.393, 0.769, 0.189, 0,
                                             0, 0, 0, 1);

    cv::transform(image_copy, image, kern);

    cv::imshow("sepia", image);
    cv::waitKey(0);

    return 0;
}
like image 73
karlphillip Avatar answered Nov 09 '22 19:11

karlphillip