101 ps9

Computer Science 101: Lab #9

In this lab we’ll have a chance to revisit some of the imaging work from Lab #6 and related concepts from the lecture notes. As usual, there are some differences between what we’ll do and what is mentioned in the book. So please read the following carefully as you progress through the lab, before asking me a question about why something doesn’t work. Also, we have a short lab today, so don’t panic if you don’t get through the whole chapter.

  • Do setPicSize('small') so you can get the smaller picture size. Even if it’s not as small as the size mentioned in the textbook, at least it doesn’t take several seconds to get to your computer.
  • Because we’re working on a networked system, you may need to specify the full pathname for saving the images. For example, I could save my image by doing savePicture(pic, '/home/levys/mypic.jpg')If I leave out the /home/levys/, and I’m typing the command directly into the shell, Calico may try to save the images in an area to which I don’t have access, and I’ll get an error.
  • You’ll also get an error if you try to save a single picture as a GIF, so don’t worry about doing that. You can still save it as a PNG and JPG, though, and compare the sizes to see what kind of compression you’re getting. Oddly enough, I was able to save a sequence of pictures as a GIF animation (page 186), which I could then display in my browser.
  • On the Mac, you can’t click on the displayed images to see the x,y coordinates.
  • Typo p. 186 bottom (correction in red): newPic = makePicture(W, H, Color("black"))
  • Typo p. 187 top: newPic = makePicture(W, H, makeColor(R,G,B))   In general, you need to use makeColor when specifying R,G,B values, and Color when using a quoted color name.
  • When one program is a minor modification to the other, you can just name them using the standard “2” convention: e.g. drawGray.py and drawGray2.py for the Do This: programs on pages 189-190.
  • For the Shrinking & Enlarging exercise (p. 191), you can use any image you like from the Web.
  • From now on, I will start taking points off if your program isn’t nicely formatted with blank lines, as in the text.
  • For the picture-resizing program on p. 192, I got an error: the range() function expects an integer, but you’re not guaranteed that X/F and Y/F will be integers (for example, 100/3 isn’t). So I modified the shrink-factor lines as follows:
        newx = int(X/F)
        newy = int(Y/F)
  • For the blur/sharpen program on p. 194, it’s sufficient just to do the blurring using the code on the previous page. Don’t worry about sharpening the image unless you have extra time.
  • For the ball-recognizing program on p. 198, you don’t have to do more than recognize the big red ball that I will provide.
  • For the programs toward the end of the chapter, you’ll need to supply values like MAX (255) and T (however many seconds you want to run the loop for – I would just use a
    while True: instead of a timer).
  • Typo bottom of p. 197: should be for pixel in getPixels(picture): Also in that locateBall() function, you’ll want to have the function return a large default value in case it doesn’t locate the ball and the count is therefore zero. In general, you’ll have to fiddle with the constants in this program to get Scribby to reach the ball. As with the corral program, once you’ve got this to work, I’d make a video.
  • Typos bottom of p. 198: cruiseSpeed is not appropriate input for the turnLeft() turnRight() and turnLeft() functions. You can find the appropriate way to use those functions in the Myro Reference Manual.
  • For the final Do This: program on p. 199 I couldn’t get takePicture("blob") to work. So I instead used
        onPixels, avgX, avgY = getBlob()

    One cool thing to do would be to use setPixel() to show a small blue square at the avgX, avgY coordinates representing the center of the blob, before you try and change the color-tracking program to work with these coordinates:

    # Get the number of "on" pixels and their average position, in a pink blob
    onPixels, avgX, avgY = getBlob()
    # Take a picture to show the blob
    pic = takePicture()
    # Add a 5x5 blue square at the average blob position
    for x in range(-5, 5):
        for y in range(-5, 5):
            setPixel(pic, avgX+x, avgY+y, Color('blue'))
    # Show the picture with the blue square

    As you’ll see, if the blob is too far off-center from Scribby’s field of view, you can get a bogus blog, indicated by a blue square in the middle of nowhere. Use the onPixels count to distinguish between this blob (few on pixels) and true blob (many on pixels). Again, if you can get this tricky program to work, make a video!

Do as many of the Do This: exercises as you can, then answer the two questions below for your writeup (and of course send me any videos as well):

Question 9.1 Comparing a few PNG and JPG images, what is the average compression ratio you’re getting with JPEG?

Question 9.2 When you do myColor = pickAColor(), you’ll see the kind of color-wheel / settings dialog we talked about in class. As you move around in the wheel or change the Hue/Saturation/Value or Red/Green/Blue settings, the “Color name“ at the bottom changes. Explain the contents of this “name”.  Hint: You may want to review Question 1.2 from the first lab.

Question 9.3 Why do we add or subtract 1 to/from certain values, as in MAX+1 in the program at the top of p. 189, and the for x in range(1,X-1): in the code at the top of p. 194?