OpenCV graphics processing

Table of contents

chapter introduction

First, the basic representation of the image

1. Binary image

2. Grayscale image

3. Color image

2. Pixel processing

1. Binary image and grayscale image

2. Color image

3. Access pixels with numpy.array

1. Binary image and grayscale image

2. Color image

4. Region of Interest (ROI)

5. Channel operation

6. Get image properties, etc.

Summarize



chapter introduction

It mainly introduces the basic representation of images, etc. At the same time, the Numpy.array library is the basis for python to process images, and it is necessary to be familiar with awa.


First, the basic representation of the image

There are generally three common representation methods: binarization, grayscale and color images.

1. Binary image

Meaning: A binary image refers to an image that contains only two colors, black and white. In the pixels of the image, generally, the white pixels are treated as "1", and the black pixels are marked as "0".

#二值化图像的具体例子
import cv2
import numpy as np

img = cv2.imread("28.jpg")
cv2.namedWindow("img", cv2.WINDOW_NORMAL)
cv2.imshow("img", img)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray, 95, 115, cv2.THRESH_BINARY)
cv2.namedWindow("binary", cv2.WINDOW_NORMAL)
cv2.imshow("binary", binary)

contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)  # 1
cv2.drawContours(img, contours, -1, (0, 0, 50), -1)  # 2
cv2.namedWindow("result", cv2.WINDOW_NORMAL)
cv2.imshow("result", img)
cv2.waitKey()
cv2.destroyAllWindows()

The following is the result of image binarization: Figure 1 is the original image, and Figure 2 is the shape of some cats obtained after binarization. 

2. Grayscale image

Significance: Let the image show more details, so that more values ​​can be used to reflect different colors, making the image more detailed.

#相应代码
import cv2
import numpy as np

img = cv2.imread("28.jpg")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY,0)

cv2.imshow("img",gray)
cv2.waitKey()
cv2.destroyAllWindows()

The result of the operation is as follows: 

3. Color image

Description: Compared with binary images and grayscale images. Color images are one of a common class of images. At the same time, in the RGB color space, each color channel value ranges from 0 to 255. Generally, we use a combination of these three color channels to represent colors. At the same time, it should be noted that in opencv, the order of channels is often BGR.

#代码一
#导包
import cv2
#读取图像
img = cv2.imread("28.jpg")
#分离颜色通道
b,g,r = cv2.split(img)
#显示图像
cv2.imshow("b", b)
cv2.imshow("g", g)
cv2.imshow("r", r)
cv2.imshow("img",img)
cv2.waitKey(0)
cv2.destroyAllWindows()



#代码二
#导包
import cv2
import numpy as np
#读取图像
img = cv2.imread("28.jpg")
#提取图像通道
b = img[:,:,0]
g = img[:,:,1]
r = img[:,:,2]
#显示不同层的通道
cv2.imshow("b", b)
cv2.imshow("g", g)
cv2.imshow("r", r)
#改变img图像的通道设置值
img[:,:,0] = 0
cv2.imshow("img1",img)
img[:,:,1] = 0
cv2.imshow("img2",img)
img[:,:,2] = 0
cv2.imshow("img3",img)

cv2.waitKey()
cv2.destroyAllWindows()

2. Pixel processing

1. Binary image and grayscale image

Explanation: Binarization must be understood as a special grayscale image, because the processed binarization generally only has two values: 0 (black) and 255 (white).

#代码一
import cv2
import numpy as np
img = np.zeros((8,8),dtype = np.uint8)
print("img=\n",img)
cv2.imshow("img",img)
print("img[0,3]=",img[0,3])   #读取像素点
img[0,3] = 255
print("img=\n",img)      #修改后
print("img[0,3]=",img[0,3])   #读取修改后像素点
cv2.waitKey()
cv2.destroyAllWindows()

#在第一段代码的基础上修改后的代码二
import cv2
import numpy as np

img = np.zeros((8,8),dtype = np.uint8)
print("img=\n",img)
cv2.imshow("img",img)

for i in range(0,1):
    for j in range(3,4):
        img[i,j]=255
print("img=\n",img)

cv2.imshow("img1",img)
cv2.waitKey()
cv2.destroyAllWindows()

 operation result:

Practical application case

