- Practical Computer Vision
- Abhinav Dadhich
- 544字
- 2021-06-30 18:54:48
Image manipulation
As explained in previous chapters, an image 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 1, A 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.
- GNU-Linux Rapid Embedded Programming
- OpenStack for Architects
- 一本書玩轉數據分析(雙色圖解版)
- 自動檢測與轉換技術
- 21天學通Java
- 菜鳥起飛系統安裝與重裝
- Excel 2007技巧大全
- R Machine Learning Projects
- Spatial Analytics with ArcGIS
- Mastering Ansible(Second Edition)
- Practical Network Automation
- PowerPoint 2003中文演示文稿5日通
- Raspberry Pi 3 Projects for Java Programmers
- Web滲透技術及實戰案例解析
- Learning OpenShift