Exemple #1
0
def getBlobImage(original, settings, cutNo):
	"""Show the colored blobs in an image at the cut specified
	by cutNo as int. The cut ratio are placed in the settings and only
	the first cut in this ratio-list will be analyzed.
	original should be the image data
	settings should be of class Settings
	cutNo as int"""

	# Get the cut defined by cutNo from the cuts from the first cut ratio in settings
	cut = lib.findMeans(cv.cvGetSize(original), settings.cutRatios[0])[cutNo]

	# Get the BW edge image
	edgeImage = getEdgeImage(original, settings)

	# Find the margin
	margin = marginCalculator.getPixels(original, cut, settings.marginPercentage)

	# Clever hack for putting the cut in an array
	tmp = []
	tmp.append(cut)

	# Get results
	(blobImage, components) = analyzeCut(original, edgeImage, cut, settings, True)
	lib.drawLines(blobImage, blobImage, tmp)
	lib.drawMargin(blobImage, cut, margin)

	# Return result, what a surprise
	return blobImage
Exemple #2
0
def getBoundingBoxImage(original, settings, cutNo, thickness=1, color=None):
	"""Same as above but will paint the bounding boxes
	original should be the image data
	settings should be of class Settings
	cutNo as int
	color as CV_RGB"""

	# Get the cut defined by cutNo from the cuts from the first cut ratio in settings
	cut = lib.findMeans(cv.cvGetSize(original), settings.cutRatios[0])[cutNo]

	# Get the BW edge image
	edgeImage = getEdgeImage(original, settings)

	# Find the margin
	margin = marginCalculator.getPixels(original, cut, settings.marginPercentage)

	tmp = []
	tmp.append(cut)
	components = analyzeCut(original, edgeImage, cut, settings)
	lib.drawMargin(original, cut, margin)

	# Draw the components
	lib.drawBoundingBoxes(original, components, thickness, color)

	return original
Exemple #3
0
def analyzeImage(original, settings):
	"""Runs the analysis on all cuts on an image"""
	# Get the BW edge image
	edgeImage = getEdgeImage(original, settings)

	# Get cuts and place then in a dictionary by cut ratio
	# XXX: Notice the ugly string conversion because python has an issue when
	# converting the ratio to a dictionary index
	cuts = {}
	for ratio in settings.cutRatios:
		cuts[str(ratio)] = lib.findMeans(cv.cvGetSize(original), ratio)

	# New dictionary for holding the resulting components
	# Hold on, now we're putting the result (which is a dictionary)
	# inside a new dict (cutDict). This holds the result for the four cuts
	# for a given ratio. We now put this dict inside the comps-dictionary
	# which then can be used for lookup by the cut-ratio
	comps = {}
	for ratio in cuts:
		cutDict = {}
		for cutNo in range(len(cuts[ratio])):
			cutComponents = analyzeCut(original, edgeImage, cuts[ratio][cutNo], settings)
			cutDict[cutNo] = cutComponents
		comps[ratio] = cutDict

	# Clean up
	cv.cvReleaseImage(edgeImage)

	# This is a dictionary in a dictionary in a dictionary
	return comps
def main():
	"""
	Just the test
	This method is a god resource on how to handle the results
	"""

	filename = sys.argv[1]
	image = highgui.cvLoadImage (filename)

	print "DO NOT EXPECT THE RUNNING TIME OF THIS TEST TO BE REPRESENTATIVE!"
	print ""
	print "THRESHOLDS AND EVERYTHING ELSE ARE HARDCODED!"

	cutRatios = [0.6667, lib.PHI, 0.6]
	settings = Settings(cutRatios)

	# Run the analysis with the above settings
	comps = naiveMethod.analyzeImage(image, settings)

	# This is just for drawing the results
	# The below methods can probably be combined but don't bother
	# {{{
	# Get and draw the cuts
	cuts = {}
	for ratio in settings.cutRatios:
		cuts[str(ratio)] = lib.findMeans(cv.cvGetSize(image), ratio)

	for ratio in cuts:
		lib.drawLines(image, None, cuts[ratio], lib.getRandomColor())

	# Get and draw the components
	for ratio in comps:
		for cut in comps[ratio]:
			lib.drawBoundingBoxes(image, comps[ratio][cut])
	# }}}

	winname = "Failure"

	highgui.cvNamedWindow (winname, highgui.CV_WINDOW_AUTOSIZE)

	while True:
		highgui.cvShowImage (winname, image)

		c = highgui.cvWaitKey(0)

		if c == 'q':
			print "Exiting ..."
			print ""
			sys.exit(0)
Exemple #5
0
def main():
	"""
	Just the test
	This method is a good resource on how to handle the results.
	Save images in this method if you have to.
	"""

	filename = sys.argv[1]
	image = highgui.cvLoadImage (filename)

	cutRatios = [lib.PHI]
	#cutRatios = [0.618]
	settings = Settings(cutRatios)
	image = highgui.cvLoadImage (filename)
	thickness = 4
	settings.setMarginPercentage(0.025)
	settings.setMethod(sys.argv[3])
	cut = int(sys.argv[2])
	winname = sys.argv[1]
	#settings.setThresholds(100,150)
	# Set the color for the boxes
	#color = lib.COL_BLACK
	#color = lib.COL_WHITE
	#color = lib.COL_RED
	color = lib.COL_GREEN
	#color = lib.COL_BLUE

	blobImg = blobResult(image, settings, cut)
	boxxImg = boundingBoxResult(image, settings, cut, thickness, color)
	cutt = lib.findMeans(cv.cvGetSize(image), settings.cutRatios[0])[cut]
	# cuttet verdi, dog skal det vi generaliseres lidt
	oriantesen = cutt.getPoints()[0].x == cutt.getPoints()[1].x
	if oriantesen:
		cutPixel = cutt.getPoints()[1].x
	else:
		cutPixel = cutt.getPoints()[1].y
	
	if oriantesen:
	#	print 'hej'
		cv.cvLine(boxxImg, cv.cvPoint(cutPixel, cutt.getPoints()[0].y), cv.cvPoint(cutPixel, cutt.getPoints()[1].y), lib.COL_RED)
	else:
		cv.cvLine(boxxImg, cv.cvPoint(cutt.getPoints()[0].x, cutPixel), cv.cvPoint(cutt.getPoints()[1].x, cutPixel), lib.COL_RED)
	# Save images
	highgui.cvSaveImage('flood_cut_%s.png' % cut, boxxImg)
	highgui.cvSaveImage('blobs_cut_%s.png' % cut, blobImg)

	# Show images
	compareImages(blobImg, boxxImg, "blob", winname)