1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# 1. Open 'Sudoku.jpg'; # 2. Create a Harris corner image for this image; # 3. Use Shi-Tomasi to find the location of the strongest corners. import numpy as np import cv2 from matplotlib import pyplot as plt from matplotlib import image as image import easygui I = cv2.imread("sudoku.jpg") gray = cv2.cvtColor(I,cv2.COLOR_BGR2GRAY) # Harris Corner code gray = np.float32(gray) dst = cv2.cornerHarris(gray,2,3,0.04) #result is dilated for marking the corners, not important dst = cv2.dilate(dst,None) # Threshold for an optimal value, it may vary depending on the image. I[dst>0.01*dst.max()]=[0,0,255] # Shi-Tomasi code corners = cv2.goodFeaturesToTrack(gray,25,0.01,10) corners = np.int0(corners) for i in corners: x,y = i.ravel() cv2.circle(I,(x,y),3,255,-1) # plt.imshow(I),plt.show() cv2.imshow('dst',I) # cv2.imshow("Shi-Tomasi", K) key = cv2.waitKey(0) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# 1. Open "Micro.jpg"; # 2. Use either thresholding or edge detection to create a binary mask; # 3. Extract and draw the boundary of the cell by finding the largest # contour in the image. import numpy as np import cv2 from matplotlib import pyplot as plt from matplotlib import image as image import easygui im = cv2.imread('Micro.jpg') imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(imgray,127,255,0) im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(im, contours, -1, (0,255,0), 3) # create hull array for convex hull points hull = [] # calculate points for each contour for i in range(len(contours)): # creating convex hull object for each contour hull.append(cv2.convexHull(contours[i], False)) # draw contours and hull points for i in range(len(contours)): color_contours = (0, 0, 255) # green - color for contours color = (255, 0, 0) # blue - color for convex hull # draw with contour cv2.drawContours(im, contours, i, color_contours, 1, 8, hierarchy) # draw with convex hull object cv2.drawContours(im, hull, i, color, 1, 8) cv2.imshow('Combined',im) key = cv2.waitKey(0) |