#读取灰度图像,并对像素进行访问获取,修改等。
import cv2
img = cv2.imread("beautiful.jpg",0)
cv2.namedWindow("img", cv2.WINDOW_NORMAL)
cv2.imshow("img",img)
for n in range(0,200):
    for b in range(0,255):
        img[n,b]=255
cv2.namedWindow("img1", cv2.WINDOW_NORMAL)
cv2.imshow("img1",img)
cv2.waitKey()
cv2.destroyAllWindows()

Actual running results: 

2. Color image

Explanation: Generally, the color image in RGB mode in opencv will read the RGB image sequentially according to the row direction when reading, that is, the pixel point awa of the BGR channel.

#代码1
import cv2
import numpy as np

blue = np.zeros((300,300,3),dtype=np.uint8)
blue[:,:,0]=255
print("blue=\n",blue)
cv2.imshow("blue",blue)

green = np.zeros((300,300,3),dtype=np.uint8)
green[:,:,1]=255
print("green=\n",green)
cv2.imshow("green",green)

red = np.zeros((300,300,3),dtype=np.uint8)
red[:,:,2]=255
print("red=\n",red)
cv2.imshow("red",red)

cv2.waitKey()
cv2.destroyAllWindows()

The three channels are worth changing (the running results are as follows):

 

#代码2
import cv2
import numpy as np

img = np.zeros((300,300,3),dtype=np.uint8)
img[:,0:100,0]=255
img[:,100:200,1]=255
img[:,200:300,2]=255
print("img=\n",img)

cv2.namedWindow("img", cv2.WINDOW_NORMAL)
cv2.imshow("img",img)
cv2.waitKey()
cv2.destroyAllWindows()

The result of running code 2: 

 Specifically, use numpy to generate a three-dimensional array to view the different colors of the three channels.

#代码4
#利用zeros()函数生成一个数组,直接使用数组索引对其进行访问,和修改。
import numpy as np
img = np.zeros((2,4,3),dtype = np.uint8)

print("img=\n",img)
print("img[0,3]=",img[0,3])
print("img[1,2,2]=",img[1,2,2])

img[0,3]=255
img[0,0]=[66,77,88]
img[1,1,1]=3
img[1,2,2]=4
img[0,2,0]=5
print("img\n",img)
print("img[1,2,2]=",img[1,2,2])
#代码5
#读取一幅彩色图像 并对其像素进行访问

import cv2
img = cv2.imread("28.jpg")
cv2.namedWindow("img", cv2.WINDOW_NORMAL)
cv2.imshow("img",img)
print("img[0,0]",img[0,0])     #访问读取
print("img[0,0,0]",img[0,0,0])
print("img[0,0,1]",img[0,0,1])
print("img[0,0,2]",img[0,0,2])
print("img[50,0]",img[50,0])
print("img[100,0]",img[100,0])

#区域1
for i in range(0,50):
    for j in range(0,100):
        for k in range(0,3):
            img[i,j,k] = 255 #白色

#区域2
for i in range(50,100):
    for j in range(0,100):
            img[i,j]=[128,128,128]  #灰色

#区域3
for i in range(100,150):
    for j in range(0,100):
        img[i,j]=0  #黑色
cv2.imshow("img",img)
print("img[0,0]=",img[0,0])       #修改后
print("img[0,0,0]=",img[0,0,0])
print("img[0,0,1]=",img[0,0,1])
print("img[0,0,2]=",img[0,0,2])
print("img[50,0]=",img[50,0])
print("img[100,0]=",img[100,0])
cv2.waitKey()
cv2.destroyAllWindows()

3. Access pixels with numpy.array

1. Binary image and grayscale image

          Description of related functions: 1. The function item() can access the pixels of the image more effectively, and the corresponding expression is: item(row, column).

        2. The function itemset() can be used to modify the pixel value, and the corresponding expression is:

itemset(index value, new value).

#
import numpy as np
img = np.random.randint(10,99,size=[5,5],dtype=np.uint8)
print("img=\n",img)
print("img.item(3,2)=",img.item(3,2))    #读取像素点
img.itemset((3,2),255)
print("img=\n",img)   #修改后
print("img.item(3,2)=",img.item(3,2))   #修改后像素点

 Running results (and ideas):

Idea: first import the runtime library and so on. Then use random.randint to generate a random array, and then use functions such as item to modify pixel values. 

