def crop(image, offset, size): w, h = size # If there will be a border, use CopyMakeBorder. # Setting ROI, no border is created and resulting image is smaller if offset[0]>0 or \ offset[1]>0 or \ offset[0]+cv.GetSize(image)[0]<w or \ offset[1]+cv.GetSize(image)[1]<h: finalImg = cv.CreateImage((w, h), cv.IPL_DEPTH_8U, 3) # offset may have negative values, if there will be a right/bottom border useOffset = (max(0, offset[0]), max(0, offset[1])) # Need to crop first as CopyMakeBorder will complain if the source is too big for the destination # (The ROI is the opposite of the offset) cv.SetImageROI(image, (-offset[0], -offset[1], w, h)) cv.CopyMakeBorder(image, finalImg, useOffset, GAP_BORDER) return finalImg else: cv.SetImageROI(image, (-offset[0], -offset[1], w, h)) return image
def size_image(img, imgsize): # check if we need to crop out ROI roiWidth = img.width roiHeight = img.height if (img.width > imgsize[1]): roiWidth = imgsize[1] if (img.height > imgsize[0]): roiHeight = imgsize[0] roi = (0, 0, roiWidth, roiHeight) cv.SetImageROI(img, roi) imgTrim = cv.CreateImage((roi[2], roi[3]), img.depth, img.nChannels) cv.Copy(img, imgTrim) # check if we need to pad padSize = 0 padSize = max(padSize, imgsize[0] - imgTrim.height) padSize = max(padSize, imgsize[1] - imgTrim.width) if padSize == 0: # no padding needed return imgTrim else: padSize = int(round((padSize + .5) / 2.)) # copy make border imgPad = cv.CreateImage( (imgTrim.width + 2 * padSize, imgTrim.height + 2 * padSize), img.depth, img.nChannels) cv.CopyMakeBorder(imgTrim, imgPad, (0, 0), 0) roi = (0, 0, imgsize[1], imgsize[0]) cv.SetImageROI(imgPad, roi) imgFinal = cv.CreateImage((roi[2], roi[3]), img.depth, img.nChannels) cv.Copy(imgPad, imgFinal) return imgFinal
def smoothNoise1(image, bgcolor=255): temp = cv.CreateImage((image.width + 2, image.height + 2), image.depth, image.nChannels) result = cv.CreateImage(cv.GetSize(image), image.depth, image.nChannels) cv.CopyMakeBorder(image, temp, (1, 1), 0, bgcolor) for x in xrange(1, image.width + 1): for y in xrange(1, image.height + 1): if temp[y + 1, x] == temp[y - 1, x] and temp[y, x] != temp[y + 1, x]: result[y - 1, x - 1] = temp[y + 1, x] elif temp[y, x + 1] == temp[y, x - 1] and temp[y, x] != temp[y, x + 1]: result[y - 1, x - 1] = temp[y, x + 1] else: result[y - 1, x - 1] = temp[y, x] return result
def smooth_constborder(A, xwin=5, ywin=5, val=0): """ Smooths A with a Gaussian kernel (with window size [XWIN,YWIN]), handling the borders of A by using VAL as the intensity value used for pixels outside of A. Input: IplImage A: Output: IplImage A_smoothed. """ wA, hA = cv.GetSize(A) A_big = cv.CreateImage((wA + 2 * xwin, hA + 2 * ywin), A.depth, A.channels) # Pass '0' as bordertype due to undocumented OpenCV flag IPL_BORDER_CONSTANT # being 0. Wow! cv.CopyMakeBorder(A, A_big, (xwin, ywin), 0, val) cv.Smooth(A_big, A_big, cv.CV_GAUSSIAN, param1=xwin, param2=ywin) cv.SetImageROI(A_big, (xwin, ywin, wA, hA)) return A_big
def smooth(I, xwin, ywin, bordertype=None, val=255.0): """ Apply a gaussian blur to I, with window size [XWIN,YWIN]. If BORDERTYPE is 'const', then treat pixels that lie outside of I as VAL (rather than what OpenCV defaults to). Input: IplImage I: """ w, h = cv.GetSize(I) if bordertype == 'const': Ibig = cv.CreateImage((w + 2 * xwin, h + 2 * ywin), I.depth, I.channels) cv.CopyMakeBorder(I, Ibig, (xwin, ywin), 0, value=val) cv.SetImageROI(Ibig, (xwin, ywin, w, h)) else: Ibig = I Iout = cv.CreateImage((w, h), I.depth, I.channels) cv.Smooth(Ibig, Iout, cv.CV_GAUSSIAN, param1=xwin, param2=ywin) return Iout
def smoothNoise2(image): b = 2 temp = cv.CreateImage((image.width + 4, image.height + 4), image.depth, image.nChannels) result = cv.CreateImage(cv.GetSize(image), image.depth, image.nChannels) # IPL_BORDER_CONSTANT = 0 # IPL_BORDER_REPLICATE = 1 cv.CopyMakeBorder(image, temp, (b, b), 0, 255) for x in xrange(b, image.width + b): for y in xrange(b, image.height + b): if temp[y + 1, x] == temp[y - 1, x] and \ temp[y + 2, x] == temp[y - 2, x] and \ temp[y + 1, x] == temp[y + 2, x] and \ temp[y, x] != temp[y + 1, x]: result[y - b, x - b] = temp[y + 1, x] elif temp[y, x + 1] == temp[y, x - 1] and \ temp[y, x + 2] == temp[y, x - 2] and \ temp[y, x + 1] == temp[y, x + 2] and \ temp[y, x] != temp[y, x + 1]: result[y - b, x - b] = temp[y, x + 1] else: result[y - b, x - b] = temp[y, x] return result
def opencvSaliency(self, scaledImageGray): cvImageGray = cv.CreateMat(scaledImageGray.height, scaledImageGray.width, cv.CV_32FC1) cv.Convert(scaledImageGray, cvImageGray) src = cvImageGray dftWidth = cv.GetOptimalDFTSize(src.width - 1) dftHeight = cv.GetOptimalDFTSize(src.height - 1) real = cv.CreateMat(dftHeight, dftWidth, cv.CV_32FC1) imaginary = cv.CreateMat(dftHeight, dftWidth, cv.CV_32FC1) dft = cv.CreateMat(dftHeight, dftWidth, cv.CV_32FC2) tmp = cv.GetSubRect(real, (0, 0, src.width, src.height)) cv.Copy(src, tmp) cv.Zero(imaginary) cv.Merge(real, imaginary, None, None, dft) # do the fft cv.DFT(dft, dft, cv.CV_DXT_FORWARD, src.height) cv.Split(dft, real, imaginary, None, None) cv.CartToPolar(real, imaginary, real, imaginary, 0) cv.Log(real, real) filtered = cv.CreateMat(dftHeight, dftWidth, cv.CV_32FC1) cv.Copy(real, filtered) cv.Smooth(filtered, filtered, cv.CV_BLUR) cv.Sub(real, filtered, real, None) cv.Exp(real, real) cv.PolarToCart(real, imaginary, real, imaginary, 0) #cv.PolarToCart( np.ones( shape=(dftHeight,dftWidth), dtype=np.float32 ), imaginary, real, imaginary,0 ) # do inverse fourier transform cv.Merge(real, imaginary, None, None, dft) cv.DFT(dft, dft, cv.CV_DXT_INV_SCALE, src.height) cv.Split(dft, real, imaginary, None, None) # get magnitude cv.CartToPolar(real, imaginary, real, None, 0) cv.Pow(real, real, 2.0) FILTER_RAD = 3 IPL_BORDER_CONSTANT = 0 sfiltered = cv.CreateMat(real.height + FILTER_RAD * 2, real.width + FILTER_RAD * 2, cv.CV_32FC1) cv.CopyMakeBorder(real, sfiltered, (FILTER_RAD, FILTER_RAD), IPL_BORDER_CONSTANT) cv.Smooth(sfiltered, sfiltered, cv.CV_GAUSSIAN, 2 * FILTER_RAD + 1) (min, max, minLoc, maxLoc) = cv.MinMaxLoc(sfiltered) cv.ConvertScale(sfiltered, sfiltered, 1 / (max - min), -min / (max - min)) # copy result to output image tmp = cv.GetSubRect(sfiltered, (FILTER_RAD, FILTER_RAD, src.width, src.height)) cv.Copy(tmp, cvImageGray) #cvReleaseMat(&sfiltered); #cvReleaseMat(&real); #cvReleaseMat(&filtered); #cvReleaseMat(&imaginary); #cvReleaseMat(&dft); saliencyMap = np.array(255.0 * np.array(cvImageGray), dtype=np.uint8) return saliencyMap
def isolate_numbers(image, name='unnamed.jpg'): h, w = image.shape #MAX_WIDTH = w * 0.33 raw_im_cv = array2cv(image) # Strip the black line on the bottom try: transposed = zip(*cv2array(raw_im_cv)) left_col = list(enumerate(reversed(transposed[2]))) right_col = list(enumerate(reversed(transposed[-2]))) findit = lambda a: a[0][0] if a[0][1][0] == 255 else findit(a[ 1:]) # It just works! h -= max(findit(left_col), findit(right_col)) cv.SetImageROI(raw_im_cv, (0, 0, w, h)) raw_im_cv = cv.CloneImage(raw_im_cv) except IndexError: pass im_cv = cv.CreateImage((w + 6, h + 6), 8, 1) im_cv_c = cv.CreateImage((w + 6, h + 6), 8, 3) cv.Rectangle(im_cv, (0, 0), cv.GetSize(im_cv), cv.CV_RGB(255, 255, 255), cv.CV_FILLED) cv.CopyMakeBorder(raw_im_cv, im_cv, (3, 3), 0, (255, 255, 255)) cv.CvtColor(im_cv, im_cv_c, cv.CV_GRAY2RGB) storage = cv.CreateMemStorage() contours = cv.FindContours(im_cv, storage, mode=cv.CV_RETR_LIST, method=cv.CV_CHAIN_APPROX_NONE, offset=(0, 0)) if not contours: return None else: cv.DrawContours(im_cv, contours, (0, 0, 0), (0, 0, 0), 7, -1) rects = [] i = -1 while contours: i += 1 storage2 = cv.CreateMemStorage(0) hull = cv.ConvexHull2(contours, storage2, cv.CV_CLOCKWISE, 1) if hull: #cv.PolyLine(im_cv_c, [hull], 1, cv.RGB(0, 255, 0), 4, cv.CV_AA) xmax, xmin, ymax, ymin = 0, w, 0, h for x, y in list(hull): xmax = max(xmax, x) ymax = max(ymax, y) xmin = min(xmin, x) ymin = min(ymin, y) height = (ymax - ymin) width = (xmax - xmin) if xmin < 5 or width < 50 or height < 50: contours = contours.h_next() continue #cv.Rectangle(im_cv_c, (xmin, ymin), (xmax, ymax), cv.RGB(0, 255, 255), 4) rects.append((xmin, ymin, xmax, ymax)) contours = contours.h_next() rects = sorted(rects) pointer = 0 while pointer < len(rects) - 1: me = rects[pointer] you = rects[pointer + 1] right = me[2] left = you[0] if right > left: me = (me[0], min(me[1], you[1]), max(me[2], you[2]), max(me[3], you[3])) rects = rects[:pointer] + [me] + rects[pointer + 2:] else: pointer += 1 numbers = [] #i = 0 for xmin, ymin, xmax, ymax in rects: #cv.Rectangle(im_cv_c, (xmin, ymin), (xmax, ymax), cv.RGB(128, 0, 128), 4) cv.SetImageROI(im_cv_c, (xmin, ymin, xmax - xmin, ymax - ymin)) dst = cv.CreateImage((40, 40), 8, 3) cv.Resize(im_cv_c, dst) #cv.SaveImage('numbers/' + name.replace('.jpg', '.%d.jpg' % (i,)), dst) _, tmp = tempfile.mkstemp('.png') cv.SaveImage(tmp, dst) dst = cv2.imread(tmp, 0) os.unlink(tmp) _, bw = cv2.threshold(dst, 80, 255, cv.CV_THRESH_BINARY + cv.CV_THRESH_OTSU) numbers.append(bw) #i += 1 return numbers
def doCopyMakeBorder(image, border, fillval): result = cv.CreateImage( (image.width + border * 2, image.height + border * 2), image.depth, image.nChannels) cv.CopyMakeBorder(image, result, (border, border), 0, fillval) return result