Difference between revisions of "OpenCV4"

From ESE205 Wiki
Jump to navigation Jump to search
(Created page with "== Step 1: Install OpenCV on your Raspberry Pi and Import it to Python== Follow these [http://www.pyimagesearch.com/2015/02/23/install-opencv-and-python-on-your-raspberry-pi-2...")
 
 
(123 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Step 1: Install OpenCV on your Raspberry Pi and Import it to Python==
+
=== Overview ===
Follow these [http://www.pyimagesearch.com/2015/02/23/install-opencv-and-python-on-your-raspberry-pi-2-and-b/ instructions] to install OpenCV onto your Raspberry Pi using the terminal. After you have successfully installed OpenCV and imported the library to Python, you will be able to use the code <code>import cv2</code> at the top of your files to gain access to the OpenCV library.
+
This is a tutorial on how to install OpenCV4 and use camera module.
  
==Step 2: Enabling the Camera Module==
+
=== Materials/Prerequisites ===
Next, you will have to connect the camera on the Raspberry Pi. Start by inserting the ribbon cable into the camera slot on the board. After the cable has been inserted, use the terminal to enable the camera module on the OS. In the terminal, type in <code>sudo raspi-config</code
+
*Raspberry Pi
> and then select <code>Enable Camera</code>. Press Enter and then select <code>Finish</code>. You will then have to reboot and log back on to the Raspberry Pi. After this has been done, your camera module should be able to work on your Raspberry Pi.
 
  
==Step 3: Capturing an Image==
+
*Raspberry Pi Camera
After setting up the OpenCV library and the camera module, we will finally be able to capture images. First, type in <code>import picamera</code> at the top of the Python file. After this, typing in <code>camera = picamera.PiCamera()</code> will create an instance of the camera which you will be able to modify and use to take pictures. You can adjust the resolution by typing in <code>camera.resoultion = (resolution, resolution)</code>. 1024 by 1024 resolution was the parameter we used in our project. To get a preview of the image to be taken on the computer screen, type in <code>camera.start_preview()</code>. Add in a sleep (measured in seconds) to create a delay between the preview and the capture by typing in <code>time.sleep(seconds)</code>. Finally, capture the image by typing in <code>camera.capture('name of file')</code> and filling in the name of the file that will be saved. 
 
  
==Step 4: Analyze your image==
+
=== Process ===
Now that we are able to set up the camera and take pictures, we can use the images that are captured for new uses. One thing we can do with captured images is analyzing the pixels in the image. Pixels are stored as lists of Blue, Green, and Red color values. Before we do this, we must first allow our code to read the image we are analyzing. To read an image, we can type <code>img = cv2.imread('name of file', cv2.IMREAD_COLOR)</code>. The image we have just read is a list of lists of Blue, Green, and Red color values respectively. We can then use the instance of the image file to access a certain pixel in the image, for example <code>img[125, 275]</code>. We can also use ranges of pixels by typing <code>img[100:500] </code>. These pixels can also have their values changed, which would be reflected in the image used. For example, we can turn a set of pixels white by typing <code>img[100:500] = [255,255,255]</code>. We can also use a section of the image and replace another part of the image with that given section by typing in <code>img[100:500] = img[300:500]</code>
+
==== Install OpenCV====
 +
Follow these [https://www.pyimagesearch.com/2018/09/26/install-opencv-4-on-your-raspberry-pi/ instruction]<ref>Open CV instruction:[https://www.pyimagesearch.com/2018/09/26/install-opencv-4-on-your-raspberry-pi/ Link]</ref> to install OpenCV onto your Raspberry Pi using the terminal.
 +
 
 +
*'''Important note''': Don't do <code>make-j4</code> since the raspberry pi may freeze so it best to just do <code>make</code> or <code>make-j1</code>.
 +
 
 +
 
 +
After you have successfully installed OpenCV, you will be able to use <code>import cv2</code>. If you do it on the terminal, you want to put in these code <code>source ~/.profile</code> and then <code>workon cv</code>.
 +
 
 +
==== Capturing image====
 +
[[File:cam.PNG|400px|thumb|left|Taking picture code]]
 +
Following these step to [https://www.dexterindustries.com/howto/installing-the-raspberry-pi-camera/ set up your camera]<ref>Setting up camera:[https://www.dexterindustries.com/howto/installing-the-raspberry-pi-camera/ Link]</ref>
 +
 
 +
Note: Ignoring GoPiGo installation.
 +
 
 +
 
 +
Once the camera is set up, it is ready to take pictures. In order to capture image, follow these [https://medium.com/@petehouston/capture-images-from-raspberry-pi-camera-module-using-picamera-505e9788d609 instruction] <ref>Capturing Image:[https://medium.com/@petehouston/capture-images-from-raspberry-pi-camera-module-using-picamera-505e9788d609 Link]</ref>
 +
 
 +
 
 +
 
 +
 
 +
 
 +
Note:
 +
 
 +
**Use <code>sleep</code> (measured in seconds) to create a delay between the preview:  <code>time.sleep(seconds)</code>
 +
 
 +
**Another way to enable your camera is: go to the terminal → type in <code>sudo raspi-config</code> → select <code>Enable Camera</code> → press Enter → select <code>Finish</code> → reboot and log back on to the Raspberry Pi.
 +
 
 +
==== Edge Detector====
 +
[[File:Edges.PNG|350px|thumb|right|Sample Edge Detector]]
 +
Once you have a picture, you may want to use edge detection to detect the chessboard region of interest.
 +
 
 +
Follow these instruction for [https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_canny/py_canny.html Canny edge detection]<ref>Canny edge detection:[https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_canny/py_canny.html Link]</ref>
 +
 
 +
* Note: Scroll down to the Explanation section.
 +
 
 +
Follow this [https://docs.opencv.org/3.4.0/dd/d1a/group__imgproc__feature.html#ga04723e007ed888ddf11d9ba04e2232de/ Link]<ref name="a">Parameter:[https://docs.opencv.org/3.4.0/dd/d1a/group__imgproc__feature.html#ga04723e007ed888ddf11d9ba04e2232de/ Link]</ref>if you want to see its parameters.(search for Canny Edge)
 +
 
 +
In order to use build-in function in Open CV: remember to <code>import</code> cv2. In the instruction, the <code>import</code> cv2 <code>as</code>cv means that you can now type in cv instead of cv2 when using build-in function.  
 +
 
 +
 +
 
 +
Ex:
 +
To load image:
 +
*src = <code>cv.imread</code>(filename, cv.IMREAD_GRAYSCALE)
 +
 
 +
Then use:
 +
*dst = <code>cv.Canny</code>(src, 50, 200, None, 3)
 +
 
 +
==== Houghline Transformation====
 +
[[File:lines.PNG|550px|thumb|left|Sample Houghline Transformation]]
 +
Using the Canny Edge detection to find the region of interest(edges of the image) prior to the Houghline Transformation,one can now ready to use the Hougline Tranformation.
 +
 
 +
 
 +
The process should be: capturing an image→Canny Edge Detection→Houghline Transformation.
 +
 
 +
 
 +
Follow the following instruction for [https://docs.opencv.org/3.4.0/d9/db0/tutorial_hough_lines.html Houghline Transformation].<ref>Houghline Transformation:[https://docs.opencv.org/3.4.0/d9/db0/tutorial_hough_lines.html Link]</ref>
 +
 
 +
Follow this [https://docs.opencv.org/3.4.0/dd/d1a/group__imgproc__feature.html#ga04723e007ed888ddf11d9ba04e2232de/ Link]<ref name="a" />if you want to see its parameters.(search for Hough Line)
 +
 
 +
 
 +
Example:
 +
 
 +
<code>lines = cv.HoughLines(dst, 1, np.pi / 180, 150, None, 0, 0)</code>
 +
 
 +
=== Sample Projects ===
 +
 
 +
*ESE205-CVChess/board_Recognition.py <ref> ESE205-CVChess/board_Recognition.py :[https://github.com/rjgoodloe/ESE205-CVChess/blob/master/board_Recognition.py Links]</ref>
 +
 
 +
*DE3-ROB1-CHESS/perception.mainDetect<ref> DE3-ROB1-CHESS/perception.mainDetect: [https://de3-rob1-chess.readthedocs.io/en/latest/_modules/perception/mainDetect.html#Perception.makeBoard/ Links]</ref>
 +
 
 +
=== Authors ===
 +
*Nhut Dang
 +
 
 +
*Robert Goodloe
 +
 
 +
*Ethan Shry(TA)
 +
 
 +
Fall 2018
 +
 
 +
=== Group Link ===
 +
[https://classes.engineering.wustl.edu/ese205/core/index.php?title=CV_Chess Group page]
 +
 
 +
[https://classes.engineering.wustl.edu/ese205/core/index.php?title=CV_Chess_Logs Group weekly log]
 +
 
 +
=== References ===
  
 
[[Category:HowTos]]
 
[[Category:HowTos]]
 
[[Category:Raspberry_Pi]]
 
[[Category:Raspberry_Pi]]

Latest revision as of 18:52, 12 December 2018

Overview

This is a tutorial on how to install OpenCV4 and use camera module.

Materials/Prerequisites

  • Raspberry Pi
  • Raspberry Pi Camera

Process

Install OpenCV

Follow these instruction[1] to install OpenCV onto your Raspberry Pi using the terminal.

  • Important note: Don't do make-j4 since the raspberry pi may freeze so it best to just do make or make-j1.


After you have successfully installed OpenCV, you will be able to use import cv2. If you do it on the terminal, you want to put in these code source ~/.profile and then workon cv.

Capturing image

Taking picture code

Following these step to set up your camera[2]

Note: Ignoring GoPiGo installation.


Once the camera is set up, it is ready to take pictures. In order to capture image, follow these instruction [3]



Note:

    • Use sleep (measured in seconds) to create a delay between the preview: time.sleep(seconds)
    • Another way to enable your camera is: go to the terminal → type in sudo raspi-config → select Enable Camera → press Enter → select Finish → reboot and log back on to the Raspberry Pi.

Edge Detector

Sample Edge Detector

Once you have a picture, you may want to use edge detection to detect the chessboard region of interest.

Follow these instruction for Canny edge detection[4]

  • Note: Scroll down to the Explanation section.

Follow this Link[5]if you want to see its parameters.(search for Canny Edge)

In order to use build-in function in Open CV: remember to import cv2. In the instruction, the import cv2 ascv means that you can now type in cv instead of cv2 when using build-in function.


Ex: To load image:

  • src = cv.imread(filename, cv.IMREAD_GRAYSCALE)

Then use:

  • dst = cv.Canny(src, 50, 200, None, 3)

Houghline Transformation

Sample Houghline Transformation

Using the Canny Edge detection to find the region of interest(edges of the image) prior to the Houghline Transformation,one can now ready to use the Hougline Tranformation.


The process should be: capturing an image→Canny Edge Detection→Houghline Transformation.


Follow the following instruction for Houghline Transformation.[6]

Follow this Link[5]if you want to see its parameters.(search for Hough Line)


Example:

lines = cv.HoughLines(dst, 1, np.pi / 180, 150, None, 0, 0)

Sample Projects

  • ESE205-CVChess/board_Recognition.py [7]
  • DE3-ROB1-CHESS/perception.mainDetect[8]

Authors

  • Nhut Dang
  • Robert Goodloe
  • Ethan Shry(TA)

Fall 2018

Group Link

Group page

Group weekly log

References

  1. Open CV instruction:Link
  2. Setting up camera:Link
  3. Capturing Image:Link
  4. Canny edge detection:Link
  5. 5.0 5.1 Parameter:Link
  6. Houghline Transformation:Link
  7. ESE205-CVChess/board_Recognition.py :Links
  8. DE3-ROB1-CHESS/perception.mainDetect: Links