PYTHON-OPENCV Tutorial -4__python

Source: Internet
Author: User
Tags bitwise

The OPENCV version requires more than 2.3.1

This paper mainly introduces the basic operation and algorithm of the image in OpenCV.

1. Basic operation

In a python environment, there are two ways to manipulate an image-either by manipulating the image with the function provided by the OPENCV or by NumPy to modify the image's array directly. Here, we recommend the use of NumPy, which should be clear and efficient.

First we read a picture:

<span style= "FONT-SIZE:14PX;" >>>> Import cv2
>>> import numpy as NP

>>> img = cv2.imread (' messi5.jpg ') </ Span>

We can then access the BGR value of the pixel directly through the row and column coordinates. For grayscale images, it is the grayscale value that accesses it.

>>> px = img[100,100]
>>> print px
[157 166]

# Accessing only blue pixel
>>&G T Blue = img[100,100,0]
>>> print Blue
157

You can then modify the pixel values in this way as well:

>>> img[100,100] = [255,255,255]
>>> print img[100,100]
[255 255 255]

However, this way of reading pixel values is very slow. Therefore, we recommend using NumPy functions--array.item () and Array.itemset () to access.

The following are recommended ways to access and modify:

# Accessing Red value
>>> img.item (10,10,2)

# Modifying red value
>>> img.itemset ((10,10,2)
>>> Img.item (10,10,2)
100
In addition to accessing the pixel values of the image, we can also get the attributes of the image--number of rows, columns, channels, image data types, and the number of pixel points.

By Img.shape This property, we can get a tuple that records the number of rows, columns, and channels of the image (the grayscale image returns only the number of rows, which can be used to distinguish between the color of the image or the grayscale).

The number of pixels can be obtained by img.size this function, and the data type of the image can be obtained by Img.dtype:

>>> Print Img.shape
(342, 548, 3)
>>> print img.size
562248
>>> Print Img.dtype
uint8
Sometimes, we often operate on a specific area of an image. For example, if we want to do a human eye detection of an image, then usually first to test the face, and then in the face of the region to detect the eyes. This improves the accuracy (because the eyes are always on the face: D) and increases the speed (the search area becomes smaller).

We call this specific area operation ROI (region of image), in the following code, we copy the ball from the graph to another region:

>>> ball = img[280:340, 330:390]
>>> img[273:333, 100:160] = Ball
The effect is as follows:


Sometimes we also need to separate the RGB channel of the graph, or to synthesize three separate channels into an RGB image:

>>> b,g,r = Cv2.split (IMG)
>>> img = Cv2.merge ((b,g,r))
Or

>>> B = img[:,:,0]
Suppose you want to set all the red pixel values to 0:

>>> img[:,:,2] = 0
In addition, the Cv2.split () function is very time-consuming to run, so it is best not to use it if necessary. Therefore, we recommend the second method.
Finally, we introduce a basic operation--boundary filling (Padding)

In the field of convolution calculation and 0-point compensation, padding has very great use. Padding mainly uses the Cv2.copymakeborder () function, which requires the following parameters:

SRC: a picture entered

Top,bottom,left,right: The width of the upper and lower left edges.

Bordertype: Please refer to the English page

The following code shows all the different types of boundaries:

Import cv2
import NumPy as NP from
matplotlib import pyplot as plt

BLUE = [255,0,0]

img1 = Cv2.imread (' Ope Ncv_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 ()


Translated from Http://docs.opencv.org/trunk/doc/py_tutorials/py_core/py_basic_ops/py_basic_ops.html#basic-ops

2. Basic algorithm

We can add two pictures by using the Cv2.add () function or the numpy operation Res=img1+img2. What needs to be explained is the Add () function two numbers added if greater than 255 the result is 255, and the direct addition of the will if greater than 255 will take the result to 255 modulo.

>>> x = Np.uint8 ([)]
>>> y = np.uint8 ([ten])

>>> print Cv2.add (x,y) # 250+10 = 260 =& Gt 255
[[255]]

>>> print x+y          # 250+10 = 256 = 4
[4]
In addition to the simple addition of two images, we can also mix two images in proportion to the following formula when mixing:

From the adjustment, we can see from one image slowly into another image, cv2.addweighted () mix two images according to the following formula:

<span style= "FONT-SIZE:18PX;" >IMG1 = Cv2.imread (' ml.png ')
img2 = Cv2.imread (' opencv_logo.jpg ')

DST = cv2.addweighted (Img1,0.7,img2, 0.3,0)

cv2.imshow (' DST ', DST)
cv2.waitkey (0)
cv2.destroyallwindows () </span>
The effect is as follows:

In addition, the OPENCV also supports the bitwise operation of the object element (with or not and OR). These algorithms can play an unexpected role in extracting the non rectangular region of the picture.

For example, I want to pick up the logo on the top of the OpenCV and put it in another picture, if the home bar two pictures added, then the color will change, if mixed with two pictures, will create a transparent effect, then you can skillfully use for the operation to solve this problem:

# Load Two images
img1 = cv2.imread (' messi5.jpg ')
img2 = Cv2.imread (' opencv_logo.png ')

# I want to put logo on  Top-left Corner, so I create a roi
rows,cols,channels = img2.shape
roi = img1[0:rows, 0:cols]

# now create a Mask of logos and create its inverse mask also
Img2gray = Cv2.cvtcolor (img2,cv2. Color_bgr2gray)
ret, mask = cv2.threshold (Img2gray, 255, Cv2. thresh_binary)
MASK_INV = Cv2.bitwise_not (mask) # now black-out the ' area '

logo in ROI
IMG1_BG = cv2.bitwise _and (Roi,roi,mask = MASK_INV)

# Take only region the logo from logo image.
IMG2_FG = Cv2.bitwise_and (Img2,img2,mask = mask)

# put logos in ROI and modify the main image
DST = Cv2.add (img1_ BG,IMG2_FG)
img1[0:rows, 0:cols] = DST

cv2.imshow (' res ', IMG1)
cv2.waitkey (0)
Cv2.destroyallwindows ()

The left is the mask image, and the right image is the final result.

This program requires that the logo must be black, it first use Cvtcolor to convert the original image into grayscale, and then use the threshold function, according to the gray scale of the images into two parts of the--logo and the background, where the gray value of the logo is 255 (white), the background is 0 (black), This is the mask image. Then, to mask, let the logo is 0, the background is 255, get MASK_INV image. Then let the upper-left corner of Messi5 and its own MASK_INV for the mask to do with the operation--that is, the mask is 0, the result is 0, the mask is greater than 0, the result is unchanged (this is the messi5 on the black with a logo, next to color). Then, let the logo and its own mask as a mask to do with the operation (the color of the part to pull out). Finally, use the Add function to add two pictures to get the right image (Black is used here (0,0,0), and what color is the color of this feature).

Translated from Http://docs.opencv.org/trunk/doc/py_tutorials/py_core/py_image_arithmetics/py_image_arithmetics.html#image-arithmetics


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.