示例#1
0
def analyzeCut(scaleImage, edgeImage, cut):
	"""Extract the interesting features respecting the cut"""

	# Set up constraints
	constraints = regionSelector.Constraints(cv.cvGetSize(scaleImage), cut, margin, superMargin, 0.002, 0.25)

	# Create temporary images
	blurImage = cv.cvCreateImage(cv.cvGetSize(scaleImage), 8, 3)
	workImage = cv.cvCreateImage(cv.cvGetSize(scaleImage), 8, 3)

	# Create a blurred copy of the original
	cv.cvSmooth(scaleImage, blurImage, cv.CV_BLUR, 3, 3, 0)

	# Superimpose the edges onto the blured image
	cv.cvNot(edgeImage, edgeImage)
	cv.cvCopy(blurImage, workImage, edgeImage)

	# Get the edges back to white
	cv.cvNot(edgeImage, edgeImage)

	# We're done with the blurred image now
	cv.cvReleaseImage(blurImage)

	# Retrive the regions touching the cut
	component_dictionary = featureDetector.ribbonFloodFill(scaleImage, edgeImage, workImage, cut, margin, lo, up)

	# Clean up
	cv.cvReleaseImage(workImage)

	# Prune components
	newComponents = regionSelector.pruneRegions(component_dictionary, constraints)

	# Return the dictionary of accepted components
	#transformer.translateBoundingBoxes(newComponents, 1)
	return newComponents
示例#2
0
def findEdges(original, out, threshold1 = 100, threshold2 = None):
	"""Return a new edge detected image with a specified threshold"""
	warnings.warn("Use findBWEdges instead unless you really need colored edges.", DeprecationWarning)

	#Define threshold2
	if threshold2 == None:
		threshold2 = threshold1 * 3

	# Create two pictures with only one channel for a b/w copy
	# and one for storring the edges found in the b/w picture
	gray = cv.cvCreateImage(cv.cvGetSize(original), 8, 1)
	edge = cv.cvCreateImage(cv.cvGetSize(original), 8, 1)

	# Create the b/w copy of the original
	cv.cvCvtColor(original, gray, cv.CV_BGR2GRAY)

	# Blur the b/w copy, but put the result into edge pic
	cv.cvSmooth(gray, edge, cv.CV_BLUR, 3, 3, 0)

	# Negate the b/w copy of original with newly blurred
	# b/w copy. This will make egdes stand out
	cv.cvNot(gray, edge)

	# Run an edge-finding algorithm called 'Canny'
	# It will analyse the first argument and store the
	# resulting picture in the second argument
	cv.cvCanny(gray, edge, threshold1, threshold2)

	# We initialize our out-image to black
	cv.cvSetZero(out)

	# Finally, we use the found edges, which are b/w, as
	# a mask for copying the colored edges from the original
	# to the out-image
	cv.cvCopy(original, out, edge)
示例#3
0
def analyzeCut(original, edgeImage, cut, settings, showBlobs=False):
	"""Extract the interesting features in the vicinity of a given cut"""
	# Get all data from the settings
	lo = settings.lo
	up = settings.up

	# Set up the margin with respect to the cut
	margin = marginCalculator.getPixels(original, cut, settings.marginPercentage)
	superMargin = 0
	# ^^ We don't use superMargin

	# Set up constraints
	constraints = regionSelector.Constraints(cv.cvGetSize(original), cut, margin, superMargin, 0.002, 0.25)

	# Create temporary images
	blurImage = cv.cvCreateImage(cv.cvGetSize(original), 8, 3)
	workImage = cv.cvCreateImage(cv.cvGetSize(original), 8, 3)

	# Create a blurred copy of the original
	cv.cvSmooth(original, blurImage, cv.CV_BLUR, 3, 3, 0)

	# Superimpose the edges onto the blured image
	cv.cvNot(edgeImage, edgeImage)
	cv.cvCopy(blurImage, workImage, edgeImage)

	# We're done with the blurred image now
	cv.cvReleaseImage(blurImage)

	# Get the edges back to white
	cv.cvNot(edgeImage, edgeImage)

	# Retrive the regions touching the cut
	component_dictionary = featureDetector.ribbonFloodFill(original, edgeImage, workImage, cut, margin, lo, up)

	#start expanded

	# Prune components BEFORE we delete the workImage
	tmpnewComponents = regionSelector.pruneExpandedRegions(component_dictionary, constraints)
	newComponents = regionSelector.pruneExpandedRagionsto(tmpnewComponents, constraints, cut, workImage)

	# Clean up only if we do not return the image
	if not showBlobs:
		cv.cvReleaseImage(workImage)

	# Return the dictionary of accepted components or both
	if not showBlobs:
		return newComponents
	else:
		return (workImage, newComponents)
