-4

I am new to OpenCV and just doing a basic RGB color filter. I was doing this by testing each pixel, but that's inefficient. So I tried Core.inRange but that returns a mask (black and white) and I need a colored Mat. Here is what I currently have:

// frame is input, out is output
Scalar minValues = new Scalar(RFrom, GFrom, BFrom);
Scalar maxValues = new Scalar(RTo, GTo, BTo);
Mat mask = new Mat();
Core.inRange(frame, minValues, maxValues, mask);
Core.bitwise_and(frame, mask, out);

When I run it, I get:

.CvException [org.opencv.core.CvException: cv::Exception: OpenCV(3.4.2) /Users/jason/Projects/openpnp/opencv/opencv/opencv-3.4.2/modules/core/src/arithm.cpp:225: error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array' (where arrays have the same size and type), nor 'array op scalar', nor 'scalar op array' in function 'binary_op' ]

This is because the mask updated by inRange has type CV_8UC1 but the frame has type CV_8UC3. What can I do to fix this?

David Newcomb
  • 10,639
  • 3
  • 49
  • 62
Titan
  • 33
  • 10
  • 3
    Stack Overflow is neither a forum, nor a blog. Please rewrite this into a proper Q and A format, or just delete it. ([ask], [answer]) – Dan Mašek Dec 10 '17 at 15:59
  • 3
    Please edit your question, remove the answer part and post it as answer. Srlf answered questions are fine, but they have to be posted in SO question / answer format. You can even accept your own answer, but you should remove "solved" from your title. – Modus Tollens Dec 10 '17 at 16:31
  • Okay, I rewrote it. – Titan Dec 19 '17 at 23:23

1 Answers1

1

I tried switching out and frame in and using frame.copyTo(out, mask) instead of Core.bitwise_and(frame, mask, out)

It worked!!

Here is my final code:

// frame is input, out is output            
Scalar minValues = new Scalar(RFrom, GFrom, BFrom);
Scalar maxValues = new Scalar(RTo, GTo, BTo);
Mat mask = new Mat();
Core.inRange(frame, minValues, maxValues, mask);
frame.copyTo(out, mask);
David Newcomb
  • 10,639
  • 3
  • 49
  • 62
Titan
  • 33
  • 10