openCv crop image

前端 未结 4 1697
野趣味
野趣味 2020-12-31 13:10

I running into problems with my openCv IplImage cropping. Assuming both tmp and img are IplImage* . Using the code:

printf(\"Orig dimensions: %dx%d\\n\", im         


        
相关标签:
4条回答
  • 2020-12-31 13:18

    you can easily crop the image in python by using

    roi = oriImage[refPoint[0][1]:refPoint[1][1], refPoint[0][0]:refPoint[1][0]]
    

    In order to get the two points you can call cv2.setMouseCallback("image", mouse_crop). The function is something like this

    def mouse_crop(event, x, y, flags, param):
        # grab references to the global variables
        global x_start, y_start, x_end, y_end, cropping
    
        # if the left mouse button was DOWN, start RECORDING
        # (x, y) coordinates and indicate that cropping is being
        if event == cv2.EVENT_LBUTTONDOWN:
            x_start, y_start, x_end, y_end = x, y, x, y
            cropping = True
    
        # Mouse is Moving
        elif event == cv2.EVENT_MOUSEMOVE:
            if cropping == True:
                x_end, y_end = x, y
    
        # if the left mouse button was released
        elif event == cv2.EVENT_LBUTTONUP:
            # record the ending (x, y) coordinates
            x_end, y_end = x, y
            cropping = False # cropping is finished
    
            refPoint = [(x_start, y_start), (x_end, y_end)]
    
            if len(refPoint) == 2: #when two points were found
                roi = oriImage[refPoint[0][1]:refPoint[1][1], refPoint[0][0]:refPoint[1][0]]
                cv2.imshow("Cropped", roi)
                cv2.imwrite("crop.jpg",roi)
    

    You can get details from here : Mouse Click and Cropping using Python

    For C++ you can do like below:

    void mouse_call(int event,int x,int y,int,void*)
    {
        if(event==EVENT_LBUTTONDOWN)
        {
            leftDown=true;
            cor1.x=x;
            cor1.y=y;
            cout <<"Corner 1: "<<cor1<<endl;
    
        }
        if(event==EVENT_LBUTTONUP)
        {
            if(abs(x-cor1.x)>20&&abs(y-cor1.y)>20) //checking whether the region is too small
            {
                leftup=true;
                cor2.x=x;
                cor2.y=y;
                cout<<"Corner 2: "<<cor2<<endl;
            }
            else
            {
                cout<<"Select a region more than 20 pixels"<<endl;
            }
        }
    
        if(leftDown==true&&leftup==false) //when the left button is down
        {
            Point pt;
            pt.x=x;
            pt.y=y;
            Mat temp_img=img.clone();
            rectangle(temp_img,cor1,pt,Scalar(0,0,255)); //drawing a rectangle continuously
            imshow("Original",temp_img);
    
        }
        if(leftDown==true&&leftup==true) //when the selection is done
        {
    
            box.width=abs(cor1.x-cor2.x);
            box.height=abs(cor1.y-cor2.y);
            box.x=min(cor1.x,cor2.x);
            box.y=min(cor1.y,cor2.y);
            Mat crop(img,box); //Selecting a ROI(region of interest) from the original pic
            namedWindow("Cropped Image");
            imshow("Cropped Image",crop); //showing the cropped image
            leftDown=false;
            leftup=false;
    
        }
    
    }
    

    You can get details from here : Mouse Click and Cropping using C++

    0 讨论(0)
  • 2020-12-31 13:36

    Try this.It's work.

                         IplImage *source_image;
                         IplImage *cropped_Image1;
    
                         cout << "Width:" << source_image->width << " pixels" << endl;
                         cout << "Height:" << source_image->height << " pixels" << endl;
                         int width = source_image->width;
                         int lenght = source_image->height;
    
                         cv::Rect roi;
                         roi.x = 1200; //1200     // 950
                         roi.y = 355; //350      //150 
                         roi.width = 2340; //2360          //2750
                         roi.height = 1425;  //1235 /2500         //2810   //2465 fully braille sheet
    
    
                         cropped_Image1 = cvCreateImage(cvSize(roi.width, roi.height), source_image->depth, source_image->nChannels);
                         cvSetImageROI(source_image, roi);
                         cvCopy(source_image, cropped_Image1);
                         cvResetImageROI(source_image);
                         cvShowImage("Cropped Image", cropped_Image1);
                         cvSaveImage("1_cropped.jpg", cropped_Image1);
    
    0 讨论(0)
  • 2020-12-31 13:38

    Image cropping using OpenCV

    Mat image=imread("image.png",1);
    
    int startX=200,startY=200,width=100,height=100
    
    Mat ROI(image, Rect(startX,startY,width,height));
    
    Mat croppedImage;
    
    // Copy the data into new matrix
    ROI.copyTo(croppedImage);
    
    imwrite("newImage.png",croppedImage);
    
    0 讨论(0)
  • 2020-12-31 13:41

    cvRect is defined as ( int x, int y, int width, int height ), not (int left, int top, int right, int bottom). So you are selecting a 500x500 region starting at the point (x,y) = (400,400). I am guessing your image height is 720 ;).

    0 讨论(0)
提交回复
热议问题