00001 import roslib
00002 roslib.load_manifest('opencv2')
00003 import cv
00004
00005 a = cv.LoadImage('/home/mkillpack/hrl_file_server/playpen_data_sets/2011-06-30_19-01-02/object000_try011_before_pr2.png', 0)
00006 b = cv.LoadImage('/home/mkillpack/hrl_file_server/playpen_data_sets/2011-06-30_19-01-02/object000_try011_after_pr2.png', 0)
00007 foreground = cv.CreateImage((640,480), 8, 1)
00008 size = cv.GetSize(a)
00009 IavgF = cv.CreateImage(size, cv.IPL_DEPTH_32F, 3)
00010 IdiffF = cv.CreateImage(size, cv.IPL_DEPTH_32F, 3)
00011 IprevF = cv.CreateImage(size, cv.IPL_DEPTH_32F, 3)
00012 IhiF = cv.CreateImage(size, cv.IPL_DEPTH_32F, 3)
00013 IlowF = cv.CreateImage(size, cv.IPL_DEPTH_32F, 3)
00014 Ilow1 = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1)
00015 Ilow2 = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1)
00016 Ilow3 = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1)
00017 Ihi1 = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1)
00018 Ihi2 = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1)
00019 Ihi3 = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1)
00020 cv.Zero(IavgF)
00021 cv.Zero(IdiffF)
00022 cv.Zero(IprevF)
00023 cv.Zero(IhiF)
00024 cv.Zero(IlowF)
00025
00026 Icount = 0.00001
00027
00028 Iscratch = cv.CreateImage(size, cv.IPL_DEPTH_32F, 3)
00029 Iscratch2 = cv.CreateImage(size, cv.IPL_DEPTH_32F, 3)
00030 Igray1 = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1)
00031 Igray2 = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1)
00032 Igray3 = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1)
00033 Imaskt = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1)
00034 cv.Zero(Iscratch)
00035 cv.Zero(Iscratch2)
00036 first = 1
00037
00038
00039 def accumulateBackground(img):
00040 global first, Icount
00041 cv.CvtScale(img, Iscratch, 1, 0)
00042 if (not first):
00043 cv.Acc(Iscratch, IavgF)
00044 cv.AbsDiff(Iscratch, IprevF, Iscratch2)
00045 cv.Acc(Iscratch2, IdiffF)
00046 Icount += 1.0
00047 first = 0
00048 cv.Copy(Iscratch, IprevF)
00049
00050 def setHighThresh(thresh):
00051 cv.ConvertScale(IdiffF, Iscratch, thresh)
00052 cv.Add(Iscratch, IavgF, IhiF)
00053 cv.Split(IhiF, Ihi1, Ihi2, Ihi3, None)
00054
00055 def setLowThresh(thresh):
00056 cv.ConvertScale(IdiffF, Iscratch, thresh)
00057 cv.Sub(IavgF, Iscratch, IlowF)
00058 cv.Split(IlowF, Ilow1, Ilow2, Ilow3, None)
00059
00060
00061 def createModelsfromStats():
00062 cv.ConvertScale(IavgF, IavgF, float(1.0/Icount))
00063 cv.ConvertScale(IdiffF, IdiffF, float(1.0/Icount))
00064
00065 cv.AddS(IdiffF, cv.Scalar(1.0, 1.0, 1.0), IdiffF)
00066 setHighThresh(10.0)
00067 setLowThresh(10.0)
00068
00069 def backgroundDiff(img, Imask):
00070 cv.CvtScale(img, Iscratch, 1, 0)
00071 cv.Split(Iscratch, Igray1, Igray2, Igray3, None)
00072 cv.InRange(Igray1, Ilow1, Ihi1, Imask)
00073
00074 cv.InRange(Igray2, Ilow2, Ihi2, Imaskt)
00075 cv.Or(Imask, Imaskt, Imask)
00076
00077 cv.InRange(Igray3, Ilow3, Ihi3, Imaskt)
00078 cv.Or(Imask, Imaskt, Imask)
00079
00080 cv.SubRS(Imask, 255, Imask)
00081 cv.SaveImage('/home/mkillpack/Desktop/mask.png', Imask)
00082
00083 print "here is the sum of the non-zero pixels", cv.Sum(Imask)
00084 return Imask
00085
00086
00087 if __name__ == '__main__':
00088 folder = '/home/mkillpack/hrl_file_server/playpen_data_sets/2011-06-30_19-01-02/'
00089
00090 for j in [3]:
00091 for i in xrange(200):
00092 try:
00093 file_name = folder+'object'+str(j).zfill(3)+'_try'+str(i).zfill(3)+'_after_pr2.png'
00094 img = cv.LoadImage(file_name, 1)
00095 print "reading ", file_name, '...'
00096 except:
00097 print file_name, " doesn't exist"
00098 if not img == None:
00099 accumulateBackground(img)
00100
00101
00102 createModelsfromStats()
00103 file_name = folder+'object006_try'+str(0).zfill(3)+'_before_pr2.png'
00104 img = cv.LoadImage(file_name, 3)
00105 Imask = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1)
00106 cv.Zero(Imask)
00107 cv.Zero(Imaskt)
00108 Imask = backgroundDiff(img, Imask)