import cv2 # import numpy as np image = '459x600-monalist.PNG' img = cv2.imread(image) cv2.imshow('Mona Lisa',img) cv2.waitKey( 0 ) smooth1_img = cv2.GaussianBlur( img, (7,7), 0 ) cv2.imshow('Mona Lisa', smooth1_img) cv2.waitKey( 0 ) # more blurring smooth2_img = cv2.GaussianBlur( img, (23,23), 0 ) cv2.imshow('Mona Lisa', smooth2_img) cv2.waitKey( 0 ) # in class - # convert image to a different color space - here we # convert from RGB to grayscale gray = cv2.cvtColor( smooth2_img, cv2.COLOR_BGR2GRAY ) cv2.imshow('Mona Lisa', gray ) cv2.waitKey( 0 ) # we will do edge detection later in the semester here is # a module, Canny, in openCV that does just that on a # grayscale image 'img' # it has two parameters in addition to the image img. 'lower' # and 'upper'. # Canny( img, lower, upper ) # lower and upper between [0,255] are just thresholds on how sensitive you are # to detecting edges, for now we will use, 30 and 100. edges = cv2.Canny( gray, 30, 100 ) cv2.imshow('Mona Lisa', edges ) cv2.waitKey( 0 ) # for cropping, we did it directly in class, here in this # example we will define the arguments of cropping via # x, y coordinates to make things more clear, typically, # the origin starts at the top left of an image, so that is # (0,0). The y coordinate system then goes 'downwards' so that # is the only really confusing part of displaying images in # openCV. # shape[1] x shape[0] is the dimension of the original image print "Dimension of the image are ( %d, %d )" % (img.shape[1], img.shape[0]) # h = denotes height # w = denotes width y1 = img.shape[0]/4 - 130 h = 260 y2 = y1+h x1 = img.shape[1]/2 - 100 w = 220 x2 = x1+w; # here is a cropped image this will at least give you a start # to complete the exercises some more hints further below # on that. # slightly different from class in that we will assign the variables # above to make the slicing more apperant img_cropped = img[ y1:y2, x1:x2 ] cv2.imshow('cropped', img_cropped ) cv2.waitKey(0) # HINTS: to select a region of interest (roi) in an image you can use the idea # of cropping, and store it to a new image variable # then after defining and tranforming this roi, you can then copy it # to the original image. # # 1) roi = ... define it similar to the slicing idea above ... # 2) tranform the roi # 3) set a new image (e.g., newImage_to the old untranformed image) # 4) newImage[ ... ] = transformedimage # cv2.destroyAllWindows()