/** * noteSeg.c * OpenCV Introduction: Demo 2 * David Johnston, 2013-01-29 */ #include "cv.h" #include "highgui.h" void display(IplImage* img) { cvShowImage("DEBUG", img); cvWaitKey(0); } int main(int argc, char *argv[]) { // Load image file, create and initialize other images (all to the size of the original) int i; IplImage* original = cvLoadImage("inputs/noteSeg.jpg", CV_LOAD_IMAGE_GRAYSCALE); CvSize size = cvGetSize(original); IplImage* binary = cvCreateImage(size, IPL_DEPTH_8U, 1); // Intermediates IplImage* noCircle = cvCreateImage(size, IPL_DEPTH_8U, 1); IplImage* body; // Outputs IplImage* stem; IplImage* tail; IplImage* colored = cvCreateImage(size, IPL_DEPTH_8U, 3); cvThreshold(original, binary, 120, 255, CV_THRESH_BINARY_INV); // Threshold and invert display(binary); body = cvCloneImage(binary); stem = cvCloneImage(binary); tail = cvCloneImage(binary); // Declare and initialize an anonymous struct with three structuring elements struct { IplConvKernel* circle; IplConvKernel* vert; IplConvKernel* plus; } se; se.circle = cvCreateStructuringElementEx(3, 3, 1, 1, CV_SHAPE_ELLIPSE, NULL); // 3x3 circle se.vert = cvCreateStructuringElementEx(1, 15, 0, 7, CV_SHAPE_RECT, NULL); // vertical line se.plus = cvCreateStructuringElementEx(3, 3, 1, 1, CV_SHAPE_CROSS, NULL); // plus sign // Isolate the circles with the circular structuring element cvErode(body, body, se.circle, 4); cvDilate(body, body, se.circle, 4); //display(body); // Initialize the noCircle image cvSub(binary, body, noCircle, NULL); //display(noCircle); // Isolate the stems with the vertical structuring element cvErode(noCircle, stem, se.vert, 1); cvDilate(stem, stem, se.vert, 1); //display(stem); // Isolate the tails by removing the stems from the noCircle image, // then clean up some speckles with a plus structuring element cvSub(noCircle, stem, tail, NULL); cvErode(tail, tail, se.plus, 1); cvDilate(tail, tail, se.plus, 1); //display(tail); // Use our three outputs as bit-masks to set regions of 'colored'. cvSet(colored, cvScalar(0xFF, 0xFF, 0xFF, 0), NULL); // Set all white cvSet(colored, cvScalar(0xFF, 0, 0, 0), body); // Set body blue cvSet(colored, cvScalar( 0, 0xFF, 0, 0), stem); // Set stem green cvSet(colored, cvScalar( 0, 0, 0xFF, 0), tail); // Set tail red // Invert then write each of the images cvNot(body, body); cvNot(stem, stem); cvNot(tail, tail); cvSaveImage("outputs/noteSeg1.png", body, 0); cvSaveImage("outputs/noteSeg2.png", stem, 0); cvSaveImage("outputs/noteSeg3.png", tail, 0); cvSaveImage("outputs/noteSeg4.png", colored, 0); // Clean up memory cvReleaseImage(&binary); cvReleaseImage(&noCircle); cvReleaseImage(&body); cvReleaseImage(&stem); cvReleaseImage(&tail); cvReleaseStructuringElement(&se.circle); cvReleaseStructuringElement(&se.vert); cvReleaseStructuringElement(&se.plus); return 0; }