FRI I ROS & OpenCV Instructor: Justin Hart - - PowerPoint PPT Presentation

fri i
SMART_READER_LITE
LIVE PREVIEW

FRI I ROS & OpenCV Instructor: Justin Hart - - PowerPoint PPT Presentation

CS 309: Autonomous Robots FRI I ROS & OpenCV Instructor: Justin Hart http://justinhart.net/teaching/2020_spring_cs309/ OpenCV Open Computer Vision System Officially launched in 1999 at Intel Research Original purpose


slide-1
SLIDE 1

CS 309: Autonomous Robots FRI I

ROS & OpenCV

Instructor: Justin Hart

http://justinhart.net/teaching/2020_spring_cs309/

slide-2
SLIDE 2

OpenCV

  • Open Computer Vision System
  • Officially launched in 1999 at Intel Research
  • Original purpose – Studying CPU-intensive applications
  • Willow Garage (kicked off in 2006)
  • Hosted
  • OpenCV
  • Point Cloud Library
  • OpenCV is Open Source!
  • BSD license
  • Now run by OpenCV.org
slide-3
SLIDE 3

OpenCV

  • Includes a wealth of low-level vision processing
  • In this class, we will do a few basics
  • Color channel filtering
  • Blob detection
  • Supports 2D and 3D vision
  • Has many features
  • Stereo vision
  • Structure from motion
  • Face recognition
  • Motion tracking
  • And tries to stay on top of incorporating tools that are of general

broad interest and use

slide-4
SLIDE 4

The Very Basics

  • We learn an introductory technique called

“Color Segmentation” or “Color Blob Detection”

  • Color channels
  • Color channel subtraction
  • Thresholding
  • Contour Detection
  • Masking
slide-5
SLIDE 5

OpenCV and ROS

  • OpenCV and ROS use different formats
  • cv_bridge helps solve this
slide-6
SLIDE 6

Color Channels

  • Colors are represented using

different systems

  • BGR → Blue, Green, Red
  • Also RGB, RGBA, BGRA
  • Alpha → Transparency
  • HSV →

Hue, Saturation, Value

  • Others get more complex
  • HSL →

Hue, Saturation, Luma

  • How brightly lit something

appears

slide-7
SLIDE 7

BGR

  • Each pixel gets a color

intensity for each channel

  • This is how bright that

channel is

  • The blend of blue, green,

and red becomes the final color

slide-8
SLIDE 8

cv::Mat

  • OpenCV stores images in its matrix type
  • cv::mat
  • A matrix has rows and columns of numbers
  • For an image, this is how tall and wide the image is
  • In OpenCV each matrix cell (number) can have more than
  • ne channel
  • The matrices have types which dictate this representation
  • CV_8UC3 - CV_8UC1, CV_8UC2, CV_8UC3, CV_8UC4
  • CV_8SC1
  • CV_64F
  • BGR images are stored in CV_8UC3
  • OpenCV, 8 bits per channel, unsigned characters, 3 channels
slide-9
SLIDE 9

Color Values

  • Unsigned character → 8 bits long
  • 0..255
  • Highest intensity 255
  • Lowest intensity 0
  • As the intensity goes higher, the color in that channel

becomes brighter

slide-10
SLIDE 10

cv::split()

Break an image with several channels into several 1 channel images std::vector<cv::Mat> chans; cv::split(image, chans);

slide-11
SLIDE 11

Input Image and Three Channels

slide-12
SLIDE 12

Color Channel Images

  • Notice that as the intensity of a channel goes up, the

greyscale pixels become brighter.

  • Look back at the image in the previous slide, each cup

becomes brightest in one particular image.

slide-13
SLIDE 13

Example, finding the blue cup

  • Let’s find the blue cup
slide-14
SLIDE 14

Color Channel Subtraction

  • cv::subtract()
  • Subtract one cv::Mat from another
  • cv::Mat bMinusG
  • cv::subtract(chans[0], chans[1], bMinusG)

Blue channel minus red channel Blue channel minus green channel

slide-15
SLIDE 15

Picking out the blue pixels

Blue minus red is really bright! We can just pick the pixels that are bright enough!

Blue channel minus red channel Blue channel minus green channel

slide-16
SLIDE 16

Image Thresholding

  • There are pixels illuminated that are not the cup
  • But the brightest ones belong to the cup
  • Pick the pixels only as bright as some value
  • Image thresholding
  • Specify a minimum and maximum value