#生成一个灰度图。以及随机的像素值
import cv2
import numpy as np
img = np.random.randint(0,256,size=[256,256],dtype=np.uint8)
cv2.imshow("img",img)
cv2.waitKey()
cv2.destroyAllWindows()

The following is a practical case: read the grayscale image and access and modify the pixel value...

#
import cv2
img = cv2.imread("28.jpg",0)

print("读取像素值img.item(3,2)=",img.item(3,2))
img.itemset((3,2),255)
print("修改后像素值img.item(3,2)=",img.item(3,2))

cv2.imshow("img",img)
for i in range(10,100):
    for j in range(80,100):
        img.itemset((i,j),255)
cv2.imshow("img1",img)
cv2.waitKey()
cv2.destroyAllWindows()

 

2. Color image

Explanation: The steps of using item and other functions to access and modify the pixel value of the color image in the color image are basically similar to the grayscale image, but the difference lies in the supplementary channel information, for example:

1.item (row, column, channel)

2.itemset (triple index value, new value)

#使用item()等函数来修改随机数组构成的彩色图像

import numpy as np
img = np.random.randint(10,99,size=[2,4,3],dtype=np.uint8)

print("img=\n",img)
print("读取像素值img[1,2,0]=",img.item(1,2,0))
print("读取像素值img[0,2,1]=",img.item(0,2,1))
print("读取像素值img[1,0,2]=",img.item(1,0,2))

img.itemset((1,2,0),255)
img.itemset((0,2,1),255)
img.itemset((1,0,2),255)
print("修改后img=\n",img)
print("修改后像素点img[1,2,0]=",img.item(1,2,0))
print("修改后像素点img[0,2,1]=",img.item(0,2,1))
print("修改后像素点img[1,0,2]=",img.item(1,0,2))

Specific running results: 

 Small suggestion: In actual operation, random functions can be used to generate larger random arrays, and the function cv2.imshow() can be used to observe the color image corresponding to the random array.

#生成彩色图像,但像素值为随机数
import cv2
import numpy as np
img = np.random.randint(0,256,size=[256,256,3],dtype=np.uint8)
cv2.imshow("img",img)
cv2.waitKey()
cv2.destroyAllWindows()

Hands-on: Read a color image and access and modify pixels. 

#参考代码
import cv2
import numpy as np
img = cv2.imread("28.jpg")
cv2.imshow("img",img)
print("访问img.item(0,0,0)=",img.item(0,0,0))
print("访问img.item(0,0,1)=",img.item(0,0,1))
print("访问img.item(0,0,2)=",img.item(0,0,2))
for i in range(0,50):
    for j in range(0,100):
        for k in range(0,3):
            img.itemset((i,j,k),255)  #白色
cv2.imshow("after",img)
print("img.item(0,0,0)=",img.item(0,0,0))   #修改后
print("img.item(0,0,1)=",img.item(0,0,1))
print("img.item(0,0,2)=",img.item(0,0,2))

 

 

4. Region of Interest (ROI)

Overview: In the process of image processing, if you are interested in a specific area of ​​the image, then this area is called the area of ​​interest. Generally speaking, after the region of interest ROI is set, the overall operation of the region can be performed.

#
import cv2
img = cv2.imread("beautiful.jpg",cv2.IMREAD_UNCHANGED)
face = img[600:1000,500:900]   #通过face = img[600:1000,500:900]获取一个ROI,且使用函数        
                                                   cv2.imshow("face",face)将其表现出来cv2.namedWindow("img", cv2.WINDOW_NORMAL)
cv2.namedWindow("face", cv2.WINDOW_NORMAL)
cv2.imshow("img",img)
cv2.imshow("face",face)
cv2.waitKey()
cv2.destroyAllWindows()

operation result:

 Note: The img on the right is the original image, and the face on the left is a part of the face image obtained from the img window image on the right.

Practical task: After reading the relevant ROI region of interest in the original image above, we now perform a coding process on that region of interest (the specific reference code is as follows).

#
import cv2
import numpy as np
img = cv2.imread("beautiful.jpg",cv2.IMREAD_UNCHANGED)
cv2.namedWindow("img", cv2.WINDOW_NORMAL)
cv2.imshow("img",img)
face = np.random.randint(0,256,(400,400,3))
img[600:1000,500:900]=face
cv2.namedWindow("img1", cv2.WINDOW_NORMAL)
cv2.imshow("img1",img)
cv2.waitKey()
cv2.destroyAllWindows()

