def processAndClassify(frame):
    backgroundClass = 3
    predictionArray = []
    centroidArray   = []
    # Convert the image into a grayscale image
    grayScaleInput = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # Apply meanshift on the RGB image
    meanShiftResult = prePro.meanShift(frame)
    # Convert the result of the Mean shifted image into Grayscale
    meanShiftGray = cv2.cvtColor(meanShiftResult, cv2.COLOR_BGR2GRAY)
    # Apply adaptive thresholding on the resulting greyscale image
    meanShiftAdapResult = prePro.adapThresh(meanShiftGray)
    # Draw Contours on the Input image with results from the meanshift 
    contourPlot = prePro.contourDraw(frame, meanShiftAdapResult)
    # Find the contours on the mean shifted image
    contours, hierarchy = prePro.contourFindFull(meanShiftAdapResult)
    # Find the contours on the mean shifted image
    boundBoxContour = grayScaleInput.copy()
    frameBoundingBox = frame.copy()
    # For each contour
    for cnt in contours: 
		# If the area covered by the contour is greater than 500 pixels
        if cv2.contourArea(cnt)>20:
			# Get the bounding box of the contour
            [x, y, w, h] = cv2.boundingRect(cnt)
			# Get the moments of the each contour for computing the centroid of the contour
            moments = cv2.moments(cnt)
            if moments['m00']!=0:
                cx = int(moments['m10']/moments['m00'])         # cx = M10/M00
                cy = int(moments['m01']/moments['m00'])         # cy = M01/M00
                centroid = (cx,cy)
			# cx,cy are the centroid of the contour
                extendBBox = 10
			# Extend it by 10 pixels to avoid missing the key points on the edges
                roiImage = boundBoxContour[y-extendBBox:y+h+extendBBox, x-extendBBox:x+w+extendBBox]
                roiImageFiltered = roiImage
			# Detect the corner key points
                kp, roiKeyPointImage = detDes.featureDetectCorner(roiImageFiltered)
			# Use the ORB feature detector and descriptor on the contour
                kp, des, roiKeyPointImage = detDes.featureDescriptorORB(roiImageFiltered, kp)
                if np.size(kp)>0:
                    histPoints = tH.histogramContour(des)
                    prediction = tC.classify(np.float32(histPoints))
				## If the predicted class is not the background class then add the prediction to the prediction array and get its centroid
                    if prediction != backgroundClass:
                        cv2.rectangle(frameBoundingBox,(x,y),(x+w,y+h),(0,255,0),2)
    return contourPlot
	
	
	
	
	
	
	
	
	
	
	
 
	
def processAndClassify(frame):
    backgroundClass = 3
    predictionArray = []
    centroidArray = []
    # Convert the image into a grayscale image
    grayScaleInput = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # Apply meanshift on the RGB image
    meanShiftResult = prePro.meanShift(frame)
    # Convert the result of the Mean shifted image into Grayscale
    meanShiftGray = cv2.cvtColor(meanShiftResult, cv2.COLOR_BGR2GRAY)
    # Apply adaptive thresholding on the resulting greyscale image
    meanShiftAdapResult = prePro.adapThresh(meanShiftGray)
    # Draw Contours on the Input image with results from the meanshift
    contourPlot = prePro.contourDraw(frame, meanShiftAdapResult)

    #cv2.imwrite('contourPlot.png',contourPlot)
    # Find the contours on the mean shifted image
    contours, hierarchy = prePro.contourFindFull(meanShiftAdapResult)
    # Find the contours on the mean shifted image
    boundBoxContour = grayScaleInput.copy()
    frameBoundingBox = frame.copy()
    # For each contour
    for cnt in contours:
        # If the area covered by the contour is greater than 500 pixels
        if cv2.contourArea(cnt) > 20:
            # Get the bounding box of the contour
            [x, y, w, h] = cv2.boundingRect(cnt)
            # Get the moments of the each contour for computing the centroid of the contour
            moments = cv2.moments(cnt)
            if moments['m00'] != 0:
                cx = int(moments['m10'] / moments['m00'])  # cx = M10/M00
                cy = int(moments['m01'] / moments['m00'])  # cy = M01/M00
                centroid = (cx, cy)
                # cx,cy are the centroid of the contour
                extendBBox = 10
                # Extend it by 10 pixels to avoid missing the key points on the edges
                roiImage = boundBoxContour[y - extendBBox:y + h + extendBBox,
                                           x - extendBBox:x + w + extendBBox]
                roiImageFiltered = roiImage
                # Detect the corner key points
                kp, roiKeyPointImage = detDes.featureDetectCorner(
                    roiImageFiltered)
                # Use the ORB feature detector and descriptor on the contour
                kp, des, roiKeyPointImage = detDes.featureDescriptorORB(
                    roiImageFiltered, kp)
                if np.size(kp) > 0:
                    histPoints = tH.histogramContour(des)
                    prediction = tC.classify(np.float32(histPoints))
                    ## If the predicted class is not the background class then add the prediction to the prediction array and get its centroid
                    if prediction != backgroundClass:
                        cv2.rectangle(frameBoundingBox, (x, y), (x + w, y + h),
                                      (0, 255, 0), 2)
    return predictionArray, centr
