def analyzeImage(f, name): im = Image.open(f) try: if (im.size[0] == 1 or im.size[1] == 1): return print(name + ' : ' + str(im.size[0]) + ',' + str(im.size[1])) le = 1 if (type(im.getpixel((0, 0))) == type((1, 2))): le = len(im.getpixel((0, 0))) gray = cv.CreateImage(cv.Size(im.size[0], im.size[1]), 8, 1) edge1 = cv.CreateImage(cv.Size(im.size[0], im.size[1]), 32, 1) edge2 = cv.CreateImage(cv.Size(im.size[0], im.size[1]), 8, 1) edge3 = cv.CreateImage(cv.Size(im.size[0], im.size[1]), 32, 3) for h in range(im.size[1]): for w in range(im.size[0]): p = im.getpixel((w, h)) if (type(p) == type(1)): gray[h][w] = im.getpixel((w, h)) else: gray[h][w] = im.getpixel((w, h))[0] cv.CornerHarris(gray, edge1, 5, 5, 0.1) cv.Canny(gray, edge2, 20, 100) cv.NamedWindow("win") cv.ShowImage("win", gray) cv.NamedWindow("win2") cv.ShowImage("win2", edge1) cv.NamedWindow("win3") cv.ShowImage("win3", edge2) cv.WaitKey() f.close() except Exception, e: print e print 'ERROR: problem handling ' + name
def Harris(image): gray = cv.CreateImage(cv.GetSize(image), 8, 1) harris = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_32F, 1) cv.CornerHarris(gray, harris, 5, 5, 0.1) #for y in range(0, 640): # for x in range(0, 480): # harris = cv.Get2D(harris_c, y, x) # get the x,y value # # check the corner detector response # if harris[0] > 10e-06: # # draw a small circle on the original image # cv.Circle(harris, (x, y), 2, cv.RGB(155, 0, 25)) # I recommend using cvMinMaxLoc to find the min and max values and then use # cvConvertScale to shift the range [min..max] to [0..255] into an 8bit # image. print cv.MinMaxLoc(harris) gray2 = cv.CreateImage(cv.GetSize(image), 8, 1) cv.ConvertScale(harris, gray2) # scale/shift? cv.NamedWindow('Harris') cv.ShowImage('Harris', gray)
import cv2.cv as cv import numpy as np im = cv.LoadImage('/home/pi/Documents/Lab3/foto4.png', cv.CV_LOAD_IMAGE_GRAYSCALE) dst_32f = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_32F, 1) neighbourhood = 3 aperture = 3 k = 0.01 maxStrength = 0.0 threshold = 0.01 nonMaxSize = 3 cv.CornerHarris(im, dst_32f, neighbourhood, aperture, k) minv, maxv, minl, maxl = cv.MinMaxLoc(dst_32f) dilated = cv.CloneImage(dst_32f) cv.Dilate( dst_32f, dilated ) # By this way we are sure that pixel with local max value will not be changed, and all the others will localMax = cv.CreateMat(dst_32f.height, dst_32f.width, cv.CV_8U) cv.Cmp( dst_32f, dilated, localMax, cv.CV_CMP_EQ ) #compare allow to keep only non modified pixel which are local maximum values which are corners. threshold = 0.01 * maxv cv.Threshold(dst_32f, dst_32f, threshold, 255, cv.CV_THRESH_BINARY)
cv.SaveImage("green edge Laplace.png", dst) cv.WaitKey(0) enter = raw_input("Mostrar Harrys") #harrys dst_32f = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_32F, 1) neighbourhood = 3 aperture = 3 k = 0.01 maxStrength = 0.0 threshold = 0.01 nonMaxSize = 3 cv.CornerHarris(img, dst_32f, neighbourhood, aperture, k) minv, maxv, minl, maxl = cv.MinMaxLoc(dst_32f) dilated = cv.CloneImage(dst_32f) cv.Dilate( dst_32f, dilated ) #nos aseguramos que el pixel local su maximo valor no cambia y todos los otros pixeles si localMax = cv.CreateMat(dst_32f.height, dst_32f.width, cv.CV_8U) cv.Cmp( dst_32f, dilated, localMax, cv.CV_CMP_EQ ) #comparamos y guardamos solo los pixeles modificados los que son los maximos valores locales que son esquinas threshold = 0.01 * maxv cv.Threshold(dst_32f, dst_32f, threshold, 255, cv.CV_THRESH_BINARY)