12

I have a snippet code from colorblobdetection but I don't know what is the purpose of CvType.CV_8UC4. The Link here does not really explain how does it works.

public void onCameraViewStarted(int width, int height) {
    mRgba = new Mat(height, width, CvType.CV_8UC4); 
    mDetector = new ColorBlobDetector();
    mSpectrum = new Mat();
    mBlobColorRgba = new Scalar(255);
    mBlobColorHsv = new Scalar(255);
    SPECTRUM_SIZE = new Size(200, 64);
    CONTOUR_COLOR = new Scalar(255,0,0,255);
}
Community
  • 1
  • 1
Shulz
  • 508
  • 3
  • 12
  • 27

3 Answers3

33

OpenCV types can be read the following:

  • CV_
  • 8U: Unsigned int 8-bit
  • C4: Four channels.

Thus mRgba = new Mat(height, width, CvType.CV_8UC4); creates a Matrix with four color channels and values in the range 0 to 255.

Unapiedra
  • 15,037
  • 12
  • 64
  • 93
  • What does that 'channels' do ? – Shulz Sep 17 '16 at 06:46
  • 4
    @Shulz the channels are the colour components. E.g. an ordinary RGB image has 3 channels, an RGBA (RGB + alpha) image has four channels, and a CMYK image has four channels. Look for the explanation of CV_8UC3 at http://docs.opencv.org/2.4/doc/tutorials/core/mat_the_basic_image_container/mat_the_basic_image_container.html#creating-a-mat-object-explicitly – Bull Sep 17 '16 at 07:12
13

This makes a specific form of Matrix. Where 8u means each element will be unsigned (only positive) integers, 8-bit.

The reason there are 4 channels (like 4 slices that layer together to make an image of different colours) is to make up the image. The first 3 in this are R, G, B, and the last is Alpha, which is a value between 0 and 1 representing transparency. When these slices combine you get the correct combination of colours.

enter image description here

Caspar Wylie
  • 2,818
  • 3
  • 18
  • 32
  • It is not in general the *last* byte that's the alpha, so there are good reasons in other Environments for format names like RGB32 and ARGB. It is important to look at the source documentation of your incomming data! – Patrick Z Feb 09 '21 at 16:01
1

All Answers are correct, additionally: the 4th channel is not in every situation an alpha/transparency information. Sometimes it is used if the incomming buffer was an 24bit color image with a padding byte (most at the front or end) to fill it up so that addressing a specific pixel can be done atomically (just by the adress) without finding the specific byte offset. E.g. QImage loads jpegs as 32bit images while only 3 of the 4 bytes are relevant.

So the CV_8UC4 can be used directly for that. BTW, without the use of Transforming RGB to somewhat like HSV all channels are treated separately, and therefore:

It might be a good idea to reformat it to a 3bpp image if there are a lot of heavy cv-operations comming in the next processing steps. This can reduce the processing-time on roughly 25% because of the missing useless 4th channel.

Patrick Z
  • 194
  • 9