[OpenCV-Python] 9 basic operations of images

OpenCV-Python: core operations

9 Basic operations of images

Goals
  • Get pixel values ​​and modify
  • Get image attributes (information)
  • Image ROI ()
  • Image channel splitting and merging
Almost all of these operations have a closer relationship with Numpy than with OpenCV, so you can get familiar with Numpy Help us write better-performing code.
(The examples will be shown in the Python terminal, because most of them have only one line of code)

9.1 Get and modify pixel value

First we need to read in an image:

import cv2
import numpy as np
img=cv2.imread('messi5.jpg')

You can get the pixel value based on the pixel's row and column coordinates. For BGR images, the return value is the value of B, G, R. For grayscale images, it will return its grayscale value (brightness? intensity)

import cv2
import numpy as np
img=cv2.imread('messi5.jpg')
px=img[100,100]
print(px)
blue=img[100,100,0]
print(blue)

You can modify the pixel value in a similar way.

import cv2
import numpy as np
img=cv2.imread('messi5.jpg')
img[100,100]=[255,255,255]
print(img[100,100])
# [255 255 255]

Warning: Numpy is an optimized software package for fast matrix operations. Therefore, we don't recommend getting the pixel values ​​one by one and modifying them one by one. This will be very slow. Do not use loops for matrix operations.
Note: The method mentioned above is used to select a region of the matrix, for example, the first 5 rows and the last 3 columns. For obtaining the value of each pixel, perhaps it would be better to use Numpy's array.item() and array.itemset(). But the return value is a scalar. If you want to get all the
values ​​of B, G, R , you need to use array.item() to divide them.

A better way to get pixel values ​​and modify them.

import cv2
import numpy as np
img=cv2.imread('messi5.jpg')
print(img.item(10,10,2))
img.itemset((10,10,2),100)
print(img.item(10,10,2))

# 59

# 100

9.2 Get image attributes

The attributes of the image include: row, column, channel, image data type, number of pixels, etc. img.shape can get the shape of the image. His return value is a tuple containing the number of rows, columns, and channels.

import cv2
import numpy as np
img=cv2.imread('messi5.jpg')
print(img.shape)

#(342, 548, 3)

Note: If the image is a grayscale image, the return value is only the number of rows and columns. So by checking this return value, you can know whether the loaded image is a grayscale image or a color image.
img.size can return the number of pixels of the image:

import cv2
import numpy as np
img=cv2.imread('messi5.jpg')
print(img.size, img.dtype) # 返回的是图像的数据类型.

# 562248 uint8

# uint8*

Note: img.dtype is very important when debugging. Because there are often inconsistencies in data types in OpenCV Python code.

9.3 Image ROI

Sometimes you need to operate on a specific area of ​​an image. For example, if we want to detect the position of the eyes in an image, we should first find the face in the image, and then find the eyes in the face area, instead of searching directly in an image. This will improve the accuracy and performance of the program.
ROI is also obtained using Numpy indexing. Now we select the part of the ball and copy it to other areas of the image.

import cv2
import numpy as np
img=cv2.imread('messi5.jpg')
ball=img[280:340,330:390]
img[273:333,100:160]=ball
img=cv2.imshow('test', img)
cv2.waitKey(0)

Take a look at the results:

img

9.4 Split and merge image channels

Sometimes we need to operate the three BGR channels separately. This is what you need to split the BGR into a single channel. Sometimes you need to merge the pictures of independent channels into one BGR image. You can do this:

import cv2
import numpy as np
img=cv2.imread('/home/duan/workspace/opencv/images/roi.jpg')
b,g,r=cv2.split(img)
img=cv2.merge(b,g,r)

or:

import cv2
import numpy as np
img=cv2.imread('/home/duan/workspace/opencv/images/roi.jpg')
b=img[:,:,0]

If you want to make the red channel value of all pixels 0, you don't need to split and then assign values. You can directly use Numpy indexing, which will be faster.

import cv2
import numpy as np
img=cv2.imread('/home/duan/workspace/opencv/images/roi.jpg')
img[:,:,2]=0

Warning: cv2.split() is a time-consuming operation. Use it only when you really need it, and try to use it if you can use Numpy indexing.

9.5 Enlarging (filling) the image

If you want to create a border around the image, just like a photo frame, you can use the cv2.copyMakeBorder() function. This is often used in convolution operations or zero padding. This function includes the following parameters:
  • src input image
  • top, bottom, left, right the number of pixels corresponding to the border.
  • borderType to add that type of border, the type is as follows:
    -cv2.BORDER_CONSTANT to add a constant value border with a color, and the next parameter (value) is required.
    – Cv2.BORDER_REFLECT The mirror image of the boundary element. For example: fedcba|abcde-fgh|hgfedcb
    – cv2.BORDER_REFLECT_101 or cv2.BORDER_DEFAULT is the same as above, but slightly modified. For example: gfedcb|abcdefgh|gfedcba
    – cv2.BORDER_REPLICATE repeats the last element. For example: aaaaaa|abcdefgh|hhhhhhh
    – cv2.BORDER_WRAP I don’t know what to say, just like this: cdefgh|abcdefgh|abcdefg
  • value border color, if the border type is cv2.BORDER_CONSTANT

In order to better understand these types, please see the demo program below.

import cv2
import numpy as np
from matplotlib import pyplot as plt
BLUE=[255,0,0]
img1=cv2.imread('opencv_logo.png')
replicate = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REPLICATE)
reflect = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT_101)
wrap = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_WRAP)
constant= cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_CONSTANT,value=BLUE)
plt.subplot(231),plt.imshow(img1,'gray'),plt.title('ORIGINAL')
plt.subplot(232),plt.imshow(replicate,'gray'),plt.title('REPLICATE')
plt.subplot(233),plt.imshow(reflect,'gray'),plt.title('REFLECT')
plt.subplot(234),plt.imshow(reflect101,'gray'),plt.title('REFLECT_101')
plt.subplot(235),plt.imshow(wrap,'gray'),plt.title('WRAP')
plt.subplot(236),plt.imshow(constant,'gray'),plt.title('CONSTANT')
plt.show()

The results are as follows (since matplotlib is used for drawing, the positions of R and B are exchanged. In OpenCV, it is arranged by BGR, and in matplotlib, it is arranged by RGB):

img
For more information, please pay attention to the official account:
img

Guess you like

Origin blog.csdn.net/yegeli/article/details/113405701