openCv crop image

前端 未结 4 1701
野趣味
野趣味 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: "<20&&abs(y-cor1.y)>20) //checking whether the region is too small
            {
                leftup=true;
                cor2.x=x;
                cor2.y=y;
                cout<<"Corner 2: "<

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

提交回复
热议问题