示例#3
0
formatName = '*.jpg'
fileList = glob.glob('dataset/' + formatName)

for fileName in fileList:
    image = cv2.imread(fileName)

    grayScaleInput = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # Apply meanshift on the RGB image
    meanShiftResult = prePro.meanShift(image)
    # Convert the result of the Mean shifted image into Grayscale
    meanShiftGray = cv2.cvtColor(meanShiftResult, cv2.COLOR_BGR2GRAY)
    # Apply adaptive thresholding on the resulting greyscale image
    meanShiftAdapResult = prePro.adapThresh(meanShiftGray)
    # Draw Contours on the Input image with results from the meanshift
    contourPlot = prePro.contourDraw(image, meanShiftAdapResult)
    cv2.imshow('contourplot', contourPlot)
    # Find the contours on the mean shifted image
    contours, hierarchy = prePro.contourFind(meanShiftAdapResult)

    ## Use Histogram equalization
    boundBoxContour = grayScaleInput.copy()
    count = 0
    # For each contour
    for cnt in contours:
        # If the area covered by the contour is greater than 500 pixels
        if cv2.contourArea(cnt) > 500:
            # Get the bounding box of the contour
            [x, y, w, h] = cv2.boundingRect(cnt)
            # Get the moments of the each contour for computing the centroid of the contour
            moments = cv2.moments(cnt)
print ("Current Directory Name:" + rootInputName)
count = 0
for files in fileList:
	inputImage=cv2.imread(fileList[count])
	fileName = os.path.basename(fileList[count])
	print ("Processing Image " + fileList[count])
	grayScaleInput = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)
	
	meanShiftResult = prePro.meanShift(inputImage)
	cv2.imwrite(rootMeanName + fileName, meanShiftResult)
	
	meanShiftGray = cv2.cvtColor(meanShiftResult, cv2.COLOR_BGR2GRAY)
	meanShiftAdapResult = prePro.adapThresh(meanShiftGray)
	cv2.imwrite(rootAdapThreshold + fileName, meanShiftAdapResult)
	
	contourPlot = prePro.contourDraw(inputImage, meanShiftAdapResult)	
	cv2.imwrite(rootContourDraw + fileName, contourPlot)
	
	print ("Preprocessing Results Written.")
	
	contours, hierarchy = prePro.contourFindFull(meanShiftAdapResult)
	boundBoxContour = grayScaleInput.copy()
	counter = 0
	for cnt in contours:
	    if cv2.contourArea(cnt)>20:
		    print("Processing Contour no.", str(counter))
		    [x, y, w, h] = cv2.boundingRect(cnt)
		    extendBBox = 5
		    roiImage = boundBoxContour[y-extendBBox:y+h+extendBBox, x-extendBBox:x+w+extendBBox]
		    roiImageFiltered = roiImage  #cv2.medianBlur(roiImage, 3)
		    kp, roiKeyPointImage = detDes.featureDetectCorner(roiImageFiltered)
        #        kernel = np.ones((5,5),np.uint8)
        #        opening = cv2.dilate(meanShiftAdapResult,kernel,iterations=1)
        #	# Draw Contours on the Input image with results from the meanshift
        #        contourPlot = prePro.contourDraw(frame, opening)
        #        cv2.imwrite('contourplot.png',contourPlot)
        #	# Find the contours on the mean shifted image
        #        contours, hierarchy = prePro.contourFindFull(opening)
        #	# Draw Contours on the Input image with results from the meanshift
        '''##############################################Morphology ends ##########################################################################

                            Comment the following lines to add morphology
                            Uncomment them if no morphology needs to be used

#########################################   No Morphology begins ########################################################################'''

        contourPlot = prePro.contourDraw(frame, meanShiftAdapResult)
        #cv2.imshow('contourplot',contourPlot)
        # Find the contours on the mean shifted image
        contours, hierarchy = prePro.contourFindFull(meanShiftAdapResult)
        '''########################################  No Morphology ends ########################################################################'''
        ## Use Histogram equalization

        boundBoxContour = grayScaleInput.copy()
        counter = 0
        for cnt in contours:
            if cv2.contourArea(cnt) > 500:
                print("Processing Contour no.", str(counter))
                [x, y, w, h] = cv2.boundingRect(cnt)
                extendBBox20 = 20
                extendBBox10 = 10
                left = 0
