官术网_书友最值得收藏!

Image manipulation

As explained in previous chapters, an image in the digital domain, such as on a computer, is made up of a grid-like structure with each grid cell termed a pixel. These pixels store a value representing information about the image. For a simple grayscale image, these pixels store an integer with range [0, 255]. Changing these pixel values also changes the image. One of the basic image manipulation techniques is modifying pixel values.

Let's start by displaying what is inside an image at pixel level. For simplicity, we will do analysis on a grayscale image:

# read an image 
img = cv2.imread('gray_flower.png')

The earlier code reads a grayscale image from a file, in this case the image is in PNG format. We can also convert from one type of image color format to another. In this case, to convert a colored image to grayscale, OpenCV provides functions as follows:

# converts rgb image to grayscale 
gray_output = cv2.cvtColor(color_input, cv2.COLOR_BGR2GRAY)

The previously shown code for displaying an image takes only a colored image as input, so to display a grayscale image there needs to be some modification:

def plot_cv_img(input_image,is_gray=False): 
"""
Takes in image with flag showing, if gray or not
Plots image using matplotlib
"""
# change color channels order for matplotlib
if not is_gray:
plt.imshow(cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB))
else:
plt.imshow(input_image, cmap='gray')

# For easier view, turn off axis around image
plt.axis('off')
plt.show()

The output of the previous code is as follows:

We can display a small patch from this image as follows, that shows pixel values:

# read the image
flower = cv2.imread('../figures/flower.png')

# convert to gray scale
gray_flower = cv2.cvtColor(flower, cv2.COLOR_BGR2GRAY)

# take out a patch of pixels
patch_gray = gray_flower[250:260, 250:260]

#plot the patch as well as print the values
plot_cv_img(patch_gray, is_gray=True)
print(patch_gray)

This will produce an image of the patch and prints out the value extracted in that patch:

Corresponding values are as follows, the lower values represent more darker regions: 

[[142 147 150 154 164 113  39  40  39  38]
 [146 145 148 152 156  78  42  41  40  40]
 [147 148 147 147 143  62  42  42  44  44]
 [155 148 147 145 142  91  42  44  43  44]
 [156 154 149 147 143 113  43  42  42  48]
 [155 157 152 149 149 133  68  45  47  50]
 [155 154 155 150 152 145  94  48  48  48]
 [152 151 153 151 152 146 106  51  50  47]
 [155 157 152 150 153 145 112  50  49  49]
 [156 154 152 151 149 147 115  49  52  52]]

These are the intensities for a pixel and is represented as a two-dimensional array. The range of each pixel value is 0-255. In order to modify image, we change these pixel values. A simple filtering for images is applying point operation targeted to multiply and add constants to each pixel values. We will see this type of filters in detail in the next section.

In this section, we saw basic IO extending our discussion from Chapter 1A Fast Introduction to Computer Vision. In further section, we will see how to modify these using filters which are used in image editing applications on smartphones, desktops and even on social media applications. 

主站蜘蛛池模板: 汉沽区| 肥东县| 旬邑县| 高陵县| 永仁县| 淮阳县| 徐闻县| 沐川县| 文水县| 宁晋县| 通州市| 吴旗县| 商城县| 万荣县| 河池市| 临泉县| 高陵县| 揭阳市| 芷江| 林口县| 和静县| 黄山市| 丽江市| 洛浦县| 彰武县| 横峰县| 云浮市| 林芝县| 衢州市| 樟树市| 贵溪市| 仙桃市| 南昌市| 桐庐县| 常州市| 芦溪县| 永年县| 华池县| 定边县| 墨玉县| 洛阳市|