【问题标题】:openCv crop imageopenCv 裁剪图像
【发布时间】:2012-12-31 04:26:44
【问题描述】:

我的 openCv IplImage 裁剪遇到问题。假设 tmp 和 img 都是 IplImage* 。使用代码:

printf("Orig dimensions: %dx%d\n", img->width, img->height);
cvSetImageROI(img, cvRect(0, 0,500,500));
tmp = cvCreateImage(cvGetSize(img),img->depth,img->nChannels);
cvCopy(img, tmp, NULL);
cvResetImageROI(img);
img = cvCloneImage(tmp);
printf("Orig dimensions after crop: %dx%d\n", tmp->width, tmp->height);

当我使用上面的 cvRect 时,我会得到一个尺寸为 500 x 500 的图像,如预期的那样,但是当我使用 rect (400,400,500,500) 时,我会得到一个尺寸为 500 x 320 的图像。

【问题讨论】:

    标签: c++ opencv crop iplimage


    【解决方案1】:

    cvRect 定义为( int x, int y, int width, int height ),而不是(int left, int top, int right, int bottom)。因此,您选择从点 (x,y) = (400,400) 开始的 500x500 区域。我猜你的图像高度是 720 ;)。

    【讨论】:

    • 一个值得接受的答案;出现在我的谷歌搜索的顶部。有时 SO 比官方文档要快得多。
    【解决方案2】:

    使用 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);
    

    【讨论】:

      【解决方案3】:

      您可以使用 python 轻松裁剪图像

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

      为了获得这两个积分,您可以致电cv2.setMouseCallback("image", mouse_crop)。 函数是这样的

      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)
      

      您可以从这里获取详细信息:Mouse Click and Cropping using Python

      对于 C++,你可以这样做:

      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;
      
          }
      
      }
      

      您可以从这里获取详细信息:Mouse Click and Cropping using C++

      【讨论】:

      • 问题是关于C++的
      【解决方案4】:

      试试这个。它的工作。

                           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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-04
        • 2023-03-05
        • 1970-01-01
        • 1970-01-01
        • 2020-10-18
        • 2018-08-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多