Javatpoint Logo
Javatpoint Logo

OpenCV Basic Operation on Images

In this tutorial, we will learn the essential operations that are related to the images. We are going to discuss the following topics.

  • Access pixel values and modify them
  • Access Image Properties
  • Setting Region of Image
  • Splitting and merging images
  • Change the image color

Accessing and Modifying pixel values

We can retrieve a pixel value by its row and column coordinates. It returns an array of blue, green, red values of the BGR image. It returns the corresponding intensity for the grayscale image. First, we need to load the BGR image.


[190 166 250]

Accessing Image Properties

It is better to know the size of the image to work with the image processing application. In OpenCV, images are generally stored in the Numpy ndarray. To get the image shape or size, use ndarray.shape to find the dimension of the image. Then, we can use the index position to get the height, width, and number of channels.

Consider the following example:


Image Dimension    :  (4, 1, 3)
Image Height       :  4
Image Width        :  1
Number of Channels :  3
Image Size  : 12

Image ROI (Region of Interest)

Sometimes, we need to work with some areas of the image. As we discuss in the previous tutorial face detection is over the entire picture. When a face is obtained, we select only the face region and search for eyes inside it instead of searching the whole image. It enhances accuracy and performance because eyes are always on the face and don't need to search the entire image.

OpenCV Basic Operation on Images

In the above image, if we need to select the ball. We only require selecting the ball region.

Splitting and Merging Image channels

An image's BGR channels can be split into their planes when needed. Then, the individual channels can be merged back together from the BGR image again. This can be done by following way:


Note: The cv2.split() function is a slow function. Numpy indexing is quit efficient and it should be used if possible.

Making Borders for Images

OpenCV provides the cv2.copyMakeBorder() function to create a border around the image, something like a photo frame. The syntax of the function is given below.


src - It denotes input image.

top, bottom, left, right - It defines the border width in the number of pixels in the corresponding direction.

borderType - It defines what kind of border to be added. The border can be the following types.

value - Color of border if border type is cv.BORDER_CONSTANT

Consider the following example:

Change in Image color

OpenCV cvtColor

The cvtColor is used to convert an image from one color space to another. The syntax is following:


src - It is used to input an image: 8-bit unsigned.

dst - It is used to display an image as output. The output image will be same size and depth as input image.

code - color space conversion code.

Consider the following example:


OpenCV cvtColor

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA