Week 12 - Interface and Application Programming

For week 12 our task was to construct a UI for an input or output device.

Telecentric Imaging Measurement UI

bolts

I was running short on time this week because I am wrapping up a final project for another class, but I cobbled together several pieces of code from the internet (mostly from here) to open and display an image in openCV, and place XY coordinates where mouse clicks occur. After writing lots of embedded C for this class and power electronics I forgot how python data types work and wasn’t able to get the contour bit working below (that would place lines between pairs of points). Along with that I would like to calculate line length (just basic trig), apply a conversion to scale from pixels to mm, and then display that value next to the line. A surprising amount of useful inspection could occur with a UI that simple.

This code requires python and openCV. I wrote it in Windows, but it ultimately will run on a Raspberry Pi which will also take the photos and stitch them together.

# importing the module
import cv2
import numpy as np

# function to display the coordinates of the points clicked on the image  
def click_event(event, x, y, flags, params):

    # checking for left mouse clicks
    if event == cv2.EVENT_LBUTTONDOWN:

        # displaying the coordinates on the Shell
        print(x, ' ', y)
        global lineArray
        lineArray = np.concatenate((lineArray,[x,y]),axis=0)

        # displaying the coordinates  on the image window
        font = cv2.FONT_HERSHEY_SIMPLEX
        cv2.putText(img, str(x) + ' , ' + str(y), (x,y), font, .5, (255, 0, 0), 1)
        cv2.imshow('image', img)

    # checking for right mouse clicks      
    if event==cv2.EVENT_RBUTTONDOWN:

        # displaying the coordinates on the Shell
        print(x, ' ', y)

        # displaying the coordinates on the image window
        font = cv2.FONT_HERSHEY_SIMPLEX
        b = img[y, x, 0]
        g = img[y, x, 1]
        r = img[y, x, 2]
        cv2.putText(img, str(b) + ' , ' + str(g) + ' , ' + str(r),  (x,y), font, .5,  (255, 255, 0), 1)
        cv2.imshow('image', img)

# driver function
if __name__=="__main__":

    # reading the image
    img = cv2.imread('stitched.jpg', 1)

    global lineArray
    lineArray =  np.array([(0,0)])

    # displaying the image
    cv2.imshow('image', img)

    # setting mouse hadler for the image and calling the click_event() function
    cv2.setMouseCallback('image', click_event)


    cv2.drawContours(img, [lineArray], 0, (255,255,255), 2)

    # wait for a key to be pressed to exit
    cv2.waitKey(0)

    # close the window
    cv2.destroyAllWindows()