示例#4
0
def on_trackbar (position):

    cv.cvSmooth (gray, edge, cv.CV_BLUR, 3, 3, 0)
    cv.cvNot (gray, edge)

    # run the edge dector on gray scale
    cv.cvCanny (gray, edge, position, position * 3, 3)

    # reset
    cv.cvSetZero (col_edge)

    # copy edge points
    cv.cvCopy (image, col_edge, edge)
    
    # show the image
    highgui.cvShowImage (win_name, col_edge)
示例#5
0
def on_trackbar(position):

    cv.cvSmooth(gray, edge, cv.CV_BLUR, 3, 3, 0)
    cv.cvNot(gray, edge)

    # run the edge dector on gray scale
    cv.cvCanny(gray, edge, position, position * 3, 3)

    # reset
    cv.cvSetZero(col_edge)

    # copy edge points
    cv.cvCopy(image, col_edge, edge)

    # show the image
    highgui.cvShowImage(win_name, col_edge)
示例#6
0
def on_trackbar (position):
    #下面两句应该是没什么用的
    cv.cvSmooth (gray, edge, cv.CV_BLUR, 3, 3, 0)    #图像平滑
    cv.cvNot (gray, edge)     #计算数组元素的按位取反
 
    # run the edge dector on gray scale
    cv.cvCanny (gray, edge, position, position * 3, 3)   #采用 Canny 算法做边缘检测
 
    # reset
    cv.cvSetZero (col_edge)   #清空数组
 
    # copy edge points
    cv.cvCopy (image, col_edge, edge)   #参数edge影响拷贝的结果
 
    # show the image
    highgui.cvShowImage (win_name, col_edge)
示例#7
0
def findBWEdges(original, out, threshold1, threshold2):
	"""Identical with findEdges except that this returns white edges
	on a black background. We really don't need colored edges any longer.
	This also makes it easy to to a manual merge of edge and blur picture."""
	if threshold2 == None:
		threshold2 = threshold1 * 3

	gray = cv.cvCreateImage(cv.cvGetSize(original), 8, 1)

	cv.cvCvtColor(original, gray, cv.CV_BGR2GRAY)

	cv.cvSmooth(gray, out, cv.CV_BLUR, 3, 3, 0)

	cv.cvNot(gray, out)

	cv.cvCanny(gray, out, threshold1, threshold2)

	return out
