第一個簡單程式
First approaches to image processing: load and display an image
import cv2 as cv
img = cv.imread('logos.jpg') # imread () method reads the file (a compressed format such as JPG) and translate it into a data structure
# made of numerical matrix corresponding to color gradations and position.
cv.imshow('Image', img) # imshow() method to create a window with the image loaded in the variable img
cv.waitKey(0) # waitKey() method starts the display of a window and also allows you to control the waiting time of the program before
continuing with the next command
Working with images
After loading the image Decompose in the three RGB channels. You can do this easily by using the split() method.
Now reassemble the three channels, but changing the order, for example by exchanging the red channel with the green channel.
You can easily make recombination using the merge() method.
The destroyWindow() method allows you to close the desired window (could be several open) by specifying as argument the name of the window which in your case is “Image”.
Save the new image
cv.imwrite('newlogos.png',img2)
Accessing and Modifying pixel values
You can access a pixel value by its row and column coordinates.
For BGR image, it returns an array of Blue, Green, Red values.
For grayscale image, just corresponding intensity is returned.
px = img[100,100] print px [157 166 200] # accessing only blue pixel blue = img[100,100,0] print blue 157
You can modify the pixel values the same way.
img[100,100] = [255,255,255] print img[100,100] [255 255 255]
Accessing Image Properties
Image properties include number of rows, columns and channels, type of image data, number of pixels etc.
Shape of image is accessed by img.shape
. It returns a tuple of number of rows, columns and channels (if image is color):
Example :
rows,cols,channels = img2.shape
print img.shape
(342, 548, 3)
Note
If image is grayscale, tuple returned contains only number of rows and columns. So it is a good method to check if loaded image is grayscale or color image.
Total number of pixels is accessed by img.size
:
>>> print img.size
562248
Image datatype is obtained by img.dtype
:
>>> print img.dtype
uint8
Image ROI
Sometimes, you will have to play with certain region of images. ROI is again obtained using Numpy indexing.
Here I am selecting the ball and copying it to another region in the image:
ball = img[280:340, 330:390] img[273:333, 100:160] = ball
Splitting and Merging Image Channels
The B,G,R channels of an image can be split into their individual planes when needed. Then, the individual channels can be merged back together to form a BGR image again. This can be performed by:
b,g,r = cv2.split(img) img = cv2.merge((b,g,r))
Or
b=img[:,:,0]
Suppose, you want to make all the red pixels to zero, you need not split like this and put it equal to zero. You can simply use Numpy indexing which is faster.
img[:,:,2] = 0
Making Borders for Images (Padding)
If you want to create a border around the image, something like a photo frame, you can use cv2.copyMakeBorder() function. But it has more applications for convolution operation, zero padding etc. This function takes following arguments:
- src - input image
- top, bottom, left, right - border width in number of pixels in corresponding directions
- borderType - Flag defining what kind of border to be added. It can be following types:
- cv2.BORDER_CONSTANT - Adds a constant colored border. The value should be given as next argument.
- cv2.BORDER_REFLECT - Border will be mirror reflection of the border elements, like this : fedcba|abcdefgh|hgfedcb
- cv2.BORDER_REFLECT_101 or cv2.BORDER_DEFAULT - Same as above, but with a slight change, like this : gfedcb|abcdefgh|gfedcba
- cv2.BORDER_REPLICATE - Last element is replicated throughout, like this: aaaaaa|abcdefgh|hhhhhhh
- cv2.BORDER_WRAP - Can’t explain, it will look like this : cdefgh|abcdefgh|abcdefg
- value - Color of border if border type is
cv2.BORDER_CONSTANT
Below is a sample code demonstrating all these border types for better understanding:
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()
Arithmetic Operations on Images
Goal
- Learn several arithmetic operations on images like addition, subtraction, bitwise operations etc.
- You will learn these functions : cv2.add(), cv2.addWeighted() etc.
Image Addition
You can add two images by OpenCV function,
cv2.add()
or simply by numpy operation,res = img1 + img2
. Both images should be of same depth and type, or second image can just be a scalar value.
Image Blending
This is also image addition, but different weights are given to images so that it gives a feeling of blending or transparency. Images are added as per the equation below:
cv2.addWeighted()
applies following equation on the image.More info ....
https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_core/py_image_arithmetics/py_image_arithmetics.htmlBitwise Operations
This includes bitwise AND, OR, NOT and XOR operations. They will be highly useful while extracting any part of the image , defining and working with non-rectangular ROI etc.
bitwise_not,bitwise_and
資料來源:
留言列表