New example for
cvContourArea() function
Prepared test image (1 month is stored)
http://rghost.ru/50527445Code:
; OpenCV
; Image filtration on the minimum area of a contour
; (removal of small elements of the image or noise)
; Based on JHPJHP code cv_contours.pb
;
; The filter On/Off by #FilterOn value
;
; AAT
;==================================================
IncludeFile "includes/cv_functions.pbi"
#CV_WINDOW_NAME = "Contour Area Filter"
#FilterOn = #True ; Filter On = #True, Off = #False
#FilterValue = 35 ; minimal area of the visible contour
ImportC "opencv_imgproc247.lib"
cvContourArea.d(*contour, start_index.i, end_index.i, oriented.i)
EndImport
ImageFolder.s = Space(#MAX_PATH)
SHGetFolderPath_(#Null, #CSIDL_MYPICTURES, 0, 0, ImageFolder)
PathAddBackslash_(ImageFolder)
Pattern.s = "All Images (*.*)|*.bmp;*.dib;*.jpeg;*.jpg;*.jpe;*.png;*.tiff;*.tif|Windows Bitmaps (*.bmp)|*.bmp;*.dib|JPEG Files (*.jpg)|*.jpeg;*.jpg;*.jpe|Portable Network Graphics (*.png)|*.png|TIFF Files (*.tiff)|*.tiff;*.tif"
ImageFile.s = OpenFileRequester("Please choose an image file", ImageFolder, Pattern, 0)
If ImageFile
*image.IplImage
*gray.IplImage
*contour.IplImage
carea.d
cvNamedWindow(#CV_WINDOW_NAME, #CV_WINDOW_NORMAL)
*image = cvLoadImage(ImageFile, #CV_LOAD_IMAGE_ANYDEPTH | #CV_LOAD_IMAGE_ANYCOLOR)
cvResizeWindow(#CV_WINDOW_NAME, *image\width, *image\height)
cvMoveWindow(#CV_WINDOW_NAME, 20, 20)
*storage.CvMemStorage
; *contours.CvContour
*contours.CvSeq ; same result as *contours.CvContour
*storage = cvCreateMemStorage(0)
*gray = cvCreateImage(*image\width, *image\height, #IPL_DEPTH_8U, 1)
cvCvtColor(*image, *gray, #CV_BGR2GRAY, 1)
cvThreshold(*gray, *gray, 64, 255, #CV_THRESH_BINARY)
nContours = cvFindContours(*gray, *storage, @*contours, SizeOf(CvContour), #CV_RETR_LIST, #CV_CHAIN_APPROX_SIMPLE, 0, 0)
If nContours
*contour = cvCreateImage(*gray\width, *gray\height, #IPL_DEPTH_8U, 3)
cvSet(*contour, 5, 5, 5, 0, #Null)
; Debug("#Counturs="+Str(nContours))
For rtnCount = 1 To nContours
carea = cvContourArea(*contours, 0, $3fffffff, 0)
; Debug("Contour Area="+StrD(carea))
If #FilterOn = #True
If carea => #FilterValue
cvDrawContours(*contour, *contours, 255, 155, 0, 0, 155, 255, 0, 0, -1, 1, #CV_AA, 0, 0)
EndIf
Else
cvDrawContours(*contour, *contours, 0, 155, 255, 0, 0, 255, 255, 0, -1, 1, #CV_AA, 0, 0)
EndIf
*contours = *contours\h_next
Next
Repeat
If *contour
cvShowImage(#CV_WINDOW_NAME, *contour)
keyPressed = cvWaitKey(0)
EndIf
Until keyPressed = 27
cvDestroyWindow(#CV_WINDOW_NAME)
cvClearMemStorage(*storage)
cvReleaseImage(@*contour)
cvReleaseImage(@*gray)
cvReleaseImage(@*image)
EndIf
EndIf