楼主: ReneeBK
1602 10

Raspberry Pi Computer Vision Programming [推广有奖]

  • 1关注
  • 62粉丝

VIP

已卖:4897份资源

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49635 个
通用积分
55.7537
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57805 点
帖子
4005
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

楼主
ReneeBK 发表于 2017-6-5 03:41:04 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币


本帖隐藏的内容

Raspberry Pi Computer Vision Programming.pdf (8.85 MB, 需要: 5 个论坛币)


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Programming Raspberry Computer Program compute Vision

沙发
ReneeBK(未真实交易用户) 发表于 2017-6-5 03:45:16
  1. Retrieving image properties

  2. We can retrieve and use many properties of an image with OpenCV functions. Have a look at the following code:

  3. import cv2
  4. img = cv2.imread('/home/pi/book/test_set/lena_color_512.tif',1)
  5. print img.shape
  6. print img.size
  7. print img.dtype
  8. The img.shape function returns the shape of an image, that is, its dimensions and the number of color channels. The output of the preceding code is as follows:

  9. pi@pi02 ~/book/code/chapter03 $ python prog1.py
  10. (512, 512, 3)
  11. 786432
  12. uint8
  13. If the image is colored, then img.shape returns a triplet containing the number of rows, columns, and channels in the image. Usually, the number of channels is three, representing the red, green, and blue channels. If the image is grayscale, then img.shape only returns the number of rows and columns. Try modifying the preceding code to read the image in grayscale mode and observe the output of img.shape.

  14. The img.size function returns the total number of pixels and img.dtype returns the image data type.
复制代码

藤椅
ReneeBK(未真实交易用户) 发表于 2017-6-5 03:45:50
  1. import cv2
  2. img1 = cv2.imread('/home/pi/book/test_set/4.2.03.tiff',1)
  3. img2 = cv2.imread('/home/pi/book/test_set/4.2.04.tiff',1)
  4. cv2.imshow('Image1',img1)
  5. cv2.waitKey(0)
  6. cv2.imshow('Image2',img2)
  7. cv2.waitKey(0)
  8. cv2.imshow('Addition',cv2.add(img1,img2))
  9. cv2.waitKey(0)
  10. cv2.imshow('Image1-Image2',cv2.subtract(img1,img2))
  11. cv2.waitKey(0)
  12. cv2.imshow('Image2-Image1',cv2.subtract(img2,img1))
  13. cv2.waitKey(0)
  14. cv2.destroyAllWindows()
复制代码

板凳
ReneeBK(未真实交易用户) 发表于 2017-6-5 03:46:24
  1. import cv2
  2. import numpy as np
  3. import time

  4. img1 = cv2.imread('/home/pi/book/test_set/4.2.03.tiff',1)
  5. img2 = cv2.imread('/home/pi/book/test_set/4.2.04.tiff',1)

  6. for i in np.linspace(0,1,40):
  7.   alpha=i
  8.   beta=1-alpha
  9.   print 'ALPHA ='+ str(alpha)+' BETA ='+str (beta)  
  10.   cv2.imshow('Image Transition',cv2.addWeighted(img1,alpha,img2,beta,0))
  11.   time.sleep(0.05)
  12.   if cv2.waitKey(1) == 27 :
  13.     break

  14. cv2.destroyAllWindows()
复制代码

报纸
ReneeBK(未真实交易用户) 发表于 2017-6-5 03:48:14
  1. Splitting and merging image colour channels

  2. On several occasions, we may be interested in working separately with the red, green, and blue channels. For example, we might want to build a histogram for every channel of an image.

  3. Note
  4. We will work separately with the different channels in Chapter 8, Histograms, Contours, Morphological Transformations, and Performance Measurement.

  5. Here, cv2.split() is used to split an image into three different intensity arrays for each color channel, whereas cv2.merge() is used to merge different arrays into a single multi-channel array, that is, a color image.

  6. The following example demonstrates this:

  7. import cv2
  8. img = cv2.imread('/home/pi/book/test_set/4.2.03.tiff',1)
  9. b,g,r = cv2.split (img)
  10. cv2.imshow('Blue Channel',b)
  11. cv2.imshow('Green Channel',g)
  12. cv2.imshow('Red Channel',r)
  13. img=cv2.merge((b,g,r))
  14. cv2.imshow('Merged Output',img)
  15. cv2.waitKey(0)
  16. cv2.destroyAllWindows()
复制代码

地板
ReneeBK(未真实交易用户) 发表于 2017-6-5 03:48:43
  1. Creating a negative of an image

  2. In mathematical terms, the negative of an image is the inversion of colors. For a grayscale image, it is even simpler! The negative of a grayscale image is just the intensity inversion, which can be achieved by finding the complement of the intensity from 255. A pixel value ranges from 0 to 255, and therefore, negation involves the subtracting of the pixel value from the maximum value, that is, 255. The code for the same is as follows:

  3. import cv2
  4. img = cv2.imread('/home/pi/book/test_set/4.2.07.tiff')
  5. grayscale = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  6. negative = abs(255-grayscale)
  7. cv2.imshow('Original',img)
  8. cv2.imshow('Grayscale',grayscale)
  9. cv2.imshow('Negative',negative)
  10. cv2.waitKey(0)
  11. cv2.destroyAllWindows()