示例#8
0
def main():

    ct1 = CurvePoint()
    ct2 = CurvePoint()
    sc = CntSC()
    ang = CntAngle()
     
    
    usage = "%prog [options] <imgfile1> <imgfile2>"
    version = "%prog 0.2\nLongbin Chen, [email protected]"
    oparser = optparse.OptionParser(usage=usage, version=version)
    oparser.add_option('-d', '--display', action="store_true", dest = 'display', default = False, help = 'display the image')
    oparser.add_option('-n', '--number', dest = 'num',  type="int", default = 200 , help = 'the number of feature points')
    oparser.add_option('-s', '--save', dest = 'save', default = None, help = 'save the img file')

    oparser.add_option('-o', '--output', dest = 'output', default = None, help = 'output file')

    (options, args) = oparser.parse_args(sys.argv)

    if len(args) != 3:
        oparser.parse_args([sys.argv[0], "--help"])
        sys.exit(1)

    ct1.GetContour(args[1], options.num)
    allkeys = []
    for c in ct1.allselected:
        allkeys = allkeys + c
    sc.ExtractFeature(allkeys)
    ang.ExtractFeature(allkeys,0); 
    allkeys = []
    ct2.GetContour(args[2], options.num)
    for c in ct2.allselected:
        allkeys = allkeys + c
    sc.ExtractFeature(allkeys)
    ang.ExtractFeature(allkeys,0); 

    sumscore = []
    matcher = SmithWaterman()
    ct1.bDrawNumber = 0
    ct2.bDrawNumber = 0
    if (options.display):
        ct1.DrawKeyPoints()
        ct2.DrawKeyPoints()
    myfont = cv.cvInitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5)
    idx = -1
    putoriginal(args[1], ct1.drawimg)
    putoriginal(args[2], ct2.drawimg)
    cv.cvNot(ct1.drawimg, ct1.drawimg)
    cv.cvNot(ct2.drawimg, ct2.drawimg)
    for c1 in ct1.allselected:
        idx += 1
        cscore = -100000000
        cpt1 =   getdata(c1)
        bX = []
        bY = []
        bestcurve = None
        for c2 in ct2.allselected:
            cpt2 =   getdata(c2)
            cost,align,X,Y = matcher.Align(cpt1, cpt2)
            normalized_score = cost - log10(len(c2) + 1) * 1000
            print len(c1), len(c2),cost, normalized_score, cscore
            if (normalized_score > cscore):
                cscore = normalized_score
                bX = X[:]
                bY = Y[:]
                bestcurve = c2
        if (options.display):
            ptcount = 0
            for i in range(len(bX)):
                xi = bX[i]
                yi = bY[i]
                #if (xi == -1):
                    #cv.cvDrawCircle(ct2.drawimg, cv.cvPoint(int(bestcurve[yi].x), int(bestcurve[yi].y)),4, cv.cvScalar(255,0,0,0))
                    #cv.cvPutText(ct2.drawimg, 'O', cv.cvPoint(int(c2[yi].x), int(c2[yi].y)), myfont, cv.cvScalar(255, 0, 0,0))
                #if (yi == -1):
                    #cv.cvDrawCircle(ct1.drawimg, cv.cvPoint(int(c1[xi].x), int(c1[xi].y)),4, cv.cvScalar(255,0,0,0))
                    #cv.cvPutText(ct1.drawimg, 'O', cv.cvPoint(int(c1[xi].x), int(c1[xi].y)), myfont, cv.cvScalar(255, 0, 0,0))
                if (xi != -1 and yi != -1):
                    ptcount  += 1
                    cv.cvDrawCircle(ct1.drawimg, cv.cvPoint(int(c1[xi].x), int(c1[xi].y)),2, clrs[idx])
                    cv.cvPutText(ct1.drawimg, str(ptcount), cv.cvPoint(int(c1[xi].x), int(c1[xi].y)), myfont, clrs[idx])
                    cv.cvDrawCircle(ct2.drawimg, cv.cvPoint(int(bestcurve[yi].x), int(bestcurve[yi].y)),2, clrs[idx])
                    cv.cvPutText(ct2.drawimg, str(ptcount), cv.cvPoint(int(bestcurve[yi].x), int(bestcurve[yi].y)), myfont, clrs[idx])
        sumscore.append(cscore)
    print sumscore
    if (options.display):            
	    highgui.cvNamedWindow ("contour1", 1)
	    highgui.cvNamedWindow ("contour2", 1)
	    highgui.cvShowImage ("contour1", ct1.drawimg)
	    highgui.cvShowImage ("contour2", ct2.drawimg)
	    highgui.cvWaitKey (0)       
    if (options.save):
        mergeimg = mergeimage_83(ct1.drawimg, ct2.drawimg)
        highgui.cvSaveImage("_sw_result.bmp", mergeimg)
