Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Color of a Rectangle (ROI) in a matrix with OpenCV Java?

I'm trying to convert a color in a ROI of an matrix with OpenCV Java with following Code

public Mat detect(Mat image){
    Rect roi = new Rect(new Point(50, 50), new Point(image.width() - 50, image.height() - 50));
    Mat mask = image.submat(roi);
    Mat temp = new Mat();
    Imgproc.cvtColor(mask, temp, Imgproc.COLOR_BGRA2GRAY,0);
    temp.copyTo(mask);
    return image;
}

But the result is the same as the given image. If I change the Imgproc.cvtColor() call with a Imgproc.medianBlur() effect for example:

    ...
    Imgproc.medianBlur(mask, temp, 11);
    ... 

There is a blurred rectangle visible. I think while the process of Imgproc.cvtColor() the references to the original image are replaced by some newly created. So how else can I convert the color of a ROI in a matrix without loosing the references?

I appreciate any help, thanks!

P.S: I wanted to add some sample images but my reputation is not high enough. I'm sorry for this and hope you can imagine my problem even without samples.

like image 789
Meldamos Avatar asked Nov 11 '22 20:11

Meldamos


1 Answers

you can't have an image, that is part 1 channel, and 4 channel otherwise.

the blur example worked fine, because it did not change the number of channels.

you'll have to convert your gray (sub)img to rgba again, before copying

Mat temp = new Mat();
Imgproc.cvtColor(mask, temp, Imgproc.COLOR_BGRA2GRAY,0);
Mat temp_rgba = new Mat();
Imgproc.cvtColor(temp, temp_rgba, Imgproc.COLOR_GRAY2BGRA,0);
temp_rgba.copyTo(mask);
like image 125
berak Avatar answered Nov 15 '22 03:11

berak