计算机视觉图像处理
现实世界中的物体被映射到二维像素的网格中,这就是图像的形成方式。
图像的形成
图像可能包含 :1、颜色。2、形状细节。3、光照条件。4、相机与物体的距离。
让我们首先尝试理解灰度图像。灰度图像表示为范围从0到255的像素值的网格。
灰度图像
彩色图像稍微复杂一点,它包含三个彩色通道。每个通道的值范围为0到255。
1、红色通道
2、绿色通道
3、蓝色通道
我们知道图像是像素值的网格,每个像素值的范围从0到255。我们将通过指定特定网格的行和列来了解这些值。让我们使用带有openCV库的python来实现这一点。
# Import openCV,matplotlib and numpy. import cv2 import matplotlib.pyplot as plt import numpy as np img = cv2.imread('location/pic.jpg') # Let's specify each row and column value as follows x=10 y=10 ''' You will get 3 values for colour images [blue_color_values,green_color_values,red_color_values].each values represents corresponding B G and R Values,if the image is grayscale you will get a single value instead of three. ''' print(img[x,y]) ''' Getting dimension of your image. (height, width, number_of_color_channels). ''' print(img.shape)
现在做一些初级准备,如读取、复制Numpy数组中的图像、添加标题、将一种格式转换为另一种格式以及使用openCV python库绘制/显示或显示图像。
# importing(bringing-in) the essential libraries # An Open-source library for Computer Vision and image processings, hence the name openCV. import cv2 # To show the image/graphs etc. import matplotlib.pyplot as plt # Numpy helps us perform matrix (rows and columns of number) operation on images. Since, images are rows and columns of numbers. import numpy as np #The output of plotting commands is displayed inline within frontends like the Jupyter notebook. #%matplotlib inline #uncomment %matplotlib inline while using jupyter. # Reading in an image img = cv2.imread('images.jpg') # Always make a copy while operating on your image, so that your original image doesn't get affected. img1 = np.copy(img) #openCV reads/brings the image in BGR format (blue channel first, then green and finally red). #If you plot/show your image using plt.imshow('img1'), it will appear weired. # Finally plot/show the image plt.title('weired looking image in BGR format') plt.imshow(img1) # Reading in an image img = cv2.imread('images.jpg') # Always make a copy while operating on your image, so that your original image doesn't get affected. img1 = np.copy(img) #openCV reads/brings the image in BGR format (blue channel first, then green and finally red). #If you plot/show your image using plt.imshow('img1'), it will appear weired. # Finally plot/show the image plt.title('weired looking image in BGR format') plt.imshow(img1) #So you need to bring it to RGB format.By using cv2.cvtColor(img,cv2.COLOR_BGR2RGB). img_bgr =np.copy(img) img_rgb=cv2.cvtColor(img,cv2.COLOR_BGR2RGB) plt.title('origional image in RGB format') plt.imshow(img_rgb)
阈值化将灰度图像转换为二进制图像,其中将两个边界/级别分配给低于或高于指定阈值/边界的像素。
在创建蒙版时,我们将图像中的一些像素值设置为零或其他值。
# Thesholding a grayscale image. # Create a new copy of image. img_rgb1=np.copy(img_rgb) # Convert it to grayscale. img_gray=cv2.cvtColor(img_rgb1,cv2.COLOR_RGB2GRAY) # Define upper and lower threshold values for grayscale image. lower_values=np.array([70]) upper_values=np.array([250]) # create mask. mask = cv2.inRange(img_gray,lower_values,upper_values) #show/display it. #cmap parameter refers to colour mapping, #which is a function that maps the colours of one image to the colours of another image. plt.imshow(mask,cmap='gray')
我们可以使用不同的颜色值进行阈值处理,因为每种颜色我们需要3个不同的值。
# Thresholding a color image. img_rgb1 = np.copy(img_rgb) lower_values=np.array([70,70,10]) upper_values=np.array([250,250,250]) #creating mask mask = cv2.inRange(img_rgb1,lower_values,upper_values) #cmap parameter refers to colour mapping, #which is a function that maps the colours of one image to the colours of another image. plt.imshow(mask,cmap='gray')
不同的颜色空间
RGB颜色空间中不同光照条件下物体的颜色有所不同。在HSV颜色空间中我们可以避免这样的问题。
让我们看看RGB和HSV颜色空间的对比。
在不同的HSV光照条件下,色相值保持相对恒定。而值在变化。
假设我们有一个蓝色背景的披萨。我们需要为它创建一个蒙版。在RGB颜色空间中做起来相当容易。
img_copy = np.copy(image) # Do the above step as given in other codes blocks. lower = np.array([0,0,230]) upper = np.array([50,50,255]) mask = cv2.inRange(img_copy, lower, upper) plt.imshow(mask, cmap='gray')
img_copy = np.copy(img_rgb1) # Do the above step as given in other codes blocks. lower = np.array([0,0,225]) upper = np.array([250,250,255]) mask = cv2.inRange(img_copy, lower, upper) plt.imshow(mask, cmap='gray')
我们可以看出区别,阴影部分也考虑在黑色部分。这个蒙版比之前的要糟糕。
我们来比较一下
u_img = cv2.imread('uniform.png') # Always make a copy while operating on your image, so that your original image doesn't get affected. img_u = np.copy(u_img) img_rgb = cv2.cvtColor(img_u,cv2.COLOR_BGR2RGB) img_hsv = cv2.cvtColor(img_rgb,cv2.COLOR_RGB2HSV) # Converting rgb to hsv color space. img_hsv=cv2.cvtColor(u_img,cv2.COLOR_BGR2HSV) # selecting all rows,columns and hue. h=img_hsv[:,:,0] # selecting all rows,columns and saturation. s=img_hsv[:,:,1] # selecting all rows,columns and values. v=img_hsv[:,:,2] #ploting multiple images using subplot f,(ax1,ax2,ax3,ax4,ax5) = plt.subplots(1,5,figsize=(20,10)) ax1.imshow(img_rgb,cmap='gray') ax2.imshow(img_hsv,cmap='gray') ax3.imshow(h,cmap='gray') ax4.imshow(s,cmap='gray') ax5.imshow(v,cmap='gray')
输出1
un_img = cv2.imread('un_uniform.png') # Always make a copy while operating on your image, so that your original image doesn't get affected. img_un = np.copy(un_img) img_rgb1 = cv2.cvtColor(img_un,cv2.COLOR_BGR2RGB) img_hsv1 = cv2.cvtColor(img_rgb1,cv2.COLOR_RGB2HSV) # Converting rgb to hsv color space. img_hsv1=cv2.cvtColor(un_img,cv2.COLOR_BGR2HSV) # selecting all rows,columns and hue. h1=img_hsv1[:,:,0] # selecting all rows,columns and saturation. s1=img_hsv1[:,:,1] # selecting all rows,columns and values. v1=img_hsv1[:,:,2] #ploting multiple images using subplot f,(ax1,ax2,ax3,ax4,ax5) = plt.subplots(1,5,figsize=(20,10)) ax1.imshow(img_rgb1,cmap='gray') ax2.imshow(img_hsv1,cmap='gray') ax3.imshow(h1,cmap='gray') ax4.imshow(s1,cmap='gray') ax5.imshow(v1,cmap='gray')
输出2
在两个输出中,我们可以看到中间图像看起来相同。这是色彩,我们可以使用这些信息进行屏蔽或其他操作。
# Caution : Hue is angle ranges from 0 to 180. lower_values=np.array([90,10,113]) upper_values=np.array([180,255,255]) mask2 = cv2.inRange(img_hsv1,lower_values,upper_values) #cmap parameter refers to colour mapping, #which is a function that maps the colours of one image to the colours of another image. plt.imshow(mask2,cmap='gray')
更好的蒙版使用使用色调颜色空间