示例#9
0
def main():

    ct1 = CurvePoint()
    ct2 = CurvePoint()
    agl = CntAngle()
    sc = CntSC()

    try:
        opts, args = getopt.getopt(sys.argv[1:], "ho:dn:es", ["help", "output=", "draw", "num=", "even", "save"])
    except getopt.GetoptError:
        usage()
        sys.exit(2)
    output = None
    bDraw = 0
    bSave = 0
    bOriginal = 0
    npoint = 100

    for o, a in opts:
        if o == "-v":
            ct1.verbose = 1
            ct2.verbose = 1
        if o in ("-h", "--help"):
            usage()
            sys.exit()
        if o in ("-o", "--output"):
            output = a
        if o in ("-d", "--draw"):
            bDraw = 1
        if o in ("-s", "--save"):
            bSave = 1
        if o in ("-r", "--original"):
            bOriginal = 1
        if o in ("-n", "--num"):
            npoint = string.atoi(a)
        if o in ("-e", "--even"):
            ct1.bEven = 1
            ct2.bEven = 1
    if (len(args)) != 2:
        usage()
        sys.exit(2)

    ct1.GetContour(args[0], npoint)
    allkeys = []
    for c in ct1.allselected:
        # agl.ExtractFeature(c, ct1.drawimg)
        allkeys = allkeys + c
    sc.ExtractFeature(allkeys)

    allkeys = []
    ct2.GetContour(args[1], npoint)
    for c in ct2.allselected:
        # agl.ExtractFeature(c, ct2.drawimg)
        allkeys = allkeys + c
    sc.ExtractFeature(allkeys)

    sumscore = []
    matcher = SmithWaterman()
    ct1.bDrawNumber = 0
    ct2.bDrawNumber = 0
    if bDraw:
        ct1.DrawKeyPoints()
        ct2.DrawKeyPoints()
    myfont = cv.cvInitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5)
    idx = -1
    putoriginal(args[0], ct1.drawimg)
    putoriginal(args[1], ct2.drawimg)
    cv.cvNot(ct1.drawimg, ct1.drawimg)
    cv.cvNot(ct2.drawimg, ct2.drawimg)
    for c1 in ct1.allselected:
        idx += 1
        cscore = -100000000
        cpt1 = getdata(c1)
        bX = []
        bY = []
        bestcurve = None
        for c2 in ct2.allselected:
            cpt2 = getdata(c2)
            cost, align, X, Y = matcher.Align(cpt1, cpt2)
            normalized_score = cost - log10(len(c2) + 1) * 1000
            print len(c1), len(c2), cost, normalized_score, cscore
            if normalized_score > cscore:
                cscore = normalized_score
                bX = X[:]
                bY = Y[:]
                bestcurve = c2
        if bDraw:
            ptcount = 0
            for i in range(len(bX)):
                xi = bX[i]
                yi = bY[i]
                # if (xi == -1):
                # cv.cvDrawCircle(ct2.drawimg, cv.cvPoint(int(bestcurve[yi].x), int(bestcurve[yi].y)),4, cv.cvScalar(255,0,0,0))
                # cv.cvPutText(ct2.drawimg, 'O', cv.cvPoint(int(c2[yi].x), int(c2[yi].y)), myfont, cv.cvScalar(255, 0, 0,0))
                # if (yi == -1):
                # cv.cvDrawCircle(ct1.drawimg, cv.cvPoint(int(c1[xi].x), int(c1[xi].y)),4, cv.cvScalar(255,0,0,0))
                # cv.cvPutText(ct1.drawimg, 'O', cv.cvPoint(int(c1[xi].x), int(c1[xi].y)), myfont, cv.cvScalar(255, 0, 0,0))
                if xi != -1 and yi != -1:
                    ptcount += 1
                    cv.cvDrawCircle(ct1.drawimg, cv.cvPoint(int(c1[xi].x), int(c1[xi].y)), 2, clrs[idx])
                    cv.cvPutText(ct1.drawimg, str(ptcount), cv.cvPoint(int(c1[xi].x), int(c1[xi].y)), myfont, clrs[idx])
                    cv.cvDrawCircle(ct2.drawimg, cv.cvPoint(int(bestcurve[yi].x), int(bestcurve[yi].y)), 2, clrs[idx])
                    cv.cvPutText(
                        ct2.drawimg,
                        str(ptcount),
                        cv.cvPoint(int(bestcurve[yi].x), int(bestcurve[yi].y)),
                        myfont,
                        clrs[idx],
                    )
        sumscore.append(cscore)
    print sumscore
    if bDraw:
        highgui.cvNamedWindow("contour1", 1)
        highgui.cvNamedWindow("contour2", 1)
        highgui.cvShowImage("contour1", ct1.drawimg)
        highgui.cvShowImage("contour2", ct2.drawimg)
        highgui.cvWaitKey(0)
    if bSave:
        mergeimg = mergeimage_83(ct1.drawimg, ct2.drawimg)
        highgui.cvSaveImage("_sw_result.bmp", mergeimg)
