opencv剪切图片

debug模式下:

包含include和lib

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>$(3RD)\opencv\include;$(IncludePath)</IncludePath>
<LibraryPath>$(3RD)\opencv\x64\vc11\lib;$(LibraryPath)</LibraryPath>
</PropertyGroup>

 

画虚线

void drawDottedLine(cv::Mat& img, cv::Point start, cv::Point end, const cv::Scalar& color, int thickness = 1, int lineType = 8, int dotLength = 10, int gapLength = 10)
{
    double length = sqrt(pow(end.x - start.x, 2) + pow(end.y - start.y, 2));
    double lineX = start.x;
    double lineY = start.y;
    double stepX = static_cast<double>(end.x - start.x) / length;
    double stepY = static_cast<double>(end.y - start.y) / length;

    for (double i = 0; i < length; i += dotLength + gapLength)
    {
        cv::Point pt1(lineX, lineY);
        lineX += stepX * dotLength;
        lineY += stepY * dotLength;
        if (lineX > end.x) lineX = end.x;
        if (lineY > end.y) lineY = end.y;
        cv::Point pt2(lineX, lineY);
        cv::line(img, pt1, pt2, color, thickness, lineType);

        lineX += stepX * gapLength;
        lineY += stepY * gapLength;
    }
}

void dottedRectangle(cv::Mat& img, cv::Point pt1, cv::Point pt2)
{
    cv::Scalar color = cvScalar(0, 0, 255, 0);
    int thickness = 1;
    int lineType = 8;
    int dotLength = 10;
    int gapLength = 10;

    cv::Point topLeft(pt1.x, pt1.y);
    cv::Point topRight(pt2.x, pt1.y);
    cv::Point bottomLeft(pt1.x, pt2.y);
    cv::Point bottomRight(pt2.x, pt2.y);
    
    drawDottedLine(img, topLeft, topRight, color, thickness, lineType, dotLength, gapLength); // 上边
    drawDottedLine(img, topRight, bottomRight, color, thickness, lineType, dotLength, gapLength); // 右边
    drawDottedLine(img, topLeft, bottomLeft, color, thickness, lineType, dotLength, gapLength); // 下边
    drawDottedLine(img, bottomLeft, bottomRight, color, thickness, lineType, dotLength, gapLength); // 左边
}

void XXX::on_mouse(int event, int x, int y, int flags, void* ustc)
{
    static CvPoint pre_pt = { -1,-1 };
    static CvPoint cur_pt = { -1,-1 };
    CvFont font;
    cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA);
    //char temp[16];

    if (event == CV_EVENT_LBUTTONDOWN)
    {
        cvCopy(org, img);
        pre_pt = cvPoint(x, y);
        //sprintf(temp, "(%d,%d)", x, y);
        //cvPutText(img, temp, pre_pt, &font, cvScalar(0, 0, 0, 255));
        //cvCircle(img, pre_pt, 3, cvScalar(255, 0, 0, 0), CV_FILLED, CV_AA, 0);
        cvShowImage("img", img);
        cvCopy(img, tmp);
    }
    else if (event == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))
    {
        cvCopy(tmp, img);
        cur_pt = cvPoint(x, y);
        //sprintf(temp, "(%d,%d)", x, y);
        //cvPutText(img, temp, cur_pt, &font, cvScalar(0, 0, 0, 255));
        cvShowImage("img", img);
    }
    else if (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))
    {
        cvCopy(tmp, img);
        cur_pt = cvPoint(x, y);
        //sprintf(temp, "(%d,%d)", x, y);
        //cvPutText(img, temp, cur_pt, &font, cvScalar(0, 0, 0, 255));
        //cvRectangle(img, pre_pt, cur_pt, CV_RGB(255, 0, 0), 1, 8, 0);
        int minX = min(pre_pt.x, cur_pt.x);
        int maxX = max(pre_pt.x, cur_pt.x);
        int minY = min(pre_pt.y, cur_pt.y);
        int maxY = max(pre_pt.y, cur_pt.y);
        cv::Point pt1(minX, minY);
        cv::Point pt2(maxX, maxY);
        cv::Mat img1(img);
        dottedRectangle(img1, pt1, pt2);
        cvShowImage("img", img);
    }
    else if (event == CV_EVENT_LBUTTONUP)
    {
        cur_pt = cvPoint(x, y);

        int minX = min(pre_pt.x, cur_pt.x);
        int maxX = max(pre_pt.x, cur_pt.x);
        int minY = min(pre_pt.y, cur_pt.y);
        int maxY = max(pre_pt.y, cur_pt.y);
        int width = maxX - minX;    //abs(pre_pt.x - cur_pt.x);
        int height = maxY - minY;   //abs(pre_pt.y - cur_pt.y);
        if (minX < 0 || minY < 0 || maxX > img->width || maxY > img->height
            || width <= 5 || height <= 5)
        {
            //cvDestroyWindow("dst");
            return;
        }

        cvCopy(tmp, img);
        //sprintf(temp, "(%d,%d)", x, y);
        //cvPutText(img, temp, cur_pt, &font, cvScalar(0, 0, 0, 255));
        //cvCircle(img, cur_pt, 3, cvScalar(255, 0, 0, 0), CV_FILLED, CV_AA, 0);
        //cvRectangle(img, pre_pt, cur_pt, CV_RGB(255, 0, 0), 1, 8, 0);
        cv::Point pt1(minX, minY);
        cv::Point pt2(maxX, maxY);
        cv::Mat img1(img);
        dottedRectangle(img1, pt1, pt2);
        cvShowImage("img", img);
        cvCopy(img, tmp);

        dst = cvCreateImage(cvSize(width, height), org->depth, org->nChannels);
        CvRect rect = cvRect(minX, minY, width, height);
        cvSetImageROI(org, rect);
        cvCopy(org, dst);
        cvResetImageROI(org);

        //cvDestroyWindow("dst");
        //cvNamedWindow("dst", 1);
        //cvShowImage("dst", dst);
        //cvWaitKey(0);
        CString strDst = _T("ai_img\\dst.jpg");
        string szPath = CStringFun::wstring_to_string(strDst.GetBuffer());
        strDst.ReleaseBuffer();
        cvSaveImage(szPath.c_str(), dst);
        {
             
 
        }
    }
}

 

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