operation result:

Extension: Combining the above two practical tasks, we are copying the image ROI of the coding position in the original image to another image. (The specific code is as follows)

#
import cv2
img = cv2.imread("beautiful.jpg",cv2.IMREAD_UNCHANGED)
img1 = cv2.imread("back.jpg",cv2.IMREAD_UNCHANGED)
cv2.namedWindow("img", cv2.WINDOW_NORMAL)
cv2.namedWindow("img1", cv2.WINDOW_NORMAL)
cv2.imshow("img",img)
cv2.imshow("img1",img1)
face = img[600:1000,500:900]
img1[200:600:,800:1200]=face
cv2.namedWindow("img2", cv2.WINDOW_NORMAL)
cv2.imshow("img2",img1)
cv2.waitKey()
cv2.destroyAllWindows()

 operation result:

 

5. Channel operation

        RGB images are generally composed of three image channels RGB, but generally in opencv, it is stored in the reverse order of subtitles.

1. Channel index split

#索引方式
import cv2

img = cv2.imread("beautiful.jpg")
cv2.namedWindow("img", cv2.WINDOW_NORMAL)
cv2.imshow("img",img)

b = img[:,:,0]
g = img[:,:,1]
r = img[:,:,2]
cv2.namedWindow("b", cv2.WINDOW_NORMAL)
cv2.namedWindow("g", cv2.WINDOW_NORMAL)
cv2.namedWindow("r", cv2.WINDOW_NORMAL)
cv2.imshow("b",b)
cv2.imshow("g",g)
cv2.imshow("r",r)

img[:,:,0] = 0
cv2.namedWindow("img1",cv2.WINDOW_NORMAL)
cv2.imshow("img1",img)
img[:,:,1] = 0
cv2.namedWindow("img2",cv2.WINDOW_NORMAL)
cv2.imshow("img2",img)
cv2.waitKey()
cv2.destroyAllWindows()

 Note: The indexing method used in the first piece of code is to split the RGB channels separately, and then implement the layers one by one.

2. Function splitting

#函数拆分
import cv2
img = cv2.imread("fxj.jpg")
b,g,r=cv2.split(img)
cv2.imshow("b",b)
cv2.imshow("g",g)
cv2.imshow("r",r)
cv2.waitKey()
cv2.destroyAllWindows()

Note: The statement b, g, r=cv2.split(img) in the function split has the same effect as the split b=cv2.split(img)[0]. . .

#使用cv2.merge()合并通道
import cv2
img = cv2.imread("25.jpg")
b,g,r=cv2.split(img)
bgr = cv2.merge([b,g,r])
rgb = cv2.merge([r,g,b])
cv2.imshow("img",img)
cv2.imshow("bgr",bgr)
cv2.imshow("rgb",rgb)
cv2.waitKey()
cv2.destroyAllWindows()

 Explanation: Channel merging is the inverse process of channel splitting. Generally speaking, it can combine grayscale images of three channels into a color image. Taking the code in the above code box as an example, first split the image read in img into a detailed channel, and then merge the b, g, r channels to obtain the bgr image, and so on. In the next line of rgb = cv2.merge([r,g,b]) code, we repeat the operation of the bgr image above to get the rgb image. Finally, all the image results are obtained by running the total program.

6. Get image properties, etc.

  Note: In the process of obtaining image properties, etc., the properties often include image size and so on. Here are several common attribute values ​​​​used in the following code:

1. The first is shape. Generally, when we use this attribute, it can help us judge whether the image is a binary image or a color image, etc. by checking whether the return value of this attribute includes the number of channels, etc.

 2. size is generally the number of pixels in the returned image.

3. dtype is the data type of the returned image.

#
import cv2

gray = cv2.imread("4.jpg",0)
color = cv2.imread("9.jpg")

print("gray:")   #指图像gray的属性
print("gray.shape=",gray.shape)
print("gray.size=",gray.size)
print("gray.dtype=",gray.dtype)

print("color:")
print("color.shape=",color.shape)
print("color.size=",color.size)
print("color.dtype=",color.dtype)

Summarize

Here is a general summary of the content of the article:

It mainly introduces the basic expression methods of images, the processing of regions of interest and other knowledge points. For the numpy.array library mentioned in the article, it is the basis for python processing...

Guess you like

Origin blog.csdn.net/Shuette/article/details/125698392