Copy an image into a region of interest (ROI) of a larger image

Copy a ROI from one image into a new image

…given an existing image and a Rect describing an ROI:

Mat imageCrop;
imageSource(rectROI).copyTo(imageCrop);

Convert a Mat into an IplImage

…NOTE – this does not make a copy – it just creates a new header over the existing data – if the Mat or the IplImage are changed, the other will also change. To make a separate copy, do a imageMat.copyTo(dst) first.

Convert an IplImage into a Mat

Decode a JPG which is already in memory

If the JPG is already loaded into memory, but not decoded (i.e. it’s just been byte-for-byte read from disk), it can be decoded as follows using imdecode. First it has to be loaded into a Matrix.

void *pImageData; // Set this to point to the 'file' in memory
long lImageDataSize; // Number of bytes
Mat imageDecoded = cv::imdecode(cv::Mat(1,
lImageDataSize,
CV_8UC1,
pImageData),
CV_LOAD_IMAGE_UNCHANGED);

Encode a Mat into a JPG (or other image format) to a memory buffer instead of file

How to fill an image with a single colour

How to resize an image

How to access pixels in an image

Note – this is NOT the quickest way of accessing pixels, especially accessing *all* pixels, or whole rows at a time – in those cases, there are ‘pointer arithmetic’ methods which are a lot more efficient. However, for ‘random’ access, this is the *easiest* way.
This example shows code getting the Blue, Green and Red channels for pixels coord iX,iY, from an RGB image (order of colour planes is BGR).