Free image selection (preferably in C ++)

I am new to image manipulation. I noticed that you can specify a rectangular area of ​​interest , and others, such as circles , etc. in image processing libraries like opencv. Basic paint programs such as ms-paint include free form selection, but I cannot find a function or tutorial on how to make free form selection in opencv or other image processing libraries. Any ideas on how to achieve this? PS: My preferred language is c / C ++. enter image description here

+5
source share
2 answers

, :

2d-, . 1- , , 0.

void cvFillPoly(CvArr* img, CvPoint** pts, int* npts, int contours, CvScalar color, int lineType=8, int shift=0)

http://opencv.willowgarage.com/documentation/drawing_functions.html

, .

+3

, , , . .

, OpenCV , - ( )! , .

#include <stdio.h>    
#include <cv.h>
#include <highgui.h>

// mouse callback    
void on_mouse(int event, int x, int y, int flags, void* param)
{
    // Remove comment below to paint only when left mouse button is pressed
    //if (event == CV_EVENT_LBUTTONDOWN)
    {
        //fprintf(stderr, "Painting at %dx%d\n", x, y);
        IplImage* img = (IplImage*)param;

        cvCircle(img, cvPoint(x,y), 1, CV_RGB(0, 255, 0), -1, CV_AA, 0);
            cvShowImage("cvPaint", img);
    }
}


int main(int argc, char* argv[])
{
    if (argc < 2)
    {
        fprintf( stderr, "Usage: %s <img>\n", argv[0]);
        return -1;
    }

    IplImage* frame = NULL; 
    frame = cvLoadImage(argv[1], CV_LOAD_IMAGE_UNCHANGED);
    if (!frame)
    {
        fprintf( stderr, "Failed: Couldn't load file %s\n", argv[1]);
        return -1;
    }

    cvNamedWindow("cvPaint", CV_WINDOW_AUTOSIZE);
    cvShowImage("cvPaint", frame);

    cvSetMouseCallback("cvPaint", &on_mouse, frame);
    while (1)
    {
        // Keep looping to prevent the app from exiting, 
        // so the mouse callback can be called by OpenCV and do some painting

        char key = cvWaitKey(10);
        if (key  == 113 || key == 27) // q was pressed on the keyboard
            break;
    }

    cvReleaseImage(&frame);
    cvDestroyWindow("cvPaint");

    return 0;
}

, - , . , Qt. , win32 X.

, , , : OpenCV

, , , , . . , :

CvScalar s;        
for (x=0; x<width-1; x++)
{
    for(y=0; y<height-1; y++)
    {
        s = cvGet2D(binImage, y, x);
        if (s.val[0] == 1)
        {
            minX = min(minX, x);
            minY = min(minY, y);
            maxX = max(maxX, x);
            maxY = max(maxY, y);
        }   
    }
}

cvSetImageROI(binImage, cvRect(minX, minY, maxX-minX, maxY-minY));

, , , , .

+1

All Articles