cv::threshold(input_image, output_image, threshold_value, value_when_above_threshold, threshold_type) We will use only cv::THRESH_BINARY

cv::Mat bThresh; cv::threshold(bMinusR, bThresh, 50, 255, cv::THRESH_BINARY);

slide-17
SLIDE 17
slide-18
SLIDE 18

Contour Detection

  • Detects closed image contours
  • “Blobs” in the image – connected areas of illuminated pixels
slide-19
SLIDE 19

Find the area of each contour

cv::contourArea(contours[i])

  • Computes the area of the contour
  • In your program, find the biggest contour and track it
  • This gets rid of the small, noisy stuff
slide-20
SLIDE 20

Image Masking

  • Selecting only certain pixels
  • Uses a “mask”
  • A 1-channel image where the pixels to be included are turned “on” (usually 255)

cv::Mat mask = cv::Mat::zeros(image.rows, image.cols, CV_8UC1); cv::drawContours(mask, contours, maxSizeContour, cv::Scalar(255), cv::LineTypes::FILLED, 8, hierarchy);

  • copyTo can be used with a mask

image.copyTo(blueCupImg, mask);

slide-21
SLIDE 21
slide-22
SLIDE 22

For Homework 3

  • Create a ROS package which puts the blue, green, and red cups

together.

  • Must build under catkin build with g++

– The three separate cups get published on ROS topics

/color_filter/blue_cup /color_filter/green_cup /color_filter/red_cup

  • The three cups together get published on a ROS topic in a composite

image /color_filter/cups

slide-23
SLIDE 23

rviz – The ROS Visualizer

  • rosrun rviz rviz
  • What you get should look something like this.
slide-24
SLIDE 24
slide-25
SLIDE 25
  • Clicking the add button will allow you to add things

to visualize.

slide-26
SLIDE 26
slide-27
SLIDE 27
  • “By topic” will list the available topics.
slide-28
SLIDE 28
slide-29
SLIDE 29
  • Expand until you see what you are interested in.
slide-30
SLIDE 30
slide-31
SLIDE 31
  • Clicking “Okay” should add it to the interface.
slide-32
SLIDE 32
slide-33
SLIDE 33
  • You can rearrange and resize windows as

appropriate.

slide-34
SLIDE 34
slide-35
SLIDE 35
  • Use rviz to help develop and debug your

homework.

slide-36
SLIDE 36

What should my program do?

  • You should write 1 (and only 1) ROS node
  • It should publish 4 topics

– /color_filter/blue_cup – /color_filter/green_cup – /color_filter/red_cup – /color_filter/cups

  • The first three should show only the blue, green,

and red cup, respectively.

  • The third should show all three together.
slide-37
SLIDE 37

How should my program do this?

  • Finding the BLUE cup is demonstrated in the

example on justinhart.net

– You may need to modify your package.xml

and CMakelists.txt as per the Piazza discussion

  • Finding the RED and GREEN cups is a variation on

this

slide-38
SLIDE 38

How should my program do this?

  • /color_filter/cups contains all three, though!
  • RIGHT! I’m not going to tell you exactly how to do

this, because it would make the homework too easy for you to learn anything.

  • But you will use cv::bitwise_or to do it

– And if you Google cv::bitwise_or, and

understand how you found the blue,green, and red cups; the example for bitwise_or is almost exact directions on how this works.

slide-39
SLIDE 39

So.. display the cups, right?

  • NO!!

– Publish a ROS TOPIC for each of the the

blue, green, and red cups, respectively, and one containing all three.

– You should be able to see this topic using

rviz

– In fact, turn off the cv::imshows in your c++

code before you submit (unless you’ve already submitted).

slide-40
SLIDE 40

How do I publish the ROS topic?

  • cv_bridge

– http://wiki.ros.org/cv_bridge/Tutorials/Usin

gCvBridgeToConvertBetweenROSImages AndOpenCVImages

– Then publish the topic as in our previous

lectures.

– See also:

https://stackoverflow.com/questions/27080 085/how-to-convert-a-cvmat-into-a-sensor- msgs-in-ros

slide-41
SLIDE 41

My ROS topic complains that I’ve advertised more than once

  • Call advertise() in your main, and publish() in your

callback.

– publish() sends the image – advertise() says that you will publish on a

topic

slide-42
SLIDE 42

My ROS topic complains that I’ve advertised more than once

  • Call advertise() in your main, and publish() in your

callback.

– publish() sends the image – advertise() says that you will publish on a

topic

  • And you can’t advertise the same topic

more than once per node.