示例#10
0
def main():

    ct1 = CurvePoint()
    ct2 = CurvePoint()
    sc = CntSC()
    ang = CntAngle()

    usage = "%prog [options] <imgfile1> <imgfile2>"
    version = "%prog 0.2\nLongbin Chen, [email protected]"
    oparser = optparse.OptionParser(usage=usage, version=version)
    oparser.add_option('-d',
                       '--display',
                       action="store_true",
                       dest='display',
                       default=False,
                       help='display the image')
    oparser.add_option('-n',
                       '--number',
                       dest='num',
                       type="int",
                       default=200,
                       help='the number of feature points')
    oparser.add_option('-s',
                       '--save',
                       dest='save',
                       default=None,
                       help='save the img file')

    oparser.add_option('-o',
                       '--output',
                       dest='output',
                       default=None,
                       help='output file')

    (options, args) = oparser.parse_args(sys.argv)

    if len(args) != 3:
        oparser.parse_args([sys.argv[0], "--help"])
        sys.exit(1)

    ct1.GetContour(args[1], options.num)
    allkeys = []
    for c in ct1.allselected:
        allkeys = allkeys + c
    sc.ExtractFeature(allkeys)
    ang.ExtractFeature(allkeys, 0)
    allkeys = []
    ct2.GetContour(args[2], options.num)
    for c in ct2.allselected:
        allkeys = allkeys + c
    sc.ExtractFeature(allkeys)
    ang.ExtractFeature(allkeys, 0)

    sumscore = []
    matcher = SmithWaterman()
    ct1.bDrawNumber = 0
    ct2.bDrawNumber = 0
    if (options.display):
        ct1.DrawKeyPoints()
        ct2.DrawKeyPoints()
    myfont = cv.cvInitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5)
    idx = -1
    putoriginal(args[1], ct1.drawimg)
    putoriginal(args[2], ct2.drawimg)
    cv.cvNot(ct1.drawimg, ct1.drawimg)
    cv.cvNot(ct2.drawimg, ct2.drawimg)
    for c1 in ct1.allselected:
        idx += 1
        cscore = -100000000
        cpt1 = getdata(c1)
        bX = []
        bY = []
        bestcurve = None
        for c2 in ct2.allselected:
            cpt2 = getdata(c2)
            cost, align, X, Y = matcher.Align(cpt1, cpt2)
            normalized_score = cost - log10(len(c2) + 1) * 1000
            print len(c1), len(c2), cost, normalized_score, cscore
            if (normalized_score > cscore):
                cscore = normalized_score
                bX = X[:]
                bY = Y[:]
                bestcurve = c2
        if (options.display):
            ptcount = 0
            for i in range(len(bX)):
                xi = bX[i]
                yi = bY[i]
                #if (xi == -1):
                #cv.cvDrawCircle(ct2.drawimg, cv.cvPoint(int(bestcurve[yi].x), int(bestcurve[yi].y)),4, cv.cvScalar(255,0,0,0))
                #cv.cvPutText(ct2.drawimg, 'O', cv.cvPoint(int(c2[yi].x), int(c2[yi].y)), myfont, cv.cvScalar(255, 0, 0,0))
                #if (yi == -1):
                #cv.cvDrawCircle(ct1.drawimg, cv.cvPoint(int(c1[xi].x), int(c1[xi].y)),4, cv.cvScalar(255,0,0,0))
                #cv.cvPutText(ct1.drawimg, 'O', cv.cvPoint(int(c1[xi].x), int(c1[xi].y)), myfont, cv.cvScalar(255, 0, 0,0))
                if (xi != -1 and yi != -1):
                    ptcount += 1
                    cv.cvDrawCircle(ct1.drawimg,
                                    cv.cvPoint(int(c1[xi].x), int(c1[xi].y)),
                                    2, clrs[idx])
                    cv.cvPutText(ct1.drawimg, str(ptcount),
                                 cv.cvPoint(int(c1[xi].x), int(c1[xi].y)),
                                 myfont, clrs[idx])
                    cv.cvDrawCircle(
                        ct2.drawimg,
                        cv.cvPoint(int(bestcurve[yi].x), int(bestcurve[yi].y)),
                        2, clrs[idx])
                    cv.cvPutText(
                        ct2.drawimg, str(ptcount),
                        cv.cvPoint(int(bestcurve[yi].x), int(bestcurve[yi].y)),
                        myfont, clrs[idx])
        sumscore.append(cscore)
    print sumscore
    if (options.display):
        highgui.cvNamedWindow("contour1", 1)
        highgui.cvNamedWindow("contour2", 1)
        highgui.cvShowImage("contour1", ct1.drawimg)
        highgui.cvShowImage("contour2", ct2.drawimg)
        highgui.cvWaitKey(0)
    if (options.save):
        mergeimg = mergeimage_83(ct1.drawimg, ct2.drawimg)
        highgui.cvSaveImage("_sw_result.bmp", mergeimg)
