PYTHON-OPENCV (6)

Source: Internet
Author: User
Tags image flip lua

Next, this time write two topics:

  1. Color space conversion in OPENCV
  2. Geometric transformations in the OpenCV
Color space conversion in OPENCV

There are many kinds of color space, commonly rgb,cmy,hsv,hsi and so on. The three-channel diagram we normally refer to is the RGB figure.
posted a section of the introduction of Baidu Encyclopedia:

RGB (red-green-blue) is defined by the color of the human eye recognition of the space, can represent most of the color. However, in scientific research, the RGB color space is generally not used because its details are difficult to adjust digitally. It will be tonal, brightness, saturation of three to put together to show that it is difficult to separate. It is the most versatile, hardware-oriented color model. The model is used for color monitors and a large class of color video cameras

CMY is the color space used in industrial printing. It corresponds to RGB. The simple analogy of RGB comes from an object that glows, and CMY is based on reflected light. Specific applications such as printers: generally use four-color ink cartridges, that is, CMY plus black ink cartridges

HSV,HSI Two color spaces are presented for better digital processing of colors. There are many kinds of hsx color spaces, where x may be V or I, depending on the use and x meaning. H is the hue, S is the saturation, I is the intensity

For image processing, there are two common color-space transformations:

  1. Rgb->gray
  2. Rgb->hsv

Conversion API:

Cv2.cvtcolor (Input_image,flag), Falg is the conversion type

Code:

__author__ =' Gavinzhou '#-*-Coding:utf-8-*-ImportCv2# Read the original imagesim = Cv2.imread ("./images/1.png")ifLen (im.shape) = =3:Print "Original image is an RGB image"Else:Print "Original image is a gray image"# Convert to GrayifLen (im.shape) = =3: Im_gray = Cv2.cvtcolor (IM, cv2. Color_rgb2gray)# Convert to HSVifLen (im.shape) = =3: IM_HSV = Cv2.cvtcolor (IM, cv2. COLOR_BGR2HSV)# show the imagesCv2.imshow ("Ori", IM) cv2.imshow ("Gray", Im_gray) Cv2.imshow ("HSV", IM_HSV) Cv2.waitkey (8000) Cv2.destroyallwindows ()

Results:

The bottom right corner of this figure is my desktop ha, not the diagram shown

Geometric transformations in the OpenCV

This part of the more difficult, if you just want to achieve similar to the image flip and so on, we can not see this, direct use PIL , there is a simple implementation, direct call on the line, do not need to see this
If you want to do something more powerful, it's important to look at what's next.

Affine transformation of a spatial transformation corresponding to a matrix. A new coordinate position where a coordinate is transformed by a function:

is the original point coordinate (x,y) , after the conversion, the new coordinates are(x‘,y‘)

So, essentially a geometric transformation is the transformation of a point, which corresponds to a matrix is an affine transformation.

As an example:
To move the image to the right two pixels, is actually the original pixel position, for example (x,y) , to change (x+2,y+2) , so that the image is moved to the right of the whole two pixels

Below, 2x2 the matrix to illustrate the problem

Panning transformations

For the original coordinates, the (x,y) transformation is (x‘,y‘) , so the transformation matrix and the inverse matrix:

Scaling transformations

Enlarge (or shrink) the image horizontal (or smaller) SX times, Ordinate zoom in (or out) sy times, transform matrix and inverse matrix:

Rotation transformation

The image rotates counterclockwise around the origin by a corner, and its transformation matrix and inverse matrix (clockwise selection) are:

Code:

__author__ =' Gavinzhou '#-*-coding:utf-8-*-import cv2import NumPy as Npfrom matplotlib import Pyplot as plt# read the original imagesimg = Cv2.imread ("./images/1.png") rows, cols, channels = Img.shaperes = Cv2.resize (IMG, cols/2, rows/2)) #Translation: #1.Shiftm_shift = Np.float32 ([ [1, 0, +], [0, 1,]]) Img_shift = Cv2.warpaffine (img, M_shift, (cols, rows)) #2.Rotatem_rotate = cv2.getrotationmatrix2d ((cols/2, rows/2), -,1) img_rotate = Cv2.warpaffine (img, M_rotate, (cols, rows)) #3.Affinepts1 = Np.float32 ([ [[A ], [[] [[]]) Pts2 = Np.float32 ([ [Ten ], [+], [+]]) M_affine = Cv2.getaffinetransform (pts1,pts2) img_affine = Cv2.warpaffine (img, M_affine, (cols, rows)) #4.Perspectivepts3 = Np.float32 ([[368], [[+], [], [ 387], [389, 390]]) Pts4 = Np.float32 ([ 0, 0], [+, 0], [0, +], [+]]) m_perspective = Cv2.getperspectivetransform (Pts3, pts4) img_perspective = Cv2.warpperspective (img, m_perspective, ( cols, rows))Print ' shift:\n ', M_shiftPrint ' rotate:\n ', M_rotatePrint ' affine:\n ', M_affinePrint ' perspective:\n ', M_perspectiveplt.subplot (231), Plt.imshow (IMG), Plt.title (' src ') Plt.subplot (232), Plt.imshow (res), Plt.title (' scale ') Plt.subplot (233), Plt.imshow (Img_shift), Plt.title (' Shift ') Plt.subplot (234), Plt.imshow (img_rotate), Plt.title (' rotate ') Plt.subplot (235), Plt.imshow (Img_affine), Plt.title (' affine ') Plt.subplot (236), Plt.imshow (img_perspective), Plt.title (' perspective ') Plt.show ()

Results:

Console Print Results:

shift:[[   1.    0.  100.] [   0.    1.   50.]]rotate:[[  6.12323400e-17   1.00000000e+00   7.70000000e+01] [ -1.00000000e+00   6.12323400e-17   4.73000000e+02]]affine:[[  1.26666667   0.6        -83.33333333] [ -0.33333333   1.          66.66666667]]perspective:[[  1.05587376e+00   9.18151097e-02  -6.50969128e+01] [  4.69010049e-02   1.12562412e+00  -7.57920240e+01] [  1.83251448e-04   5.13337001e-04   1.00000000e+00]]

PYTHON-OPENCV (6)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.