0

I am getting an image from the mobile phone device camera and want to use OpenCV to apply color filters to the images. (Software/System is a Flutter App that uses Foreign Function Interface (FFI) to interact with the OpenCV Library in C++)


I followed below question and tried to implement the solution, but so far I get only totally white images.


I ended up with the following code trying to achieve it:

transform function (not working)

    void filter_1(char* inputImagePath, char* outputImagePath) {
        Mat input = imread(inputImagePath);
        Mat output = input.clone();

        Mat filter = (Mat_<float>(4,4) << 0.272, 0.534, 0.131, 0.0,
                                          0.349, 0.686, 0.168, 0.0,
                                          0.393, 0.769, 0.189, 0.0,
                                          0.0, 0.0, 0.0, 1.0);
        transform(output, input, filter);
        imwrite(outputImagePath, input);
    }

filter with 0~255 values instead of 0.0~1.0 (also not working)

       Mat filter = (Mat_<int>(4,4) <<69, 136, 33, 0,
                                      88, 174, 42, 0,
                                      99, 196, 48, 0,
                                      0, 0, 0, 255);

So my question would be how to apply the filter and save the result successfully for further use? The sample filter here should result in a Sepia effect on the image.


UPDATE (2021.07.21)

  • Removed filter2d function and related text to avoid confusion.
  • Removed link to filter2d related question in SO
  • Added sample for 0~255 values matrix

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
goldensoju
  • 606
  • 1
  • 6
  • 14
  • 1
    filter2d is wrong here, it's a spatial filter, not a color filter (best to remove that from the post entirely, it only confuses people). `transform` is correct. the problem is the scale of values. check the maximum value. if the output is floating point, the assumed range is probably 0.0 to 1.0, not 0 to 255. scale (multiply) your values accordingly. – Christoph Rackwitz Jul 09 '21 at 11:02
  • @ChristophRackwitz thanks for the info. i removed the filter2d content. i tried to use a mat with 0~255 values but that also results in just plain white. – goldensoju Jul 12 '21 at 02:59
  • 1
    as I tried to explain in the second half of my previous comment, that depends on the **data type** because imshow/imwrite are sensitive to that. it does not do any scaling. if your pixels are 0..255 and your filter is 0..255, the result will be 0..195075 (255*255*3 worst case). **you** have to scale your result or your filter properly. the range of the **output** values matters. – Christoph Rackwitz Jul 12 '21 at 09:00
  • Thanks for the explanation. I was able to get the data type and to solve my problem. Actually the code from the the question works. Not sure what the problem exactly was but maybe that i forgot to remove the `IMREAD_COLOR` in `imread(inputImagePath, IMREAD_COLOR);` – goldensoju Jul 12 '21 at 15:10

0 Answers0