content: # /usr/bin/python
  # To run this test you need python nose tools installed
  # Run test just use:
  #   nosetest test_display.py
  #
  
  import os, sys, pickle
  from SimpleCV import *
  from nose.tools import with_setup, nottest
  
  VISUAL_TEST = True  # if TRUE we save the images - otherwise we DIFF against them - the default is False
  SHOW_WARNING_TESTS = False  # show that warnings are working - tests will pass but warnings are generated.
  
  #colors
  black = Color.BLACK
  white = Color.WHITE
  red = Color.RED
  green = Color.GREEN
  blue = Color.BLUE
  
  ###############
  # TODO -
  # Examples of how to do profiling
  # Examples of how to do a single test -
  # UPDATE THE VISUAL TESTS WITH EXAMPLES.
  # Fix exif data
  # Turn off test warnings using decorators.
  # Write a use the tests doc.
  
  #images
  barcode = "../sampleimages/barcode.png"
  testimage = "../sampleimages/9dots4lines.png"
  testimage2 = "../sampleimages/aerospace.jpg"
  whiteimage = "../sampleimages/white.png"
  blackimage = "../sampleimages/black.png"
  testimageclr = "../sampleimages/statue_liberty.jpg"
  testbarcode = "../sampleimages/barcode.png"
  testoutput = "../sampleimages/9d4l.jpg"
  tmpimg = "../sampleimages/tmpimg.jpg"
  greyscaleimage = "../sampleimages/greyscale.jpg"
  logo = "../sampleimages/simplecv.png"
  logo_inverted = "../sampleimages/simplecv_inverted.png"
  ocrimage = "../sampleimages/ocr-test.png"
  circles = "../sampleimages/circles.png"
  webp = "../sampleimages/simplecv.webp"
  
  #alpha masking images
  topImg = "../sampleimages/RatTop.png"
  bottomImg = "../sampleimages/RatBottom.png"
  maskImg = "../sampleimages/RatMask.png"
  alphaMaskImg = "../sampleimages/RatAlphaMask.png"
  alphaSrcImg = "../sampleimages/GreenMaskSource.png"
  
  #standards path
  standard_path = "./standard/"
  
  #track images
  trackimgs = ["../sampleimages/tracktest0.jpg",
          "../sampleimages/tracktest1.jpg",
          "../sampleimages/tracktest2.jpg",
          "../sampleimages/tracktest3.jpg",
          "../sampleimages/tracktest4.jpg",
          "../sampleimages/tracktest5.jpg",
          "../sampleimages/tracktest6.jpg",
          "../sampleimages/tracktest7.jpg",
          "../sampleimages/tracktest8.jpg",
          "../sampleimages/tracktest9.jpg",]
  
  #Given a set of images, a path, and a tolerance do the image diff.
  def imgDiffs(test_imgs,name_stem,tolerance,path):
      count = len(test_imgs)
      for idx in range(0,count):
          lhs = test_imgs[idx].applyLayers() # this catches drawing methods
          fname = standard_path+name_stem+str(idx)+".jpg"
          rhs = Image(fname)
          if( lhs.width == rhs.width and lhs.height == rhs.height ):
              diff = (lhs-rhs)
              val = np.average(diff.getNumpy())
              if( val > tolerance ):
                  print val
                  return True
      return False
  
  #Save a list of images to a standard path.
  def imgSaves(test_imgs, name_stem, path=standard_path):
      count = len(test_imgs)
      for idx in range(0,count):
          fname = standard_path+name_stem+str(idx)+".jpg"
          test_imgs[idx].save(fname)#,quality=95)
  
  #perform the actual image save and image diffs.
  def perform_diff(result,name_stem,tolerance=2.0,path=standard_path):
      if(VISUAL_TEST): # save the correct images for a visual test
          imgSaves(result,name_stem,path)
      else: # otherwise we test our output against the visual test
          if( imgDiffs(result,name_stem,tolerance,path) ):
              assert False
          else:
              pass
  
  #These function names are required by nose test, please leave them as is
  def setup_context():
      img = Image(testimage)
  
  def destroy_context():
      img = ""
  
  @with_setup(setup_context, destroy_context)
  def test_image_stretch():
      img = Image(greyscaleimage)
      stretched = img.stretch(100,200)
      if(stretched == None):
          assert False
  
      result = [stretched]
      name_stem = "test_stretch"
      perform_diff(result,name_stem)
  
  def test_image_bitmap():
      img1 = Image("lenna")
      img2 = Image("lenna")
      img2 = img2.smooth()
      result = [img1,img2]
      name_stem = "test_image_bitmap"
      perform_diff(result,name_stem)
  
  # # Image Class Test
  
  def test_image_scale():
      img = Image(testimage)
      thumb = img.scale(30,30)
      if(thumb == None):
          assert False
      result = [thumb]
      name_stem = "test_image_scale"
      perform_diff(result,name_stem)
  
  def test_image_copy():
      img = Image(testimage2)
      copy = img.copy()
  
      if (img[1,1] != copy[1,1] or img.size() != copy.size()):
          assert False
  
      result = [copy]
      name_stem = "test_image_copy"
      perform_diff(result,name_stem)
      pass
  
  def test_image_setitem():
      img = Image(testimage)
      img[1,1] = (0, 0, 0)
      newimg = Image(img.getBitmap())
      colors = newimg[1,1]
      if (colors[0] == 0 and colors[1] == 0 and colors[2] == 0):
          pass
      else:
          assert False
  
      result = [newimg]
      name_stem = "test_image_setitem"
      perform_diff(result,name_stem)
  
  
  def test_image_setslice():
      img = Image(testimage)
      img[1:10,1:10] = (0,0,0) #make a black box
      newimg = Image(img.getBitmap())
      section = newimg[1:10,1:10]
      for i in range(5):
          colors = section[i,0]
          if (colors[0] != 0 or colors[1] != 0 or colors[2] != 0):
              assert False
      pass
      result = [newimg]
      name_stem = "test_image_setslice"
      perform_diff(result,name_stem)
  
  
  def test_detection_findCorners():
      img = Image(testimage2)
      corners = img.findCorners(25)
      corners.draw()
      if (len(corners) == 0):
          assert False
      result = [img]
      name_stem = "test_detection_findCorners"
      perform_diff(result,name_stem)
  
  def test_image_smooth():
      img = Image(testimage2)
      result = []
      result.append(img.smooth())
      result.append(img.smooth('bilateral', (3,3), 4, 1))
      result.append(img.smooth('blur', (3, 3)))
      result.append(img.smooth('median', (3, 3)))
      result.append(img.smooth('gaussian', (5,5), 0))
      result.append(img.smooth('bilateral', (3,3), 4, 1,grayscale=False))
      result.append(img.smooth('blur', (3, 3),grayscale=True))
      result.append(img.smooth('median', (3, 3),grayscale=True))
      result.append(img.smooth('gaussian', (5,5), 0,grayscale=True))
      name_stem = "test_image_smooth"
      perform_diff(result,name_stem)
      pass
  
  def test_image_binarize():
      img =  Image(testimage2)
      binary = img.binarize()
      binary2 = img.binarize((60, 100, 200))
      hist = binary.histogram(20)
      hist2 = binary2.histogram(20)
  
      result = [binary,binary2]
      name_stem = "test_image_binarize"
      perform_diff(result,name_stem)
  
      if (hist[0] + hist[-1] == np.sum(hist) and hist2[0] + hist2[-1] == np.sum(hist2)):
          pass
      else:
          assert False
  
  def test_image_binarize_adaptive():
      img =  Image(testimage2)
      binary = img.binarize(-1)
      hist = binary.histogram(20)
  
      result = [binary]
      name_stem = "test_image_binarize_adaptive"
      perform_diff(result,name_stem)
  
      if (hist[0] + hist[-1] == np.sum(hist)):
          pass
      else:
          assert False
  
  def test_image_invert():
      img = Image(testimage2)
      clr = img[1,1]
      img = img.invert()
  
      result = [img]
      name_stem = "test_image_invert"
      perform_diff(result,name_stem)
  
      if (clr[0] == (255 - img[1,1][0])):
          pass
      else:
          assert False
  
  def test_image_drawing():
      img = Image(testimageclr)
      img.drawCircle((img.width/2, img.height/2), 10,thickness=3)
      img.drawCircle((img.width/2, img.height/2), 15,thickness=5,color=Color.RED)
      img.drawCircle((img.width/2, img.height/2), 20)
      img.drawLine((5, 5), (5, 8))
      img.drawLine((5, 5), (10, 10),thickness=3)
      img.drawLine((0, 0), (img.width, img.height),thickness=3,color=Color.BLUE)
      img.drawRectangle(20,20,10,5)
      img.drawRectangle(22,22,10,5,alpha=128)
      img.drawRectangle(24,24,10,15,width=-1,alpha=128)
      img.drawRectangle(28,28,10,15,width=3,alpha=128)
      result = [img]
      name_stem = "test_image_drawing"
      perform_diff(result,name_stem)
  
  
  def test_image_splitchannels():
      img = Image(testimageclr)
      (r, g, b) = img.splitChannels(True)
      (red, green, blue) = img.splitChannels()
      result = [r,g,b,red,green,blue]
      name_stem = "test_image_splitchannels"
      perform_diff(result,name_stem)
      pass
  
  def test_detection_lines():
      img = Image(testimage2)
      lines = img.findLines()
      lines.draw()
      result = [img]
      name_stem = "test_detection_lines"
      perform_diff(result,name_stem)
  
      if(lines == 0 or lines == None):
          assert False
  
  def test_detection_blobs_appx():
      img = Image("lenna")
      blobs = img.findBlobs()
      blobs[-1].draw(color=Color.RED)
      blobs[-1].drawAppx(color=Color.BLUE)
      result = [img]
  
      img2 = Image("lenna")
      blobs = img2.findBlobs(appx_level=11)
      blobs[-1].draw(color=Color.RED)
      blobs[-1].drawAppx(color=Color.BLUE)
      result.append(img2)
  
      name_stem = "test_detection_blobs_appx"
      perform_diff(result,name_stem,5.00)
      if blobs == None:
          assert False
  
  def test_detection_blobs():
      img = Image(testbarcode)
      blobs = img.findBlobs()
      blobs.draw(color=Color.RED)
      result = [img]
      #TODO - WE NEED BETTER COVERAGE HERE
      name_stem = "test_detection_blobs"
      perform_diff(result,name_stem,5.00)
  
      if blobs == None:
          assert False
  
  def test_detection_blobs_lazy():
  
      img = Image("lenna")
      b = img.findBlobs()
      result = []
  
      s = pickle.dumps(b[-1]) # use two otherwise it w
      b2 =  pickle.loads(s)
  
      result.append(b[-1].mImg)
      result.append(b[-1].mMask)
      result.append(b[-1].mHullImg)
      result.append(b[-1].mHullMask)
  
      result.append(b2.mImg)
      result.append(b2.mMask)
      result.append(b2.mHullImg)
      result.append(b2.mHullMask)
  
      #TODO - WE NEED BETTER COVERAGE HERE
      name_stem = "test_detection_blobs_lazy"
      perform_diff(result,name_stem,6.00)
  
  
  def test_detection_blobs_adaptive():
      img = Image(testimage)
      blobs = img.findBlobs(-1, threshblocksize=99)
      blobs.draw(color=Color.RED)
      result = [img]
      name_stem = "test_detection_blobs_adaptive"
      perform_diff(result,name_stem,5.00)
      if blobs == None:
          assert False
  
  
  def test_color_curve_HSL():
      y = np.array([[0,0],[64,128],[192,128],[255,255]])  #These are the weights
      curve = ColorCurve(y)
      img = Image(testimage)
      img2 = img.applyHLSCurve(curve,curve,curve)
      img3 = img-img2
  
      result = [img2,img3]
      name_stem = "test_color_curve_HLS"
      perform_diff(result,name_stem)
  
      c = img3.meanColor()
      if( c[0] > 2.0 or c[1] > 2.0 or c[2] > 2.0 ): #there may be a bit of roundoff error
          assert False
  
  def test_color_curve_RGB():
      y = np.array([[0,0],[64,128],[192,128],[255,255]])  #These are the weights
      curve = ColorCurve(y)
      img = Image(testimage)
      img2 = img.applyRGBCurve(curve,curve,curve)
      img3 = img-img2
  
      result = [img2,img3]
      name_stem = "test_color_curve_RGB"
      perform_diff(result,name_stem)
  
      c = img3.meanColor()
      if( c[0] > 1.0 or c[1] > 1.0 or c[2] > 1.0 ): #there may be a bit of roundoff error
          assert False
  
  def test_color_curve_GRAY():
      y = np.array([[0,0],[64,128],[192,128],[255,255]])  #These are the weights
      curve = ColorCurve(y)
      img = Image(testimage)
      gray = img.grayscale()
      img2 = img.applyIntensityCurve(curve)
  
      result = [img2]
      name_stem = "test_color_curve_GRAY"
      perform_diff(result,name_stem)
  
      g=gray.meanColor()
      i2=img2.meanColor()
      if( g[0]-i2[0] > 1 ): #there may be a bit of roundoff error
          assert False
  
  def test_image_dilate():
      img=Image(barcode)
      img2 = img.dilate(20)
  
      result = [img2]
      name_stem = "test_image_dilate"
      perform_diff(result,name_stem)
      c=img2.meanColor()
  
      if( c[0] < 254 or c[1] < 254 or c[2] < 254 ):
          assert False;
  
  def test_image_erode():
      img=Image(barcode)
      img2 = img.erode(100)
  
      result = [img2]
      name_stem = "test_image_erode"
      perform_diff(result,name_stem)
  
      c=img2.meanColor()
      print(c)
      if( c[0] > 0 or c[1] > 0 or c[2] > 0 ):
          assert False;
  
  def test_image_morph_open():
      img = Image(barcode);
      erode= img.erode()
      dilate = erode.dilate()
      result = img.morphOpen()
      test = result-dilate
      c=test.meanColor()
      results = [result]
      name_stem = "test_image_morph_open"
      perform_diff(results,name_stem)
  
      if( c[0] > 1 or c[1] > 1 or c[2] > 1 ):
          assert False;
  
  def test_image_morph_close():
      img = Image(barcode)
      dilate = img.dilate()
      erode = dilate.erode()
      result = img.morphClose()
      test = result-erode
      c=test.meanColor()
  
      results = [result]
      name_stem = "test_image_morph_close"
      perform_diff(results,name_stem)
  
  
      if( c[0] > 1 or c[1] > 1 or c[2] > 1 ):
          assert False;
  
  def test_image_morph_grad():
      img = Image(barcode)
      dilate = img.dilate()
      erode = img.erode()
      dif = dilate-erode
      result = img.morphGradient()
      test = result-dif
      c=test.meanColor()
  
      results = [result]
      name_stem = "test_image_morph_grad"
      perform_diff(results,name_stem)
  
  
      if( c[0] > 1 or c[1] > 1 or c[2] > 1 ):
          assert False
  
  def test_image_rotate_fixed():
      img = Image(testimage2)
      img2=img.rotate(180, scale = 1)
      img3=img.flipVertical()
      img4=img3.flipHorizontal()
      img5 = img.rotate(70)
      img6 = img.rotate(70,scale=0.5)
  
      results = [img2,img3,img4,img5,img6]
      name_stem = "test_image_rotate_fixed"
      perform_diff(results,name_stem)
  
      test = img4-img2
      c=test.meanColor()
      print(c)
      if( c[0] > 5 or c[1] > 5 or c[2] > 5 ):
          assert False
  
  
  def test_image_rotate_full():
      img = Image(testimage2)
      img2=img.rotate(180,"full",scale = 1)
  
      results = [img2]
      name_stem = "test_image_rotate_full"
      perform_diff(results,name_stem)
  
      c1=img.meanColor()
      c2=img2.meanColor()
      if( abs(c1[0]-c2[0]) > 5 or abs(c1[1]-c2[1]) > 5 or abs(c1[2]-c2[2]) > 5 ):
          assert False
  
  def test_image_shear_warp():
      img = Image(testimage2)
      dst =  ((img.width/2,0),(img.width-1,img.height/2),(img.width/2,img.height-1))
      s = img.shear(dst)
  
  
      color = s[0,0]
      if (color != (0,0,0)):
          assert False
  
      dst = ((img.width*0.05,img.height*0.03),(img.width*0.9,img.height*0.1),(img.width*0.8,img.height*0.7),(img.width*0.2,img.height*0.9))
      w = img.warp(dst)
  
      results = [s,w]
      name_stem = "test_image_shear_warp"
      perform_diff(results,name_stem)
  
      color = s[0,0]
      if (color != (0,0,0)):
          assert False
  
      pass
  
  def test_image_affine():
      img = Image(testimage2)
      src =  ((0,0),(img.width-1,0),(img.width-1,img.height-1))
      dst =  ((img.width/2,0),(img.width-1,img.height/2),(img.width/2,img.height-1))
      aWarp = cv.CreateMat(2,3,cv.CV_32FC1)
      cv.GetAffineTransform(src,dst,aWarp)
      atrans = img.transformAffine(aWarp)
  
      aWarp2 = np.array(aWarp)
      atrans2 = img.transformAffine(aWarp2)
  
      test = atrans-atrans2
      c=test.meanColor()
  
      results = [atrans,atrans2]
  
      name_stem = "test_image_affine"
      perform_diff(results,name_stem)
  
      if( c[0] > 1 or c[1] > 1 or c[2] > 1 ):
          assert False
  
  def test_image_perspective():
      img = Image(testimage2)
      src = ((0,0),(img.width-1,0),(img.width-1,img.height-1),(0,img.height-1))
      dst = ((img.width*0.05,img.height*0.03),(img.width*0.9,img.height*0.1),(img.width*0.8,img.height*0.7),(img.width*0.2,img.height*0.9))
      pWarp = cv.CreateMat(3,3,cv.CV_32FC1)
      cv.GetPerspectiveTransform(src,dst,pWarp)
      ptrans = img.transformPerspective(pWarp)
  
      pWarp2 = np.array(pWarp)
      ptrans2 = img.transformPerspective(pWarp2)
  
  
      test = ptrans-ptrans2
      c=test.meanColor()
  
      results = [ptrans,ptrans2]
      name_stem = "test_image_perspective"
      perform_diff(results,name_stem)
      if( c[0] > 1 or c[1] > 1 or c[2] > 1 ):
          assert False
  
  def test_camera_undistort():
      
      fakeCamera = FrameSource()
      fakeCamera.loadCalibration("TestCalibration")
      img = Image("../sampleimages/CalibImage0.png")
      img2 = fakeCamera.undistort(img)
  
      results = [img2]
      name_stem = "test_camera_undistort"
      perform_diff(results,name_stem)
  
      if( not img2 ): #right now just wait for this to return
          assert False
  
  def test_image_crop():
      img = Image(logo)
      x = 5
      y = 6
      w = 10
      h = 20
      crop = img.crop(x,y,w,h)
      crop2 = img[x:(x+w),y:(y+h)]
      crop6 = img.crop(0,0,10,10)
      if( SHOW_WARNING_TESTS ):
          crop7 = img.crop(0,0,-10,10)
          crop8 = img.crop(-50,-50,10,10)
          crop3 = img.crop(-3,-3,10,20)
          crop4 = img.crop(-10,10,20,20,centered=True)
          crop5 = img.crop(-10,-10,20,20)
  
      results = [crop,crop2,crop6]
      name_stem = "test_image_crop"
      perform_diff(results,name_stem)
  
      diff = crop-crop2;
      c=diff.meanColor()
      if( c[0] > 0 or c[1] > 0 or c[2] > 0 ):
          assert False
  
  def test_image_region_select():
      img = Image(logo)
      x1 = 0
      y1 = 0
      x2 = img.width
      y2 = img.height
      crop = img.regionSelect(x1,y1,x2,y2)
  
      results = [crop]
      name_stem = "test_image_region_select"
      perform_diff(results,name_stem)
  
      diff = crop-img;
      c=diff.meanColor()
      if( c[0] > 0 or c[1] > 0 or c[2] > 0 ):
          assert False
  
  def test_image_subtract():
      imgA = Image(logo)
      imgB = Image(logo_inverted)
      imgC = imgA - imgB
      results = [imgC]
      name_stem = "test_image_subtract"
      perform_diff(results,name_stem)
  
  def test_image_negative():
      imgA = Image(logo)
      imgB = -imgA
      results = [imgB]
      name_stem = "test_image_negative"
      perform_diff(results,name_stem)
  
  
  def test_image_divide():
      imgA = Image(logo)
      imgB = Image(logo_inverted)
  
      imgC = imgA / imgB
  
      results = [imgC]
      name_stem = "test_image_divide"
      perform_diff(results,name_stem)
  
  def test_image_and():
      imgA = Image(barcode)
      imgB = imgA.invert()
  
  
      imgC = imgA & imgB # should be all black
  
      results = [imgC]
      name_stem = "test_image_and"
      perform_diff(results,name_stem)
  
  
  def test_image_or():
      imgA = Image(barcode)
      imgB = imgA.invert()
  
      imgC = imgA | imgB #should be all white
  
      results = [imgC]
      name_stem = "test_image_or"
      perform_diff(results,name_stem)
  
  
  
  def test_color_colormap_build():
      cm = ColorModel()
      #cm.add(Image(logo))
      cm.add((127,127,127))
      if(cm.contains((127,127,127))):
          cm.remove((127,127,127))
      else:
          assert False
  
      cm.remove((0,0,0))
      cm.remove((255,255,255))
      cm.add((0,0,0))
      cm.add([(0,0,0),(255,255,255)])
      cm.add([(255,0,0),(0,255,0)])
      img = cm.threshold(Image(testimage))
      c=img.meanColor()
  
      #if( c[0] > 1 or c[1] > 1 or c[2] > 1 ):
      #  assert False
  
      cm.save("temp.txt")
      cm2 = ColorModel()
      cm2.load("temp.txt")
      img = Image("logo")
      img2 = cm2.threshold(img)
      cm2.add((0,0,255))
      img3 = cm2.threshold(img)
      cm2.add((255,255,0))
      cm2.add((0,255,255))
      cm2.add((255,0,255))
      img4 = cm2.threshold(img)
      cm2.add(img)
      img5 = cm2.threshold(img)
  
      results = [img,img2,img3,img4,img5]
      name_stem = "test_color_colormap_build"
      perform_diff(results,name_stem)
  
      #c=img.meanColor()
      #if( c[0] > 1 or c[1] > 1 or c[2] > 1 ):
      #  assert False
  
  def test_color_conversion_func_BGR():
      #we'll just go through the space to make sure nothing blows up
      img = Image(testimage)
      results = []
      results.append(img.toBGR())
      results.append(img.toRGB())
      results.append(img.toHLS())
      results.append(img.toHSV())
      results.append(img.toXYZ())
  
      bgr = img.toBGR()
  
      results.append(bgr.toBGR())
      results.append(bgr.toRGB())
      results.append(bgr.toHLS())
      results.append(bgr.toHSV())
      results.append(bgr.toXYZ())
  
      name_stem = "test_color_conversion_func_BGR"
      perform_diff(results,name_stem,tolerance=4.0)
  
  
  def test_color_conversion_func_HSV():
      img = Image(testimage)
      hsv = img.toHSV()
      results = [hsv]
      results.append(hsv.toBGR())
      results.append(hsv.toRGB())
      results.append(hsv.toHLS())
      results.append(hsv.toHSV())
      results.append(hsv.toXYZ())
      name_stem = "test_color_conversion_func_HSV"
      perform_diff(results,name_stem,tolerance=4.0 )
  
  
  def test_color_conversion_func_HLS():
      img = Image(testimage)
  
      hls = img.toHLS()
      results = [hls]
  
      results.append(hls.toBGR())
      results.append(hls.toRGB())
      results.append(hls.toHLS())
      results.append(hls.toHSV())
      results.append(hls.toXYZ())
  
      name_stem = "test_color_conversion_func_HLS"
      perform_diff(results,name_stem,tolerance=4.0)
  
  
  def test_color_conversion_func_XYZ():
      img = Image(testimage)
  
      xyz = img.toXYZ()
      results = [xyz]
      results.append(xyz.toBGR())
      results.append(xyz.toRGB())
      results.append(xyz.toHLS())
      results.append(xyz.toHSV())
      results.append(xyz.toXYZ())
  
      name_stem = "test_color_conversion_func_XYZ"
      perform_diff(results,name_stem,tolerance=8.0)
  
  
  def test_blob_maker():
      img = Image("../sampleimages/blockhead.png")
      blobber = BlobMaker()
      results = blobber.extract(img)
      print(len(results))
      if( len(results) != 7 ):
          assert False
  
  def test_blob_holes():
      img = Image("../sampleimages/blockhead.png")
      blobber = BlobMaker()
      blobs = blobber.extract(img)
      count = 0
      blobs.draw()
      results = [img]
      name_stem = "test_blob_holes"
      perform_diff(results,name_stem,tolerance=3.0)
  
  
      for b in blobs:
          if( b.mHoleContour is not None ):
              count = count + len(b.mHoleContour)
      if( count != 7 ):
          assert False
  
  def test_blob_hull():
      img = Image("../sampleimages/blockhead.png")
      blobber = BlobMaker()
      blobs = blobber.extract(img)
      blobs.draw()
  
      results = [img]
      name_stem = "test_blob_holes"
      perform_diff(results,name_stem,tolerance=3.0)
  
      for b in blobs:
          if( len(b.mConvexHull) < 3 ):
              assert False
  
  def test_blob_render():
      img = Image("../sampleimages/blockhead.png")
      blobber = BlobMaker()
      blobs = blobber.extract(img)
      dl = DrawingLayer((img.width,img.height))
      reimg = DrawingLayer((img.width,img.height))
      for b in blobs:
          b.draw(color=Color.RED, alpha=128)
          b.drawHoles(width=2,color=Color.BLUE)
          b.drawHull(color=Color.ORANGE,width=2)
          b.draw(color=Color.RED, alpha=128,layer=dl)
          b.drawHoles(width=2,color=Color.BLUE,layer=dl)
          b.drawHull(color=Color.ORANGE,width=2,layer=dl)
          b.drawMaskToLayer(reimg)
  
      img.addDrawingLayer(dl)
      results = [img]
      name_stem = "test_blob_render"
      perform_diff(results,name_stem,tolerance=5.0)
  
      pass
  
  def test_image_convolve():
      img = Image(testimageclr)
      kernel = np.array([[0,0,0],[0,1,0],[0,0,0]])
      img2 = img.convolve(kernel,center=(2,2))
  
      results = [img2]
      name_stem = "test_image_convolve"
      perform_diff(results,name_stem)
  
      c=img.meanColor()
      d=img2.meanColor()
      e0 = abs(c[0]-d[0])
      e1 = abs(c[1]-d[1])
      e2 = abs(c[2]-d[2])
      if( e0 > 1 or e1 > 1 or e2 > 1 ):
          assert False
  
  def test_template_match():
      source = Image("../sampleimages/templatetest.png")
      template = Image("../sampleimages/template.png")
      t = 2
      fs = source.findTemplate(template,threshold=t)
      fs.draw()
      results = [source]
      name_stem = "test_template_match"
      perform_diff(results,name_stem)
  
      pass
  
  def test_embiggen():
      img = Image(logo)
  
      results = []
      w = int(img.width*1.2)
      h = int(img.height*1.2)
  
      results.append(img.embiggen(size=(w,h),color=Color.RED))
      results.append(img.embiggen(size=(w,h),color=Color.RED,pos=(30,30)))
  
      results.append(img.embiggen(size=(w,h),color=Color.RED,pos=(-20,-20)))
      results.append(img.embiggen(size=(w,h),color=Color.RED,pos=(30,-20)))
      results.append(img.embiggen(size=(w,h),color=Color.RED,pos=(60,-20)))
      results.append(img.embiggen(size=(w,h),color=Color.RED,pos=(60,30)))
  
      results.append(img.embiggen(size=(w,h),color=Color.RED,pos=(80,80)))
      results.append(img.embiggen(size=(w,h),color=Color.RED,pos=(30,80)))
      results.append(img.embiggen(size=(w,h),color=Color.RED,pos=(-20,80)))
      results.append(img.embiggen(size=(w,h),color=Color.RED,pos=(-20,30)))
  
      name_stem = "test_embiggen"
      perform_diff(results,name_stem)
  
      pass
  
  def test_createBinaryMask():
      img2 = Image(logo)
      results = []
      results.append(img2.createBinaryMask(color1=(0,100,100),color2=(255,200,200)))
      results.append(img2.createBinaryMask(color1=(0,0,0),color2=(128,128,128)))
      results.append(img2.createBinaryMask(color1=(0,0,128),color2=(255,255,255)))
  
      name_stem = "test_createBinaryMask"
      perform_diff(results,name_stem)
  
      pass
  
  def test_applyBinaryMask():
      img = Image(logo)
      mask = img.createBinaryMask(color1=(0,128,128),color2=(255,255,255))
      results = []
      results.append(img.applyBinaryMask(mask))
      results.append(img.applyBinaryMask(mask,bg_color=Color.RED))
  
      name_stem = "test_applyBinaryMask"
      perform_diff(results,name_stem,tolerance=3.0)
  
      pass
  
  def test_applyPixelFunc():
      img = Image(logo)
      def myFunc((r,g,b)):
          return( (b,g,r) )
  
      img = img.applyPixelFunction(myFunc)
      name_stem = "test_applyPixelFunc"
      results = [img]
      perform_diff(results,name_stem)
      pass
  
  def test_applySideBySide():
      img = Image(logo)
      img3 = Image(testimage2)
  
      #LB = little image big image
      #BL = big image little image  -> this is important to test all the possible cases.
      results = []
  
      results.append(img3.sideBySide(img,side='right',scale=False))
      results.append(img3.sideBySide(img,side='left',scale=False))
      results.append(img3.sideBySide(img,side='top',scale=False))
      results.append(img3.sideBySide(img,side='bottom',scale=False))
  
      results.append(img.sideBySide(img3,side='right',scale=False))
      results.append(img.sideBySide(img3,side='left',scale=False))
      results.append(img.sideBySide(img3,side='top',scale=False))
      results.append(img.sideBySide(img3,side='bottom',scale=False))
  
      results.append(img3.sideBySide(img,side='right',scale=True))
      results.append(img3.sideBySide(img,side='left',scale=True))
      results.append(img3.sideBySide(img,side='top',scale=True))
      results.append(img3.sideBySide(img,side='bottom',scale=True))
  
      results.append(img.sideBySide(img3,side='right',scale=True))
      results.append(img.sideBySide(img3,side='left',scale=True))
      results.append(img.sideBySide(img3,side='top',scale=True))
      results.append(img.sideBySide(img3,side='bottom',scale=True))
  
      name_stem = "test_applySideBySide"
      perform_diff(results,name_stem)
  
      pass
  
  def test_resize():
      img = Image(logo)
      w = img.width
      h = img.height
      img2 = img.resize(w*2,None)
      if( img2.width != w*2 or img2.height != h*2):
          assert False
  
      img3 = img.resize(h=h*2)
  
      if( img3.width != w*2 or img3.height != h*2):
          assert False
  
      img4 = img.resize(h=h*2,w=w*2)
  
      if( img4.width != w*2 or img4.height != h*2):
          assert False
  
          pass
  
      results = [img2,img3,img4]
      name_stem = "test_resize"
      perform_diff(results,name_stem)
  
  
  def test_createAlphaMask():
      alphaMask = Image(alphaSrcImg)
      mask = alphaMask.createAlphaMask(hue=60)
      mask2 = alphaMask.createAlphaMask(hue_lb=59,hue_ub=61)
      top = Image(topImg)
      bottom = Image(bottomImg)
      bottom = bottom.blit(top,alphaMask=mask2)
      results = [mask,mask2,bottom]
      name_stem = "test_createAlphaMask"
      perform_diff(results,name_stem)
  
  
  def test_blit_regular():
      top = Image(topImg)
      bottom = Image(bottomImg)
      results = []
      results.append(bottom.blit(top))
      results.append(bottom.blit(top,pos=(-10,-10)))
      results.append(bottom.blit(top,pos=(-10,10)))
      results.append(bottom.blit(top,pos=(10,-10)))
      results.append(bottom.blit(top,pos=(10,10)))
  
      name_stem = "test_blit_regular"
      perform_diff(results,name_stem)
  
      pass
  
  def test_blit_mask():
      top = Image(topImg)
      bottom = Image(bottomImg)
      mask = Image(maskImg)
      results = []
      results.append(bottom.blit(top,mask=mask))
      results.append(bottom.blit(top,mask=mask,pos=(-50,-50)))
      results.append(bottom.blit(top,mask=mask,pos=(-50,50)))
      results.append(bottom.blit(top,mask=mask,pos=(50,-50)))
      results.append(bottom.blit(top,mask=mask,pos=(50,50)))
  
      name_stem = "test_blit_mask"
      perform_diff(results,name_stem)
  
      pass
  
  
  def test_blit_alpha():
      top = Image(topImg)
      bottom = Image(bottomImg)
      a = 0.5
      results = []
      results.append(bottom.blit(top,alpha=a))
      results.append(bottom.blit(top,alpha=a,pos=(-50,-50)))
      results.append(bottom.blit(top,alpha=a,pos=(-50,50)))
      results.append(bottom.blit(top,alpha=a,pos=(50,-50)))
      results.append(bottom.blit(top,alpha=a,pos=(50,50)))
      name_stem = "test_blit_alpha"
      perform_diff(results,name_stem)
  
      pass
  
  
  def test_blit_alpha_mask():
      top = Image(topImg)
      bottom = Image(bottomImg)
      aMask = Image(alphaMaskImg)
      results = []
  
      results.append(bottom.blit(top,alphaMask=aMask))
      results.append(bottom.blit(top,alphaMask=aMask,pos=(-10,-10)))
      results.append(bottom.blit(top,alphaMask=aMask,pos=(-10,10)))
      results.append(bottom.blit(top,alphaMask=aMask,pos=(10,-10)))
      results.append(bottom.blit(top,alphaMask=aMask,pos=(10,10)))
  
      name_stem = "test_blit_alpha_mask"
      perform_diff(results,name_stem)
  
      pass
  
  def test_whiteBalance():
      img = Image("../sampleimages/BadWB2.jpg")
      output = img.whiteBalance()
      output2 = img.whiteBalance(method="GrayWorld")
      results = [output,output2]
      name_stem = "test_whiteBalance"
      perform_diff(results,name_stem)
  
  def test_hough_circles():
      img = Image(circles)
      circs = img.findCircle(thresh=100)
      circs.draw()
      if( circs[0] < 1 ):
          assert False
      circs[0].coordinates()
      circs[0].width()
      circs[0].area()
      circs[0].perimeter()
      circs[0].height()
      circs[0].radius()
      circs[0].diameter()
      circs[0].colorDistance()
      circs[0].meanColor()
      circs[0].distanceFrom(point=(0,0))
      circs[0].draw()
      img2 = circs[0].crop()
      img3 = circs[0].crop(noMask=True)
  
      results = [img,img2,img3]
      name_stem = "test_hough_circle"
      perform_diff(results,name_stem)
  
  
      if( img2 is not None and img3 is not None ):
          pass
      else:
          assert False
  
  def test_drawRectangle():
      img = Image(testimage2)
      img.drawRectangle(0,0,100,100,color=Color.BLUE,width=0,alpha=0)
      img.drawRectangle(1,1,100,100,color=Color.BLUE,width=2,alpha=128)
      img.drawRectangle(1,1,100,100,color=Color.BLUE,width=1,alpha=128)
      img.drawRectangle(2,2,100,100,color=Color.BLUE,width=1,alpha=255)
      img.drawRectangle(3,3,100,100,color=Color.BLUE)
      img.drawRectangle(4,4,100,100,color=Color.BLUE,width=12)
      img.drawRectangle(5,5,100,100,color=Color.BLUE,width=-1)
  
      results = [img]
      name_stem = "test_drawRectangle"
      perform_diff(results,name_stem)
  
      pass
  
  
  def test_BlobMinRect():
      img = Image(testimageclr)
      blobs = img.findBlobs()
      for b in blobs:
          b.drawMinRect(color=Color.BLUE,width=3,alpha=123)
      results = [img]
      name_stem = "test_BlobMinRect"
      perform_diff(results,name_stem)
      pass
  
  def test_BlobRect():
      img = Image(testimageclr)
      blobs = img.findBlobs()
      for b in blobs:
          b.drawRect(color=Color.BLUE,width=3,alpha=123)
  
      results = [img]
      name_stem = "test_BlobRect"
      perform_diff(results,name_stem)
      pass
  
  def test_blob_isa_methods():
      img1 = Image(circles)
      img2 = Image("../sampleimages/blockhead.png")
      blobs = img1.findBlobs().sortArea()
      t1 = blobs[-1].isCircle()
      f1 = blobs[-1].isRectangle()
      blobs = img2.findBlobs().sortArea()
      f2 = blobs[-1].isCircle()
      t2 = blobs[-1].isRectangle()
      if( t1 and t2 and not f1 and not f2):
          pass
      else:
          assert False
  
  def test_findKeypoints():
      try:
          import cv2
      except:
          pass
          return
      img = Image(testimage2)
      kp = img.findKeypoints()
      for k in kp:
          k.getObject()
          k.descriptor()
          k.quality()
          k.octave()
          k.flavor()
          k.angle()
          k.coordinates()
          k.draw()
          k.distanceFrom()
          k.meanColor()
          k.area()
          k.perimeter()
          k.width()
          k.height()
          k.radius()
          k.crop()
  
      kp.draw()
      results = [img]
      name_stem = "test_findKeypoints"
      perform_diff(results,name_stem)
  
      pass
  
  def test_movement_feature():
      current1 = Image("../sampleimages/flow_simple1.png")
      prev = Image("../sampleimages/flow_simple2.png")
  
      fs = current1.findMotion(prev, window=7)
      if( len(fs) > 0 ):
          fs.draw(color=Color.RED)
          img = fs[0].crop()
          color = fs[1].meanColor()
  