IplImage* org = 0;
IplImage* img = 0;
IplImage* tmp = 0;
IplImage* dst = 0;
void on_mouse(int event, int x, int y, int flags, void* ustc)
{
    static CvPoint pre_pt = { -1,-1 };
    static CvPoint cur_pt = { -1,-1 };
    CvFont font;
    cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA);
    char temp[16];

    if (event == CV_EVENT_LBUTTONDOWN)
    {
        cvCopy(org, img);
        sprintf(temp, "(%d,%d)", x, y);
        pre_pt = cvPoint(x, y);
        cvPutText(img, temp, pre_pt, &font, cvScalar(0, 0, 0, 255));
        cvCircle(img, pre_pt, 3, cvScalar(255, 0, 0, 0), CV_FILLED, CV_AA, 0);
        cvShowImage("img", img);
        cvCopy(img, tmp);
    }
    else if (event == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))
    {
        cvCopy(tmp, img);
        sprintf(temp, "(%d,%d)", x, y);
        cur_pt = cvPoint(x, y);
        cvPutText(img, temp, cur_pt, &font, cvScalar(0, 0, 0, 255));
        cvShowImage("img", img);
    }
    else if (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))
    {
        cvCopy(tmp, img);
        sprintf(temp, "(%d,%d)", x, y);
        cur_pt = cvPoint(x, y);
        cvPutText(img, temp, cur_pt, &font, cvScalar(0, 0, 0, 255));
        cvRectangle(img, pre_pt, cur_pt, cvScalar(0, 255, 0, 0), 1, 8, 0);
        cvShowImage("img", img);
    }
    else if (event == CV_EVENT_LBUTTONUP)
    {
        cvCopy(tmp, img);
        sprintf(temp, "(%d,%d)", x, y);
        cur_pt = cvPoint(x, y);
        cvPutText(img, temp, cur_pt, &font, cvScalar(0, 0, 0, 255));
        cvCircle(img, cur_pt, 3, cvScalar(255, 0, 0, 0), CV_FILLED, CV_AA, 0);
        cvRectangle(img, pre_pt, cur_pt, cvScalar(0, 255, 0, 0), 1, 8, 0);
        cvShowImage("img", img);
        cvCopy(img, tmp);
        int width = abs(pre_pt.x - cur_pt.x);
        int height = abs(pre_pt.y - cur_pt.y);
        if (width == 0 || height == 0)
        {
            cvDestroyWindow("dst");
            return;
        }
        dst = cvCreateImage(cvSize(width, height), org->depth, org->nChannels);
        CvRect rect;
        if (pre_pt.x < cur_pt.x && pre_pt.y < cur_pt.y)
        {
            rect = cvRect(pre_pt.x, pre_pt.y, width, height);
        }
        else if (pre_pt.x > cur_pt.x && pre_pt.y < cur_pt.y)
        {
            rect = cvRect(cur_pt.x, pre_pt.y, width, height);
        }
        else if (pre_pt.x > cur_pt.x && pre_pt.y > cur_pt.y)
        {
            rect = cvRect(cur_pt.x, cur_pt.y, width, height);
        }
        else if (pre_pt.x<cur_pt.x && pre_pt.y>cur_pt.y)
        {
            rect = cvRect(pre_pt.x, cur_pt.y, width, height);
        }
        cvSetImageROI(org, rect);
        cvCopy(org, dst);
        cvResetImageROI(org);
        cvDestroyWindow("dst");
        cvNamedWindow("dst", 1);
        cvShowImage("dst", dst);
        cvWaitKey(0);
        cvSaveImage("D:\\71253.jpg", dst);
    }
}
void CDlgXXXX::OnStnClickedStaticPic()
{
    CString strTxtImg_ = L"D:\\Picture1.jpg";
    string szTxtImg = CStringFun::wstring_to_string(strTxtImg_.GetBuffer());
    strTxtImg_.ReleaseBuffer();
    org = cvLoadImage("D:\\Picture1.jpg", 1);
    img = cvCloneImage(org);
    tmp = cvCloneImage(org);
    cvNamedWindow("img", 1);
    CRect rtPic;
    m_static_image_.GetWindowRect(&rtPic);
    cvMoveWindow("img", rtPic.left, rtPic.top);

    cvSetMouseCallback("img", on_mouse, 0);
    cvShowImage("img", img);
    
    cvWaitKey(0);
    cvDestroyAllWindows();
    cvReleaseImage(&org);
    cvReleaseImage(&img);
    cvReleaseImage(&tmp);
    cvReleaseImage(&dst);
}

 

posted @ 2024-01-15 16:49  曦花  阅读(4)  评论(0编辑  收藏  举报