复制代码

7
ReneeBK(未真实交易用户) 发表于 2017-6-5 03:50:00
  1. Logical operations on images

  2. OpenCV provides bitwise logical operation functions for images. We will have a look at the functions that provide the bitwise logical AND, OR, XOR (exclusive OR), and NOT (inversion) functionality. These functions can be better demonstrated visually with grayscale images. I am going to use barcode images in horizontal and vertical orientation for demonstration. Let's have a look at the following code:

  3. import cv2
  4. import matplotlib.pyplot as plt

  5. img1 = cv2.imread('/home/pi/book/test_set/Barcode_Hor.png',0)
  6. img2 = cv2.imread('/home/pi/book/test_set/Barcode_Ver.png',0)
  7. not_out=cv2.bitwise_not(img1)
  8. and_out=cv2.bitwise_and(img1,img2)
  9. or_out=cv2.bitwise_or(img1,img2)
  10. xor_out=cv2.bitwise_xor(img1,img2)

  11. titles = ['Image 1','Image 2','Image 1 NOT','AND','OR','XOR']
  12. images = [img1,img2,not_out,and_out,or_out,xor_out]

  13. for i in xrange(6):
  14.     plt.subplot(2,3,i+1)
  15.     plt.imshow(images[i],cmap='gray')
  16.     plt.title(titles[i])
  17.     plt.xticks([]),plt.yticks([])
  18. plt.show()
复制代码

8
ReneeBK(未真实交易用户) 发表于 2017-6-5 04:04:02
  1. Colorspaces and conversions

  2. A colorspace is a mathematical model used to represent colors. Usually, colorspaces are used to represent the colors in a numerical form and to perform mathematical and logical operations with them. In this book, the colorspaces we mostly use are BGR (OpenCV's default colorspace), RGB, HSV, and grayscale. BGR stands for blue, green, and red. HSV represents colors in Hue, Saturation, and Value format. OpenCV has a function cv2.cvtColor(img,conv_flag) that allows us to change the colorspace of an image (img), while the source and target colorspaces are indicated on the conv_flag parameter.

  3. If you remember, in Chapter 2, Working with Images, Webcams, and GUI, we discovered that OpenCV loads images in BGR format and matplotlib uses the RGB format for images. So, before displaying an image with matplotlib, we need to convert an image from BGR to RGB colorspace.

  4. Take a look at the following code. The program reads the image in color mode using cv2.imread(), which imports the image in the BGR colorspace. Then, it converts it to RGB using cv2.cvtColor(), and finally, it uses matplotlib to display the image:

  5. import cv2
  6. import matplotlib.pyplot as plt

  7. img = cv2.imread('/home/pi/book/test_set/4.2.07.tiff',1)
  8. img = cv2.cvtColor ( img , cv2.COLOR_BGR2RGB )
  9. plt.imshow ( img ) , plt.title ('COLOR IMAGE'), plt.xticks([]) , plt.yticks([])
  10. plt.show()
复制代码

9
ReneeBK(未真实交易用户) 发表于 2017-6-5 04:07:05
  1. Tracking in real time based on color

  2. Let's study a real-life application of this concept. In HSV format, it's much easier to recognize the color range. If we need to track a specific color object, we will have to define a color range in HSV, then convert the captured image in the HSV format, and then check whether the part of that image falls within the HSV color range of our interest. We can use the cv2.inRange() function to achieve this. This function takes an image, the upper and lower bounds of the colors, and then checks the range criteria for each pixel. If the pixel value falls in the given color range, the corresponding pixel in the output image is 0; otherwise it is 255, thus creating a binary mask.

  3. We can use bitwise_and() to extract the color range we're interested in using this binary mask thereafter. Take a look at the following code to understand this concept:

  4. import numpy as np
  5. import cv2

  6. cam = cv2.VideoCapture(0)

  7. while ( True ):
  8.         ret, frame = cam.read()

  9.         hsv=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)

  10.         image_mask=cv2.inRange(hsv,np.array([40,50,50]),np.array([80,255,255]))

  11.         output=cv2.bitwise_and(frame,frame,mask=image_mask)

  12.         cv2.imshow('Original',frame)
  13.         cv2.imshow('Output',output)

  14.         if cv2.waitKey(1) == 27:
  15.                 break

  16. cv2.destroyAllWindows()
  17. cam.release()
复制代码

10
小陆家嘴(真实交易用户) 发表于 2017-6-5 07:49:38
thanks for sharing

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-3 06:39