meanShiftOtsuContours = 'meanShiftOtsuContours/'
meanShiftAda = 'meanShiftAda/'
meanShiftAdaContours = 'meanShiftAdaContours/'
cannyEdges = 'cannyEdges/'
cannyEdgesContours = 'cannyEdgesContours/'
meanShiftCanny = 'meanShiftCanny/'
meanShiftCannyContours = 'meanShiftCannyContours/'

inputFileName = 'yellowdice.png'
inputImage = cv2.imread(inputFileName)
grayScaleInput = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)
#
#
# Otsu Result
otsuResult = prePro.otsuBin(grayScaleInput)
otsuContourPlot = prePro.contourDraw(grayScaleInput, otsuResult)


# meanShiftOtsu Result
meanShiftResult = prePro.meanShift(inputImage)
meanShiftGray = cv2.cvtColor(meanShiftResult, cv2.COLOR_BGR2GRAY)
meanShiftOtsuResult = prePro.otsuBin(meanShiftGray)
mOtsuContourPlot = prePro.contourDraw(grayScaleInput, meanShiftOtsuResult)

# Adaptive Threshold
meanShiftAdaResult = prePro.adapThresh(meanShiftGray)
meanShiftAdaContourPlot =  prePro.contourDraw(grayScaleInput, meanShiftAdaResult)

# Canny Edges Result 
cannyEdgesResult = prePro.cannyEdge(grayScaleInput)
cannyEdgesContourPlot = prePro.contourDraw(grayScaleInput, cannyEdgesResult)
meanShiftOtsuContours = 'meanShiftOtsuContours/'
meanShiftAda = 'meanShiftAda/'
meanShiftAdaContours = 'meanShiftAdaContours/'
cannyEdges = 'cannyEdges/'
cannyEdgesContours = 'cannyEdgesContours/'
meanShiftCanny = 'meanShiftCanny/'
meanShiftCannyContours = 'meanShiftCannyContours/'

inputFileName = 'yellowdice.png'
inputImage = cv2.imread(inputFileName)
grayScaleInput = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)
#
#
# Otsu Result
otsuResult = prePro.otsuBin(grayScaleInput)
otsuContourPlot = prePro.contourDraw(grayScaleInput, otsuResult)

# meanShiftOtsu Result
meanShiftResult = prePro.meanShift(inputImage)
meanShiftGray = cv2.cvtColor(meanShiftResult, cv2.COLOR_BGR2GRAY)
meanShiftOtsuResult = prePro.otsuBin(meanShiftGray)
mOtsuContourPlot = prePro.contourDraw(grayScaleInput, meanShiftOtsuResult)

# Adaptive Threshold
meanShiftAdaResult = prePro.adapThresh(meanShiftGray)
meanShiftAdaContourPlot = prePro.contourDraw(grayScaleInput,
                                             meanShiftAdaResult)

# Canny Edges Result
cannyEdgesResult = prePro.cannyEdge(grayScaleInput)
cannyEdgesContourPlot = prePro.contourDraw(grayScaleInput, cannyEdgesResult)
#	# Draw Contours on the Input image with results from the meanshift 
#        contourPlot = prePro.contourDraw(frame, opening)
#        cv2.imwrite('contourplot.png',contourPlot)
#	# Find the contours on the mean shifted image
#        contours, hierarchy = prePro.contourFindFull(opening)
#	# Draw Contours on the Input image with results from the meanshift 
        

        '''##############################################Morphology ends ##########################################################################

                            Comment the following lines to add morphology
                            Uncomment them if no morphology needs to be used

#########################################   No Morphology begins ########################################################################'''        
        
        contourPlot = prePro.contourDraw(frame, meanShiftAdapResult)
    #cv2.imshow('contourplot',contourPlot)
	# Find the contours on the mean shifted image
        contours, hierarchy = prePro.contourFindFull(meanShiftAdapResult)
    
        '''########################################  No Morphology ends ########################################################################'''
    ## Use Histogram equalization  
  
        boundBoxContour = grayScaleInput.copy()
        counter = 0
        for cnt in contours:
            if cv2.contourArea(cnt)>500:
                print("Processing Contour no.", str(counter))
                [x, y, w, h] = cv2.boundingRect(cnt)
                extendBBox20 = 20
                extendBBox10 = 10