Opencv-python (cv2) image read, display and save, to see which one is enough

The image reading cv2.imread () function

  opencv read image depends on the cv2.imread()function, cv2.imread()the function prototype is Mat imread( const string& filename, int flags=1 ), where Matis the most important Opencv data structure, which is defined as a class in Opencv in which a matrix of data stored by the image considered. As a class, it contains the attribute data Data image matrix, Dimension DIMS image matrix (such as 32 * 32 image dimension 2), and other image data matrix rows, Mat structure detailed description, reference may OpenCV-Mat Detailed structure . It should be noted that the function can be directly processed Opencv numpy data types - multi-dimensional arrays, which save the image in depth learning framework for us to use opencv provides a great convenience when operating images normalization , I guess because Mat and multi-dimensional arrays may represent matrix data.
  In cv2.imread(), which has two parameters, the first file name (index addresses) of the image, the second argument flagsspecifies in what format the read image, its value in the following situations:
  (1 ) flags=1, which is the default , representing the selected CV_LOAD_IMAGE_UNCHANGEDmode, i.e., using the original holding the read image format manner. For example the original image to be read is a grayscale image, the grayscale image is read.
  (2) flags=2, this representing the selected CV_LOAD_IMAGE_GRAYSCALEmode, i.e., the read image to a grayscale image format. No matter what the original format of the original image, after reading all of the results are converted to grayscale images.
  (3) flags=3, this representing the selected CV_LOAD_IMAGE_COLORmode, i.e., the read image in BGR format. No matter what the original format of the original image, after reading all of the results are converted into three-channel image BGR form.
  Of particular note are:cv2.imread()When the RGB image reading function, image format returned by the channel is not R、G、Barranged, but by B、G、Rorder of ! Of course, if you're using cv2.imread()to read RGB image, but also with cv2.imshow () to display an image, not a problem. But if you use the cv2.imread()read RGB image, and then the other libraries display method, it is very likely question arises , reference matplotlib & visdom image display problems .
  Usage examples are as follows:

image = cv2.imread('D:\\code\\datasets\\facades\\train\\714.jpg', flags=1)

  
  
  • 1

Hang imread function

  1. Image file name can not appear in the index address any Chinese characters
  2. If the windows system, separator index address of the file names need “\\”not just with“\” , as it should address D:\\code\\datasets\\facades\\train\\714.jpginstead D:\code\datasets\facades\train\714.jpg. This is to avoid confusion with the escape character file address, such as \train中的\tthe system default as an escape character .
  3. If the read image by numpy.asarray()converting into a multi-dimensional array type, i.e. an array of shape after converted to[Height, Width, Channels] , and which Tensorflowthe desired shape of the tensor model input [B,H,W,C]is very consistent, but for the PyTorchpurposes, it is desirable shape model is input tensor [B, C, H, W], then you need to Channeldimension before the transfer.

The image display cv2.imshow () function

  Opencv-python image display generally used cv2.imshow()functions, which consists of two parameters , which are a name of the image display window , the other is Mat type of data , which use the following example :

image = cv2.imread('D:\\code\\datasets\\facades\\train\\714.jpg', flags=1)
cv2.imshow('Example',image)
cv2.waitKey(0)

  
  
  • 1
  • 2
  • 3

In the image displaying results of Example window

Hang imshow function

  1. You must have two parameters, i.e. the first window name display parameter can not be omitted
  2. Generally need to add a back cv2.waitKey(0), the next step is represented by manually determined, or Show fleeting image appears unresponsive or image appears.
  3. Of particular note is that the image data of different formats, imshow()function different normalization operation of the automatic, details, see the Opencv Detailed imshow function . Keep in mind that, for the gradation value is (generally obtained from the result of such depth neural network) 32-bit floating-point type in terms of the image , imshow()the function will automatically each pixel value is multiplied by 255 and then be displayed , i.e. the original FIG pixel values range from the [0,1]map to the [0,255]thus Note: the desired neural network output display image matrix must be normalized to [0,1], without having to bother normalized [0,255]again displayed .
  4. There is a pit, if you use the cv2.imread()read RGB image, and then the other libraries display method, it is very image and the original image colors may appear completely inconsistent ! This is because cv2.imread()when the RGB image reading function, the return channel is not based image format R、G、Barranged, but by B、G、Rorder of! Examples of which may refer to the problem of image display matplotlib & visdom embodiment of FIG given here are also given period to B、G、Rconvert the order R、G、Bsequence code:
b,g,r=cv2.split(image)
image=cv2.merge([r,g,b])

  
  
  • 1
  • 2

Read and display image common error prompt

  1. cv2.error: OpenCV (4.1.0) C: \ projects \ opencv-python \ opencv \ modules \ highgui \ src \ window.cpp: 352: error: (-215: Assertion failed) size.width> 0 && size. height> 0 in function 'cv :: imshow':
    this error include the following two reasons: 1, the path there are Chinese characters; 2, the path used \to be mistaken for an escape character
  2. Fleeting image, or the image does not appear in response to: the cv2.imshow()after addingcv2.waitKey(0)
  3. If the image display black or white through such abnormal conditions may be due to cv2.imshow()the automatic normalization of the results, then processing may be required for specific reference to the Opencv function Detailed imshow

Save the image cv2.imwrite () function

  opencv_python used cv2.imwrite()functions to save the image, its use is an example:

img = cv2.imread("D:\\code\\datasets\\facades\\train\\716.jpg")
img = np.asarray(img).astype(float)
cv2.imwrite('D:\\code\\1.png',img)

  
  
  • 1
  • 2
  • 3

  cv2.imwrite()The first parameter in the function 'D:\\code\\1.png'is a desired image storage file name, and address, the file name extension format may be .jpgor .pngthe like , the second parameter imgrepresentative of the image data to be stored, or may be Numpy Mat type of Ndarray (multidimensional array) , in the sample code that will be transformed into Numpy type, which is convenient because the amount of conversion so as to be used in the sheet depth learning framework .

Hang imwrite function

  cv2.imwrite () function is that it pits needs to save the image of the image pixel gray value dynamic range[0,255] , already mentioned above, the output image for the neural network, such as a GAN which generates an image gray value dynamic range is usually [-1,1], 32-bit data format is, for cv2.imshow()purposes of function, the dynamic range required by the cv2.normalize()conversion function to [0,1]order the correct display ( cv2.imshow()function automatically process), and for the cv2.imwrite()purposes of the function, you need to take to change its dynamic range [0,255]in order to properly save.
  In summary, the network generates a depth image learning, if an image is displayed OpenCV like, need to be adjusted to a gray scale value of the dynamic range of [0,1], if you want to save the image by OpenCV, the required dynamic gradation value adjusted to the range [0,255].

Published 34 original articles · won praise 0 · Views 1001

Guess you like

Origin blog.csdn.net/forever_008/article/details/104053496