示例#11
0
 def SaveImage(self, filename):
     cv.cvNot(self.drawimg, self.drawimg)
     highgui.cvSaveImage(filename, self.drawimg)
示例#12
0
 def SaveImage(self, filename):
     cv.cvNot(self.drawimg, self.drawimg)
     highgui.cvSaveImage(filename, self.drawimg)
示例#13
0
print "Finding the golden means in the picture"

lines = lib.findGoldenMeans(cv.cvGetSize(image))
#lines = lib.findMeans(cv.cvGetSize(image), lib.PHI)

print "Test plot and line scanner methods"
points = lineScanner.naiveBWLineScanner(edges, lines[0])

#cv.cvSmooth(out, out, cv.CV_MEDIAN, 7, 7, 0)
cv.cvSmooth(image, blurImage, cv.CV_BLUR, 3, 3, 0)
#cv.cvSmooth(out, out, cv.CV_GAUSSIAN, 7, 7, 0)
#out = blurImage

# Superimpose the edges onto the blured image
cv.cvNot(edges, edges)
cv.cvCopy(blurImage, out, edges)

# We're done with the blurred image now
#cv.cvReleaseImage(blurImage)

#print points[:0]
cut = lines[1]
margin = marginCalculator.getPixels(image, cut, 0.024)
component_dictionary = featureDetector.ribbonFloodFill(image, edges, out, cut, margin, lo, up)
#featureDetector.floodFillLine(image, out, points, cut, lo, up, {})
#flags = cv.CV_FLOODFILL_FIXED_RANGE
#flags = 4
color = lib.getRandomColor()
comp = cv.CvConnectedComp()
#cv.cvFloodFill(out, cv.cvPoint(x,y), color, cv.CV_RGB(lo,lo,lo), cv.CV_RGB(up,up,up),comp ,flags)#, None);