c++imageopencvimage-processingmat

Change amount of channels / convert OpenCV Mat


So, I'm trying to take a screenshot of my desktop, then import a template image using imread() and use them in a matchTemplate().

My imported template's type is CV_8UC3.

For taking a screenshot and converting it to Mat i use function from this post (From the answer with 40+ upvotes), which outputs a Mat with type of CV_8UC4.

But matchTemplate() only works if source image and template have the same type.

So i need to either increase template's number of channels by 1 or decrease screenshot's number of channels by 1.

When i try to change the type of created Mat in screenshot function (at src.create(height,width,CV_8UC4);) to anything but CV_8UC4, it just stops working and outputs a blank image. When i set template's number of channels to 4 using reshape() the image just dissapears. When i set screenshot's number of channels to 3 using reshape() this happens:

Original image: https://imgur.com/a/gVmkjG2

Reshaped to 3 channels: https://imgur.com/a/Ut16NPx

I'm out of ideas :/


Solution

  • Dan Mašek gave the correct answer in comments and asked to self-answer:

    So drop the alpha channel (using cvtColor. Considering it's a screenshot, the transparency is probably useless anyway... reshape really don't make sense.