def processImage(dirname, filename): (p, e) = os.path.splitext(filename) p += ".txt" annotationFile = os.path.join(dirname, "annotate", p) builddirname = os.path.join(dirname, "build") (l, t, w, h) = (-1, -1, -1, -1) with file(annotationFile) as fp: for l in fp: l = l.strip() if not l: continue m = r1.match(l) m2 = r2.match(l) p2 = l.split(", ") if not m and m2: (l, t, w, h) = ( int(m2.group("left")), int(m2.group("top")), int(m2.group("width")), int(m2.group("height")), ) break pass pass if l >= 0 and t >= 0 and w >= 0 and h >= 0: imagefilename = os.path.join(dirname, filename) im2 = None img = cv2.imread(imagefilename) im2 = img[t : t + h, l : l + w] # im = Image.open(imagefilename) print filename, img.size, l, t, l + w, t + h # im2 = im.crop((l,t,w+l,h+t)) im3 = cv2.resize(im2, (128, 64), interpolation=cv2.INTER_LANCZOS4) im2 = None (n, e) = os.path.splitext(filename) n += "_canonical" if not os.path.exists(builddirname): os.mkdir(builddirname) # im3 = im2.resize((128, 64), Image.ANTIALIAS) # im3 = im2.resize((128, 64), Image.LINEAR) # cvimg = cv.CreateImageHeader(im3.size, cv.IPL_DEPTH_8U, 3) # cimg2 is a OpenCV image # cv.SetData(cvimg, im3.tostring()) numpyImg = nu.asarray(im3) hogc.gammaCorrect(numpyImg, 2.2, True) # print type(cvimg) # normalizedImg = cv2.cvtColor(numpyImg, cv2.COLOR_RGB2BGR) # normalizedImg = normalizeImage(numpyImg) # im3.save(os.path.join(builddirname, n + e)) # im3 = None cv2.imwrite((os.path.join(builddirname, n + e)), numpyImg) cvimg = None pass
def test_GammaCorrect(self): height =2 width = 5 grayImg = np.zeros((height,width,3), np.uint8) grayImg[:,:] = (128,128,128) hogc.gammaCorrect(grayImg, 2, False ) self.assertEqual(list(grayImg[1,2]) , [64,64,64]) hogc.gammaCorrect(grayImg, 2, reverse=True ) self.assertEqual(list(grayImg[1,4]) , [128,128,128])
def processImage(dirname, filename): if True: imagefilename = os.path.join(dirname, filename) builddirname = os.path.join(dirname, "build") im2 = None img = cv2.imread(imagefilename) print filename, img.size (n, e) = os.path.splitext(filename) n += "_normalized" if not os.path.exists(builddirname): os.mkdir(builddirname) numpyImg = nu.asarray(img) hogc.gammaCorrect(numpyImg, 2.2, True) normalizedImg = normalizeImage(numpyImg) cv2.imwrite((os.path.join(builddirname, n + e)), normalizedImg) cvimg = None pass
def hog(img, hogScheme, aHistTemplate, smAngle, smMag, descriptor): #print img.width, img.height imgCopy = cv2.resize(img, (128, 64)) numpyImg = np.asarray(imgCopy) hogc.gammaCorrect(numpyImg, 2.2, True) #normalizedImg = normalizeImage(numpyImg) convertedImg = cv2.cvtColor( numpyImg, cv2.COLOR_BGR2GRAY) h = convertedImg.shape[0] w = convertedImg.shape[1] assert(descriptor.shape[0] == hogScheme.numBlocksVertical * hogScheme.numBlocksHorizontal * hogScheme.numCellsInUnitPerSide * hogScheme.numCellsInUnitPerSide * 9) assert(descriptor.dtype == np.float32) hog = hogc.Hog(hogScheme, hogc.Hog.BlockGaussianWeightingPolicy.YesGaussian, hogc.Hog.BlockNormalizationPolicy.L2Normalization) for r in range(0, hogScheme.numBlocksVertical): for c in range(0, hogScheme.numBlocksHorizontal): histVec = hogc.FloatVec() hog.computeBlock( convertedImg,aHistTemplate, r, c, smAngle, smMag, False, histVec) hogc.copyToNpArray(histVec, (r*15 +c)*36, descriptor) return
def processImage2(builddir, dirname, filename): if True: imagefilename = os.path.join(dirname, filename) im2 = None img = cv2.imread(imagefilename) print filename, img.size im2 = cv2.resize(img, (128, 64), interpolation=cv2.INTER_LANCZOS4) img = None (n, e) = os.path.splitext(filename) n += "_canonical" if not os.path.exists(builddir): os.mkdir(builddir) numpyImg = nu.asarray(im2) hogc.gammaCorrect(numpyImg, 2.2, True) # normalizedImg = normalizeImage(numpyImg) cv2.imwrite((os.path.join(builddir, n + e)), numpyImg) cvimg = None pass