Ask Your Question
3

Convert CV_8UC4 to CV_8UC3,in openCV4Android?

asked 2014-10-14 13:07:22 -0500

HasanGhaforian gravatar image

Here src is an instance of Mat and it's type is CV_8UC4.I want to use src as first parameter of watershed:

public static void watershed(Mat image, Mat markers)

In about first parameter of watershed,Docs say:

Parameters:
image - Input 8-bit 3-channel image.

So I have to convert src from CV_8UC4 to CV_8UC3.I used this to convert src type:

src.convertTo(src, CvType.CV_8UC3);

But after that,when I debug code,type of src is CV_8UC4 yet and no CV_8UC3.Did I do any thing wrong?And how I can convert it's type?

edit retag flag offensive close merge delete

Comments

Aside from using cvtColor instead of convertTo, keep in mind that because converting CV_8UC4 to CV_8UC3 is a shape-changing operation (the total number of elements will change, and hence a memory reallocation may have taken place), that it is probably bad idea to use the same handle src as both the source and the destination. Because of this, cvtColor should be called with two distinct handles src and temp, and then after the operation the temp handle can be assigned back to src. (My use of the word "handle" refers to Handle-Body Idiom, which is applicable to smart-pointers and Java references.)

rwong gravatar imagerwong ( 2014-10-14 17:05:56 -0500 )edit

2 answers

Sort by » oldest newest most voted
5

answered 2014-10-14 13:42:30 -0500

berak gravatar image

no, please instead use:

Mat dst = new Mat();
Imgproc.cvtColor(src,dst,Imgproc.BGRA2BGR);
//convertTo changes the depth/per_pixel_type, not the channel count.
edit flag offensive delete link more

Comments

The third parameter should be prefixed by COLOR_, i.e. Imgproc.cvtColor(src, dst, Imgproc.COLOR_BGRA2BGR);

jjinking gravatar imagejjinking ( 2016-05-30 04:48:24 -0500 )edit
2

answered 2014-10-14 17:01:25 -0500

rwong gravatar image

A correct answer has been given by berak.


Below is just an explanation of the historical context.

convertTo cannot be used to change the number of channels. Simply speaking, convertTo will only read the "depth" part of the enum and ignore the "channel" part of the enum.


In the documentation, this is explained as follows: (emphasis added)

rtype – desired output matrix type or, rather, the depth since the number of channels are the same as the input has; if rtype is negative, the output matrix will have the same type as the input.

What this means is that your code is equivalent to calling convertTo with a enum of CV_8U, while keeping the number of channels unchanged at 4.


I agree that this is very confusing to OpenCV users, but it is one of the many rough edges that can't be fixed anymore due to backwards compatibility.

edit flag offensive delete link more

Comments

1

@rwong +1 vote for your good explanation and good comment.Thank you!

HasanGhaforian gravatar imageHasanGhaforian ( 2014-10-14 21:38:21 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2014-10-14 13:07:22 -0500

Seen: 10,396 times

Last updated: Oct 14 '14